Files
empidee/src/commands/queue/prioid.rs

62 lines
1.8 KiB
Rust

use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
common::types::{Priority, SongId},
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
};
pub struct PrioId;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PrioIdRequest {
pub prio: Priority,
pub songids: Vec<SongId>,
}
impl Command for PrioId {
type Request = PrioIdRequest;
type Response = ();
const COMMAND: &'static str = "prioid";
fn serialize_request(&self, request: Self::Request) -> String {
let songids = request
.songids
.iter()
.map(|id| id.to_string())
.collect::<Vec<String>>()
.join(",");
format!("{} {} {}", Self::COMMAND, request.prio, songids)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
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)?;
// TODO: This is just a guess to satisfy compilation, double check it
let songids = songids
.split(',')
.map(|s| {
s.parse()
.map_err(|_| RequestParserError::SyntaxError(0, s.to_string()))
})
.collect::<Result<Vec<u32>, RequestParserError>>()?;
debug_assert!(parts.next().is_none());
Ok((Request::PrioId(prio, songids), ""))
}
fn parse_response(
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
debug_assert!(parts.is_empty());
Ok(())
}
}