68 lines
1.9 KiB
Rust
68 lines
1.9 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{
|
|
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
|
request_tokenizer::RequestTokenizer,
|
|
types::{Priority, SongId},
|
|
};
|
|
|
|
pub struct PrioId;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
pub struct PrioIdRequest {
|
|
pub prio: Priority,
|
|
pub songids: Vec<SongId>,
|
|
}
|
|
|
|
impl CommandRequest<'_> for PrioIdRequest {
|
|
const COMMAND: &'static str = "prioid";
|
|
|
|
fn into_request_enum(self) -> crate::Request {
|
|
crate::Request::PrioId(self.prio, self.songids)
|
|
}
|
|
|
|
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
|
match request {
|
|
crate::Request::PrioId(prio, songids) => Some(PrioIdRequest { prio, songids }),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn serialize(&self) -> String {
|
|
let songids = self
|
|
.songids
|
|
.iter()
|
|
.map(|id| id.to_string())
|
|
.collect::<Vec<String>>()
|
|
.join(",");
|
|
format!("{} {} {}", Self::COMMAND, self.prio, songids)
|
|
}
|
|
|
|
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
|
let prio = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
|
let prio = prio
|
|
.parse()
|
|
.map_err(|_| RequestParserError::SyntaxError(0, prio.to_string()))?;
|
|
|
|
let songids = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
|
let songids = songids
|
|
.split(',')
|
|
.map(|s| {
|
|
s.parse()
|
|
.map_err(|_| RequestParserError::SyntaxError(0, s.to_string()))
|
|
})
|
|
.collect::<Result<Vec<SongId>, RequestParserError>>()?;
|
|
|
|
debug_assert!(parts.next().is_none());
|
|
|
|
Ok(PrioIdRequest { prio, songids })
|
|
}
|
|
}
|
|
|
|
empty_command_response!(PrioId);
|
|
|
|
impl Command<'_, '_> for PrioId {
|
|
type Request = PrioIdRequest;
|
|
type Response = PrioIdResponse;
|
|
}
|