57 lines
1.5 KiB
Rust
57 lines
1.5 KiB
Rust
use crate::{
|
|
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
|
request_tokenizer::RequestTokenizer,
|
|
};
|
|
|
|
pub struct Random;
|
|
|
|
pub struct RandomRequest(bool);
|
|
|
|
impl CommandRequest for RandomRequest {
|
|
const COMMAND: &'static str = "random";
|
|
const MIN_ARGS: u32 = 1;
|
|
const MAX_ARGS: Option<u32> = Some(1);
|
|
|
|
fn into_request_enum(self) -> crate::Request {
|
|
crate::Request::Random(self.0)
|
|
}
|
|
|
|
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
|
match request {
|
|
crate::Request::Random(state) => Some(RandomRequest(state)),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn serialize(&self) -> String {
|
|
let state = if self.0 { "1" } else { "0" };
|
|
format!("{} {}\n", Self::COMMAND, state)
|
|
}
|
|
|
|
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
|
let state = match parts.next() {
|
|
Some("0") => false,
|
|
Some("1") => true,
|
|
Some(s) => {
|
|
return Err(RequestParserError::SubtypeParserError {
|
|
argument_index: 0,
|
|
expected_type: "bool".to_owned(),
|
|
raw_input: s.to_owned(),
|
|
});
|
|
}
|
|
None => return Err(Self::missing_arguments_error(0)),
|
|
};
|
|
|
|
Self::throw_if_too_many_arguments(parts)?;
|
|
|
|
Ok(RandomRequest(state))
|
|
}
|
|
}
|
|
|
|
empty_command_response!(Random);
|
|
|
|
impl Command for Random {
|
|
type Request = RandomRequest;
|
|
type Response = RandomResponse;
|
|
}
|