From 492579035d884ae5117751be5c34227bbdc90d9d Mon Sep 17 00:00:00 2001 From: askiiart Date: Sat, 30 Nov 2024 21:08:40 -0600 Subject: [PATCH] switch to single search function --- src/api.rs | 4 ++-- src/data.rs | 56 ++++++++++++++++++++++++++++++++++++++++++---------- src/dummy.rs | 4 ---- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/api.rs b/src/api.rs index 5f38cb7..87886cd 100644 --- a/src/api.rs +++ b/src/api.rs @@ -33,7 +33,7 @@ struct SearchForm { impl SearchForm { /// Converts it to a SearchParameters object - fn to_parameters(&self, conf: Config) -> SearchParameters { + fn to_parameters(&self, conf: Config) -> InternalSearchParameters { // TODO: Clean up this code - split it into a separate function? let mut categories: Option> = None; if !self.cat.is_none() { @@ -72,7 +72,7 @@ impl SearchForm { limit = conf.caps.limits.max; } - return SearchParameters { + return InternalSearchParameters { q: self.q.clone(), apikey: self.apikey.clone(), categories: categories, diff --git a/src/data.rs b/src/data.rs index 1e68418..4c869ae 100644 --- a/src/data.rs +++ b/src/data.rs @@ -112,24 +112,60 @@ pub struct Caps { /// The search function (`/api?t=search`) and capabilities (`/api?t=caps` - struct [`Caps`]) are required /// Everything else is optional pub struct Config { - /// The function to use for a free text search + /// The function to use for all search types + /// + /// What search types are available is dependent on what's marked as available in the `searching` field of `caps` ([`Caps`]) + /// + /// Search types: `search`, `tv-search`, `movie-search`, `audio-search`, `book-search` pub search: SearchFunc, /// The auth function - if not specified, then no authorization is needed. pub auth: Option, /// The capabilities of the indexer - see [`Caps`] pub caps: Caps, - /// The function to use for a tv search - pub tvsearch: Option, - /// The function to use for a movie search - pub movie: Option, - /// The function to use for a music search - pub music: Option, - /// The function to use for a book search - pub book: Option, } +/// An internal struct to hold the parameters for a search +/// +/// Before being sent to the "client" program, it's turned into a SearchParameters object by `to_search_param()`, adding the search type #[derive(Debug, Clone, PartialEq, Eq, FromForm)] -pub(crate) struct SearchParameters { +pub(crate) struct InternalSearchParameters { + /// The text query for the search + pub(crate) q: Option, + /// The apikey, for authentication + pub(crate) apikey: Option, + /// A [`Vec`] containing the numeric category IDs to be included in the search results + pub(crate) categories: Option>, + /// A [`Vec`] containing the extended attribute names to be included in the search results + pub(crate) attributes: Option>, + /// Whether *all* extended attributes should be included in the search results; overrules `attributes` + pub(crate) extended_attrs: Option, + /// How many items to skip/offset by in the results. + pub(crate) offset: Option, + /// The maximum number of items to return - also limited to whatever `limits` is in [`Caps`] + pub(crate) limit: u32, +} + +impl InternalSearchParameters { + pub(crate) fn to_search_param(&self, search_type: String) -> SearchParameters { + return SearchParameters { + search_type: search_type, + q: self.q.clone(), + apikey: self.apikey.clone(), + categories: self.categories.clone(), + attributes: self.attributes.clone(), + extended_attrs: self.extended_attrs, + offset: self.offset, + limit: self.limit, + }; + } +} + +/// Holds the parameters for a search query +pub struct SearchParameters { + /// What type of search this is + /// + /// Search types: `search`, `tv-search`, `movie-search`, `audio-search`, `book-search` + pub(crate) search_type: String, /// The text query for the search pub(crate) q: Option, /// The apikey, for authentication diff --git a/src/dummy.rs b/src/dummy.rs index 3db04c6..3da7d46 100644 --- a/src/dummy.rs +++ b/src/dummy.rs @@ -76,10 +76,6 @@ pub(crate) fn create_empty_config() -> Config { genres: Some(genres), tags: Some(tags), }, - book: None, - movie: None, - music: None, - tvsearch: None, }; }