Files
empidee/src/commands/queue/swapid.rs
2025-12-08 04:14:32 +09:00

58 lines
1.6 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 CommandRequest<'_> for SwapIdRequest {
const COMMAND: &'static str = "swapid";
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!("{} {} {}", Self::COMMAND, self.songid1, self.songid2)
}
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
let songid1 = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let songid1 = songid1
.parse()
.map_err(|_| RequestParserError::SyntaxError(0, songid1.to_string()))?;
let songid2 = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let songid2 = songid2
.parse()
.map_err(|_| RequestParserError::SyntaxError(0, songid2.to_string()))?;
debug_assert!(parts.next().is_none());
Ok(SwapIdRequest { songid1, songid2 })
}
}
empty_command_response!(SwapId);
impl Command<'_, '_> for SwapId {
type Request = SwapIdRequest;
type Response = SwapIdResponse;
}