76 lines
2.2 KiB
Rust
76 lines
2.2 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";
|
|
const MIN_ARGS: u32 = 2;
|
|
// TODO: should this be 2?
|
|
const MAX_ARGS: Option<u32> = None;
|
|
|
|
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!("{} {} {}\n", Self::COMMAND, self.prio, songids)
|
|
}
|
|
|
|
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
|
let prio = parts.next().ok_or(Self::missing_arguments_error(0))?;
|
|
let prio = prio
|
|
.parse()
|
|
.map_err(|_| RequestParserError::SubtypeParserError {
|
|
argument_index: 0,
|
|
expected_type: "Priority".to_string(),
|
|
raw_input: prio.to_string(),
|
|
})?;
|
|
|
|
// TODO: determine how to count arguments here...
|
|
let songids = parts.next().ok_or(Self::missing_arguments_error(1))?;
|
|
let songids = songids
|
|
.split(',')
|
|
.map(|s| {
|
|
s.parse()
|
|
.map_err(|_| RequestParserError::SyntaxError(0, s.to_string()))
|
|
})
|
|
.collect::<Result<Vec<SongId>, RequestParserError>>()?;
|
|
|
|
Self::throw_if_too_many_arguments(parts)?;
|
|
|
|
Ok(PrioIdRequest { prio, songids })
|
|
}
|
|
}
|
|
|
|
empty_command_response!(PrioId);
|
|
|
|
impl Command for PrioId {
|
|
type Request = PrioIdRequest;
|
|
type Response = PrioIdResponse;
|
|
}
|