74 lines
2.0 KiB
Rust
74 lines
2.0 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{
|
|
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
|
request_tokenizer::RequestTokenizer,
|
|
types::SongId,
|
|
};
|
|
|
|
pub struct SwapId;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
pub struct SwapIdRequest {
|
|
pub songid1: SongId,
|
|
pub songid2: SongId,
|
|
}
|
|
|
|
impl SwapIdRequest {
|
|
pub fn new(songid1: SongId, songid2: SongId) -> Self {
|
|
Self { songid1, songid2 }
|
|
}
|
|
}
|
|
|
|
impl CommandRequest for SwapIdRequest {
|
|
const COMMAND: &'static str = "swapid";
|
|
const MIN_ARGS: u32 = 2;
|
|
const MAX_ARGS: Option<u32> = Some(2);
|
|
|
|
fn into_request_enum(self) -> crate::Request {
|
|
crate::Request::SwapId(self.songid1, self.songid2)
|
|
}
|
|
|
|
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
|
match request {
|
|
crate::Request::SwapId(songid1, songid2) => Some(SwapIdRequest { songid1, songid2 }),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn serialize(&self) -> String {
|
|
format!("{} {} {}\n", Self::COMMAND, self.songid1, self.songid2)
|
|
}
|
|
|
|
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
|
let songid1 = parts.next().ok_or(Self::missing_arguments_error(0))?;
|
|
let songid1 = songid1
|
|
.parse()
|
|
.map_err(|_| RequestParserError::SubtypeParserError {
|
|
argument_index: 0,
|
|
expected_type: "SongId",
|
|
raw_input: songid1.to_string(),
|
|
})?;
|
|
|
|
let songid2 = parts.next().ok_or(Self::missing_arguments_error(1))?;
|
|
let songid2 = songid2
|
|
.parse()
|
|
.map_err(|_| RequestParserError::SubtypeParserError {
|
|
argument_index: 1,
|
|
expected_type: "SongId",
|
|
raw_input: songid2.to_string(),
|
|
})?;
|
|
|
|
Self::throw_if_too_many_arguments(parts)?;
|
|
|
|
Ok(SwapIdRequest { songid1, songid2 })
|
|
}
|
|
}
|
|
|
|
empty_command_response!(SwapId);
|
|
|
|
impl Command for SwapId {
|
|
type Request = SwapIdRequest;
|
|
type Response = SwapIdResponse;
|
|
}
|