56d7c942db
This also exposes all the command types as public API
137 lines
4.3 KiB
Rust
137 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::{DbSelectionPrintResponse, DbSongInfo, Sort, WindowRange},
|
|
};
|
|
|
|
pub struct Search;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
pub struct SearchRequest {
|
|
filter: Filter,
|
|
sort: Option<Sort>,
|
|
window: Option<WindowRange>,
|
|
}
|
|
|
|
impl SearchRequest {
|
|
pub fn new(filter: Filter, sort: Option<Sort>, window: Option<WindowRange>) -> Self {
|
|
Self {
|
|
filter,
|
|
sort,
|
|
window,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl CommandRequest for SearchRequest {
|
|
const COMMAND: &'static str = "search";
|
|
const MIN_ARGS: u32 = 1;
|
|
const MAX_ARGS: Option<u32> = Some(3);
|
|
|
|
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",
|
|
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",
|
|
raw_input: w.to_string(),
|
|
})?,
|
|
);
|
|
}
|
|
|
|
Self::throw_if_too_many_arguments(parts)?;
|
|
|
|
Ok(SearchRequest {
|
|
filter,
|
|
sort,
|
|
window,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
pub struct SearchResponse(Vec<DbSongInfo>);
|
|
|
|
impl SearchResponse {
|
|
pub fn new(items: Vec<DbSongInfo>) -> Self {
|
|
SearchResponse(items)
|
|
}
|
|
}
|
|
|
|
impl CommandResponse for SearchResponse {
|
|
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
|
DbSelectionPrintResponse::parse(parts)?
|
|
.into_iter()
|
|
.map(|i| match i {
|
|
DbSelectionPrintResponse::Song(db_song_info) => Ok(db_song_info),
|
|
DbSelectionPrintResponse::Directory(_db_directory_info) => Err(
|
|
ResponseParserError::UnexpectedProperty("directory".to_string()),
|
|
),
|
|
DbSelectionPrintResponse::Playlist(_db_playlist_info) => Err(
|
|
ResponseParserError::UnexpectedProperty("playlist".to_string()),
|
|
),
|
|
})
|
|
.collect::<Result<Vec<DbSongInfo>, ResponseParserError>>()
|
|
.map(SearchResponse)
|
|
}
|
|
}
|
|
|
|
impl Command for Search {
|
|
type Request = SearchRequest;
|
|
type Response = SearchResponse;
|
|
}
|