58 lines
1.7 KiB
Rust
58 lines
1.7 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{
|
|
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
|
common::types::{AbsouluteRelativeSongPosition, OneOrRange},
|
|
request_tokenizer::RequestTokenizer,
|
|
};
|
|
|
|
pub struct Move;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
pub struct MoveRequest {
|
|
pub from_or_range: OneOrRange,
|
|
pub to: AbsouluteRelativeSongPosition,
|
|
}
|
|
|
|
impl CommandRequest<'_> for MoveRequest {
|
|
const COMMAND: &'static str = "move";
|
|
|
|
fn into_request_enum(self) -> crate::Request {
|
|
crate::Request::Move(self.from_or_range, self.to)
|
|
}
|
|
|
|
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
|
match request {
|
|
crate::Request::Move(from_or_range, to) => Some(MoveRequest { from_or_range, to }),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn serialize(&self) -> String {
|
|
format!("{} {} {}", Self::COMMAND, self.from_or_range, self.to)
|
|
}
|
|
|
|
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
|
let from_or_range = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
|
let from_or_range = from_or_range
|
|
.parse()
|
|
.map_err(|_| RequestParserError::SyntaxError(0, from_or_range.to_string()))?;
|
|
|
|
let to = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
|
let to = to
|
|
.parse()
|
|
.map_err(|_| RequestParserError::SyntaxError(0, to.to_string()))?;
|
|
|
|
debug_assert!(parts.next().is_none());
|
|
|
|
Ok(MoveRequest { from_or_range, to })
|
|
}
|
|
}
|
|
|
|
empty_command_response!(Move);
|
|
|
|
impl Command<'_, '_> for Move {
|
|
type Request = MoveRequest;
|
|
type Response = MoveResponse;
|
|
}
|