Lines
0 %
Functions
use crate::{
commands::{
Command, GenericResponseValue, Request, RequestParserError, RequestParserResult,
ResponseAttributes, ResponseParserError,
},
common::{SongId, SongPosition},
};
pub struct AddId;
pub struct AddIdResponse {
pub id: SongId,
}
impl Command for AddId {
type Response = AddIdResponse;
const COMMAND: &'static str = "addid";
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
let uri = match parts.next() {
Some(s) => s,
None => return Err(RequestParserError::UnexpectedEOF),
let position = match parts.next() {
Some(s) => Some(
s.parse::<SongPosition>()
.map_err(|_| RequestParserError::SyntaxError(1, s.to_owned()))?,
),
None => None,
debug_assert!(parts.next().is_none());
Ok((Request::AddId(uri.to_string(), position), ""))
fn parse_response(
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError> {
let parts: Vec<_> = parts.into();
let (key, value) = parts.first().ok_or(ResponseParserError::UnexpectedEOF)?;
debug_assert!(key == &"Id");
let value = match value {
GenericResponseValue::Text(value) => value,
GenericResponseValue::Binary(_) => {
return Err(ResponseParserError::UnexpectedPropertyType("Id", "Binary"))
let id = value
.parse()
.map_err(|_| ResponseParserError::InvalidProperty("Id", value.to_owned()))?;
Ok(AddIdResponse { id })