140 lines
4.3 KiB
Rust
140 lines
4.3 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";
|
|
const MIN_ARGS: u32 = 1;
|
|
const MAX_ARGS: Option<u32> = Some(3);
|
|
|
|
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.push('\n');
|
|
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(Self::missing_arguments_error(0)),
|
|
};
|
|
|
|
let mut argument_index_counter = 0;
|
|
let mut sort_or_window = parts.next();
|
|
let mut sort = None;
|
|
if let Some("sort") = sort_or_window {
|
|
argument_index_counter += 1;
|
|
let s = parts
|
|
.next()
|
|
.ok_or(RequestParserError::MissingKeywordValue {
|
|
keyword: "sort",
|
|
argument_index: argument_index_counter,
|
|
})?;
|
|
sort = Some(
|
|
s.parse()
|
|
.map_err(|_| RequestParserError::SubtypeParserError {
|
|
argument_index: argument_index_counter,
|
|
expected_type: "Sort".to_string(),
|
|
raw_input: s.to_string(),
|
|
})?,
|
|
);
|
|
sort_or_window = parts.next();
|
|
}
|
|
|
|
let mut window = None;
|
|
if let Some("window") = sort_or_window {
|
|
argument_index_counter += 1;
|
|
let w = parts
|
|
.next()
|
|
.ok_or(RequestParserError::MissingKeywordValue {
|
|
keyword: "window",
|
|
argument_index: argument_index_counter,
|
|
})?;
|
|
window = Some(
|
|
w.parse()
|
|
.map_err(|_| RequestParserError::SubtypeParserError {
|
|
argument_index: argument_index_counter,
|
|
expected_type: "WindowRange".to_string(),
|
|
raw_input: w.to_string(),
|
|
})?,
|
|
);
|
|
}
|
|
|
|
Self::throw_if_too_many_arguments(parts)?;
|
|
|
|
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;
|
|
}
|