23563cfb9b
This commit contains a rewrite of the response tokenizer, which introduces lazy parsing of the response, handling of binary data, some tests, as well as just generally more robustness against errors.
62 lines
1.7 KiB
Rust
62 lines
1.7 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{
|
|
commands::{Command, Request, RequestParserError, RequestParserResult, ResponseParserError},
|
|
common::{SongPosition, Uri},
|
|
request_tokenizer::RequestTokenizer,
|
|
response_tokenizer::ResponseAttributes,
|
|
};
|
|
|
|
pub struct Add;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
pub struct AddRequest {
|
|
uri: Uri,
|
|
position: Option<SongPosition>,
|
|
}
|
|
|
|
impl AddRequest {
|
|
pub fn new(uri: Uri, position: Option<SongPosition>) -> Self {
|
|
Self { uri, position }
|
|
}
|
|
}
|
|
|
|
impl Command for Add {
|
|
type Request = AddRequest;
|
|
type Response = ();
|
|
const COMMAND: &'static str = "add";
|
|
|
|
fn serialize_request(&self, request: Self::Request) -> String {
|
|
match request.position {
|
|
Some(position) => format!("{} {} {}", Self::COMMAND, request.uri, position),
|
|
None => format!("{} {}", Self::COMMAND, request.uri),
|
|
}
|
|
}
|
|
|
|
fn parse_request(mut parts: RequestTokenizer<'_>) -> 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::Add(uri.to_string(), position), ""))
|
|
}
|
|
|
|
fn parse_response(
|
|
parts: ResponseAttributes<'_>,
|
|
) -> Result<Self::Response, ResponseParserError<'_>> {
|
|
debug_assert!(parts.is_empty());
|
|
Ok(())
|
|
}
|
|
}
|