commands: parse to Self::Request

This commit is contained in:
2025-12-05 22:00:11 +09:00
parent 925eb1941a
commit d811840ea1
134 changed files with 710 additions and 660 deletions
+6 -3
View File
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
commands::{Command, Request, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::{SongPosition, Uri},
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -33,7 +33,7 @@ impl Command for Add {
}
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let uri = match parts.next() {
Some(s) => s,
None => return Err(RequestParserError::UnexpectedEOF),
@@ -49,7 +49,10 @@ impl Command for Add {
debug_assert!(parts.next().is_none());
Ok((Request::Add(uri.to_string(), position), ""))
Ok(AddRequest {
uri: uri.to_string(),
position,
})
}
fn parse_response(
+6 -3
View File
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
commands::{Command, Request, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::{SongId, SongPosition, Uri},
request_tokenizer::RequestTokenizer,
response_tokenizer::{ResponseAttributes, get_next_and_parse_property},
@@ -32,7 +32,7 @@ impl Command for AddId {
}
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let uri = match parts.next() {
Some(s) => s,
None => return Err(RequestParserError::UnexpectedEOF),
@@ -48,7 +48,10 @@ impl Command for AddId {
debug_assert!(parts.next().is_none());
Ok((Request::AddId(uri.to_string(), position), ""))
Ok(AddIdRequest {
uri: uri.to_string(),
position,
})
}
fn parse_response(
+7 -6
View File
@@ -1,10 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{
Command, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError,
},
commands::{Command, RequestParserError, ResponseAttributes, ResponseParserError},
common::types::{SongId, TagName, TagValue},
request_tokenizer::RequestTokenizer,
};
@@ -33,7 +30,7 @@ impl Command for AddTagId {
)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let songid = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let songid = songid
.parse()
@@ -51,7 +48,11 @@ impl Command for AddTagId {
debug_assert!(parts.next().is_none());
Ok((Request::AddTagId(songid, tag_name, tag_value), ""))
Ok(AddTagIdRequest {
songid,
tag_name,
tag_value,
})
}
fn parse_response(
+3 -3
View File
@@ -1,5 +1,5 @@
use crate::{
commands::{Command, Request, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
};
@@ -15,9 +15,9 @@ impl Command for Clear {
Self::COMMAND.to_string()
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
debug_assert!(parts.next().is_none());
Ok((Request::Clear, ""))
Ok(())
}
fn parse_response(
+3 -4
View File
@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::{SongId, TagName},
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -25,7 +24,7 @@ impl Command for ClearTagId {
format!("{} {} {}", Self::COMMAND, request.songid, request.tag_name)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let songid = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let songid = songid
.parse()
@@ -38,7 +37,7 @@ impl Command for ClearTagId {
debug_assert!(parts.next().is_none());
Ok((Request::ClearTagId(songid, tag_name), ""))
Ok(ClearTagIdRequest { songid, tag_name })
}
fn parse_response(
+3 -4
View File
@@ -1,8 +1,7 @@
use std::str::FromStr;
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::OneOrRange,
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -19,14 +18,14 @@ impl Command for Delete {
format!("{} {}", Self::COMMAND, request)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let pos_or_range = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let one_or_range = OneOrRange::from_str(pos_or_range)
.map_err(|_| RequestParserError::SyntaxError(0, pos_or_range.to_string()))?;
debug_assert!(parts.next().is_none());
Ok((Request::Delete(one_or_range), ""))
Ok(one_or_range)
}
fn parse_response(
+3 -4
View File
@@ -1,6 +1,5 @@
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::SongId,
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -17,7 +16,7 @@ impl Command for DeleteId {
format!("{} {}", Self::COMMAND, request)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let id = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let id = id
.parse()
@@ -25,7 +24,7 @@ impl Command for DeleteId {
debug_assert!(parts.next().is_none());
Ok((Request::DeleteId(id), ""))
Ok(id)
}
fn parse_response(
+3 -6
View File
@@ -1,10 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{
Command, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError,
},
commands::{Command, RequestParserError, ResponseAttributes, ResponseParserError},
common::types::{AbsouluteRelativeSongPosition, OneOrRange},
request_tokenizer::RequestTokenizer,
};
@@ -26,7 +23,7 @@ impl Command for Move {
format!("{} {} {}", Self::COMMAND, request.from_or_range, request.to)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let from_or_range = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let from_or_range = from_or_range
.parse()
@@ -39,7 +36,7 @@ impl Command for Move {
debug_assert!(parts.next().is_none());
Ok((Request::Move(from_or_range, to), ""))
Ok(MoveRequest { from_or_range, to })
}
fn parse_response(
+3 -6
View File
@@ -1,10 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{
Command, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError,
},
commands::{Command, RequestParserError, ResponseAttributes, ResponseParserError},
common::types::{AbsouluteRelativeSongPosition, SongId},
request_tokenizer::RequestTokenizer,
};
@@ -26,7 +23,7 @@ impl Command for MoveId {
format!("{} {} {}", Self::COMMAND, request.id, request.to)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let id = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let id = id
.parse()
@@ -39,7 +36,7 @@ impl Command for MoveId {
debug_assert!(parts.next().is_none());
Ok((Request::MoveId(id, to), ""))
Ok(MoveIdRequest { id, to })
}
fn parse_response(
+3 -4
View File
@@ -1,6 +1,5 @@
use crate::{
Request,
commands::{Command, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
};
@@ -16,9 +15,9 @@ impl Command for Playlist {
Self::COMMAND.to_string()
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
debug_assert!(parts.next().is_none());
Ok((Request::Playlist, ""))
Ok(())
}
fn parse_response(
+7 -4
View File
@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::{Sort, WindowRange},
filter::Filter,
request_tokenizer::RequestTokenizer,
@@ -34,7 +33,7 @@ impl Command for PlaylistFind {
cmd
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let filter = match parts.next() {
Some(f) => {
Filter::parse(f).map_err(|_| RequestParserError::SyntaxError(1, f.to_owned()))?
@@ -64,7 +63,11 @@ impl Command for PlaylistFind {
debug_assert!(parts.next().is_none());
Ok((Request::PlaylistFind(filter, sort, window), ""))
Ok(PlaylistFindRequest {
filter,
sort,
window,
})
}
fn parse_response(
+3 -4
View File
@@ -1,6 +1,5 @@
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::SongId,
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -17,7 +16,7 @@ impl Command for PlaylistId {
format!("{} {}", Self::COMMAND, request)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let id = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let id = id
.parse()
@@ -25,7 +24,7 @@ impl Command for PlaylistId {
debug_assert!(parts.next().is_none());
Ok((Request::PlaylistId(id), ""))
Ok(id)
}
fn parse_response(
+3 -4
View File
@@ -1,6 +1,5 @@
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::OneOrRange,
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -20,7 +19,7 @@ impl Command for PlaylistInfo {
}
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let one_or_range = parts
.next()
.map(|s| {
@@ -31,7 +30,7 @@ impl Command for PlaylistInfo {
debug_assert!(parts.next().is_none());
Ok((Request::PlaylistInfo(one_or_range), ""))
Ok(one_or_range)
}
fn parse_response(
+7 -4
View File
@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::{Sort, WindowRange},
filter::Filter,
request_tokenizer::RequestTokenizer,
@@ -34,7 +33,7 @@ impl Command for PlaylistSearch {
cmd
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let filter = match parts.next() {
Some(f) => {
Filter::parse(f).map_err(|_| RequestParserError::SyntaxError(1, f.to_owned()))?
@@ -64,7 +63,11 @@ impl Command for PlaylistSearch {
debug_assert!(parts.next().is_none());
Ok((Request::PlaylistSearch(filter, sort, window), ""))
Ok(PlaylistSearchRequest {
filter,
sort,
window,
})
}
fn parse_response(
+3 -4
View File
@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::{PlaylistVersion, WindowRange},
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -28,7 +27,7 @@ impl Command for PlChanges {
}
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let version = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let version = version
.parse()
@@ -44,7 +43,7 @@ impl Command for PlChanges {
debug_assert!(parts.next().is_none());
Ok((Request::PlChanges(version, window), ""))
Ok(PlChangesRequest { version, window })
}
fn parse_response(
+3 -4
View File
@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::{PlaylistVersion, WindowRange},
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -28,7 +27,7 @@ impl Command for PlChangesPosId {
}
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let version = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let version = version
.parse()
@@ -44,7 +43,7 @@ impl Command for PlChangesPosId {
debug_assert!(parts.next().is_none());
Ok((Request::PlChangesPosId(version, window), ""))
Ok(PlChangesPosIdRequest { version, window })
}
fn parse_response(
+3 -4
View File
@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::{Priority, WindowRange},
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -25,7 +24,7 @@ impl Command for Prio {
format!("{} {} {}", Self::COMMAND, request.prio, request.window)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let prio = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let prio = prio
.parse()
@@ -38,7 +37,7 @@ impl Command for Prio {
debug_assert!(parts.next().is_none());
Ok((Request::Prio(prio, window), ""))
Ok(PrioRequest { prio, window })
}
fn parse_response(
+3 -4
View File
@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::{Priority, SongId},
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -31,7 +30,7 @@ impl Command for PrioId {
format!("{} {} {}", Self::COMMAND, request.prio, songids)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let prio = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let prio = prio
.parse()
@@ -49,7 +48,7 @@ impl Command for PrioId {
debug_assert!(parts.next().is_none());
Ok((Request::PrioId(prio, songids), ""))
Ok(PrioIdRequest { prio, songids })
}
fn parse_response(
+6 -4
View File
@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::{SongId, TimeInterval},
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -30,7 +29,7 @@ impl Command for RangeId {
)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let songid = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let songid = songid
.parse()
@@ -43,7 +42,10 @@ impl Command for RangeId {
debug_assert!(parts.next().is_none());
Ok((Request::RangeId(songid, time_interval), ""))
Ok(RangeIdRequest {
songid,
time_interval,
})
}
fn parse_response(
+3 -4
View File
@@ -1,6 +1,5 @@
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::OneOrRange,
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -20,7 +19,7 @@ impl Command for Shuffle {
}
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let range = parts
.next()
.map(|range| {
@@ -32,7 +31,7 @@ impl Command for Shuffle {
debug_assert!(parts.next().is_none());
Ok((Request::Shuffle(range), ""))
Ok(range)
}
fn parse_response(
+3 -4
View File
@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::SongPosition,
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -30,7 +29,7 @@ impl Command for Swap {
)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let songpos1 = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let songpos1 = songpos1
.parse()
@@ -43,7 +42,7 @@ impl Command for Swap {
debug_assert!(parts.next().is_none());
Ok((Request::Swap(songpos1, songpos2), ""))
Ok(SwapRequest { songpos1, songpos2 })
}
fn parse_response(
+3 -4
View File
@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::{
Request,
commands::{Command, RequestParserError, RequestParserResult, ResponseParserError},
commands::{Command, RequestParserError, ResponseParserError},
common::types::SongId,
request_tokenizer::RequestTokenizer,
response_tokenizer::ResponseAttributes,
@@ -25,7 +24,7 @@ impl Command for SwapId {
format!("{} {} {}", Self::COMMAND, request.songid1, request.songid2)
}
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
fn parse_request(mut parts: RequestTokenizer<'_>) -> Result<Self::Request, RequestParserError> {
let songid1 = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let songid1 = songid1
.parse()
@@ -38,7 +37,7 @@ impl Command for SwapId {
debug_assert!(parts.next().is_none());
Ok((Request::SwapId(songid1, songid2), ""))
Ok(SwapIdRequest { songid1, songid2 })
}
fn parse_response(