49 lines
1.3 KiB
Rust
49 lines
1.3 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";
|
|
|
|
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!("{} {}", 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::SyntaxError(0, s.to_owned())),
|
|
None => return Err(RequestParserError::UnexpectedEOF),
|
|
};
|
|
|
|
debug_assert!(parts.next().is_none());
|
|
|
|
Ok(RandomRequest(state))
|
|
}
|
|
}
|
|
|
|
empty_command_response!(Random);
|
|
|
|
impl Command<'_, '_> for Random {
|
|
type Request = RandomRequest;
|
|
type Response = RandomResponse;
|
|
}
|