Files
empidee/src/commands/music_database/readcomments.rs

52 lines
1.5 KiB
Rust

use std::collections::HashMap;
use crate::{
request_tokenizer::RequestTokenizer,
commands::{
Command, GenericResponseValue, Request, RequestParserError, RequestParserResult,
ResponseAttributes, ResponseParserError,
},
common::Uri,
};
pub struct ReadComments;
pub type ReadCommentsResponse = HashMap<String, String>;
impl Command for ReadComments {
type Request = Uri;
type Response = ReadCommentsResponse;
const COMMAND: &'static str = "readcomments";
fn serialize_request(&self, request: Self::Request) -> String {
format!("{} {}", Self::COMMAND, request)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
let uri = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let uri = uri
.parse()
.map_err(|_| RequestParserError::SyntaxError(1, uri.to_owned()))?;
debug_assert!(parts.next().is_none());
Ok((Request::ReadComments(uri), ""))
}
fn parse_response(
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
let parts: HashMap<_, _> = parts.into();
let comments = parts
.iter()
.map(|(k, v)| match v {
GenericResponseValue::Text(s) => Ok((k.to_string(), s.to_string())),
GenericResponseValue::Binary(_) => Err(ResponseParserError::SyntaxError(1, k)),
})
.collect::<Result<HashMap<_, _>, ResponseParserError>>()?;
Ok(comments)
}
}