116 lines
3.4 KiB
Rust
116 lines
3.4 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{
|
|
commands::{Command, CommandRequest, CommandResponse, RequestParserError, ResponseParserError},
|
|
filter::Filter,
|
|
request_tokenizer::RequestTokenizer,
|
|
response_tokenizer::ResponseAttributes,
|
|
types::{DbSongInfo, Priority, SongId, SongPosition, Sort, WindowRange},
|
|
};
|
|
|
|
pub struct PlaylistSearch;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
pub struct PlaylistSearchRequest {
|
|
filter: Filter,
|
|
sort: Option<Sort>,
|
|
window: Option<WindowRange>,
|
|
}
|
|
|
|
impl CommandRequest<'_> for PlaylistSearchRequest {
|
|
const COMMAND: &'static str = "playlistsearch";
|
|
|
|
fn into_request_enum(self) -> crate::Request {
|
|
crate::Request::PlaylistSearch(self.filter, self.sort, self.window)
|
|
}
|
|
|
|
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
|
match request {
|
|
crate::Request::PlaylistSearch(filter, sort, window) => Some(PlaylistSearchRequest {
|
|
filter,
|
|
sort,
|
|
window,
|
|
}),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn serialize(&self) -> String {
|
|
let mut cmd = format!("{} {}", Self::COMMAND, self.filter);
|
|
if let Some(sort) = &self.sort {
|
|
cmd.push_str(&format!(" sort {}", sort));
|
|
}
|
|
if let Some(window) = &self.window {
|
|
cmd.push_str(&format!(" window {}", window));
|
|
}
|
|
cmd
|
|
}
|
|
|
|
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
|
let filter = match parts.next() {
|
|
Some(f) => {
|
|
Filter::parse(f).map_err(|_| RequestParserError::SyntaxError(1, f.to_owned()))?
|
|
}
|
|
None => return Err(RequestParserError::UnexpectedEOF),
|
|
};
|
|
|
|
let mut sort_or_window = parts.next();
|
|
let mut sort = None;
|
|
if let Some("sort") = sort_or_window {
|
|
let s = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
|
sort = Some(
|
|
s.parse()
|
|
.map_err(|_| RequestParserError::SyntaxError(0, s.to_string()))?,
|
|
);
|
|
sort_or_window = parts.next();
|
|
}
|
|
|
|
let mut window = None;
|
|
if let Some("window") = sort_or_window {
|
|
let w = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
|
window = Some(
|
|
w.parse()
|
|
.map_err(|_| RequestParserError::SyntaxError(0, w.to_string()))?,
|
|
);
|
|
}
|
|
|
|
debug_assert!(parts.next().is_none());
|
|
|
|
Ok(PlaylistSearchRequest {
|
|
filter,
|
|
sort,
|
|
window,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
pub struct PlaylistSearchResponse(Vec<PlaylistSearchResponseEntry>);
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
pub struct PlaylistSearchResponseEntry {
|
|
pub position: SongPosition,
|
|
pub id: SongId,
|
|
pub priority: Option<Priority>,
|
|
pub song_info: DbSongInfo,
|
|
}
|
|
|
|
impl CommandResponse<'_> for PlaylistSearchResponse {
|
|
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
|
todo!()
|
|
}
|
|
|
|
fn into_response_enum(self) -> crate::Response {
|
|
todo!()
|
|
}
|
|
|
|
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
|
unimplemented!()
|
|
}
|
|
}
|
|
|
|
impl Command<'_, '_> for PlaylistSearch {
|
|
type Request = PlaylistSearchRequest;
|
|
type Response = PlaylistSearchResponse;
|
|
}
|