Files
empidee/src/commands/music_database/readpicture.rs
h7x4 9cb92741a4
Some checks failed
Build and test / check (push) Failing after 13s
Build and test / test (push) Failing after 14s
Build and test / build (push) Successful in 57s
Build and test / docs (push) Successful in 1m12s
Implement some more commands
2024-12-13 18:20:03 +01:00

63 lines
1.7 KiB
Rust

use std::collections::HashMap;
use crate::{
commands::{
get_and_parse_property, get_optional_property, get_property, Command, Request,
RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError,
},
common::Offset,
};
pub struct ReadPicture;
pub struct ReadPictureResponse {
pub size: usize,
pub binary: Vec<u8>,
pub mimetype: Option<String>,
}
impl Command for ReadPicture {
type Response = Option<ReadPictureResponse>;
const COMMAND: &'static str = "readpicture";
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
let uri = match parts.next() {
Some(s) => s,
None => return Err(RequestParserError::UnexpectedEOF),
};
let offset = match parts.next() {
Some(s) => s
.parse::<Offset>()
.map_err(|_| RequestParserError::SyntaxError(1, s.to_owned()))?,
None => return Err(RequestParserError::UnexpectedEOF),
};
debug_assert!(parts.next().is_none());
Ok((Request::ReadPicture(uri.to_string(), offset), ""))
}
fn parse_response(
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError> {
let parts: HashMap<_, _> = parts.into();
if parts.is_empty() {
return Ok(None);
}
let size = get_and_parse_property!(parts, "size", Text);
let binary = get_property!(parts, "binary", Binary).into();
let mimetype = get_optional_property!(parts, "mimetype", Text).map(|s| s.to_string());
Ok(Some(ReadPictureResponse {
size,
binary,
mimetype,
}))
}
}