From 3e37af540efab8fac1b79aa9384a007a3a2d144a Mon Sep 17 00:00:00 2001 From: askiiart Date: Sun, 1 Dec 2024 23:06:16 -0600 Subject: [PATCH] clean up docs a bit, and remove InternalSearchParameters as it was unneeded --- README.md | 4 +-- src/api.rs | 65 +++++++++++------------------------------------- src/data.rs | 40 +---------------------------- src/notes/mod.rs | 2 +- 4 files changed, 18 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index f97af52..b9f1f65 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ A safe, multi-threaded, async toolkit for adding Torznab APIs to programs. You just focus on the indexer itself, we abstract away the hell that is the Torznab API. -Just fill in your own relevant functions and config, and torznab-toolkit will run the API for you +Just fill in your own relevant functions and config ([`Config`]), and torznab-toolkit will run the API for you ```rust use torznab_toolkit; @@ -11,7 +11,7 @@ let config: torznab_toolkit::config::Config = /* config goes here */ torznab_toolkit::run(config); ``` -The environment variables `ROCKET_ADDRESS` and `ROCKET_PORT` specify the address and port it will run on; these currently cannot be configured any other way. See the [relevant docs](https://rocket.rs/guide/v0.5/deploying/) for details. +To configure what it listens on, just change `ROCKET_ADDRESS` and `ROCKET_PORT`; see the [relevant docs](https://rocket.rs/guide/v0.5/deploying/) for details. --- diff --git a/src/api.rs b/src/api.rs index 169d96d..0d667a7 100644 --- a/src/api.rs +++ b/src/api.rs @@ -32,7 +32,7 @@ struct SearchForm { impl SearchForm { /// Converts it to a SearchParameters object - fn to_parameters(&self, conf: Config) -> InternalSearchParameters { + fn to_parameters(&self, conf: Config, search_type: &str) -> SearchParameters { let mut categories: Option> = None; if !self.cat.is_none() { // unholy amalgation of code to make the comma-separated list of strings into a vector of integers @@ -71,7 +71,8 @@ impl SearchForm { limit = 1 } - return InternalSearchParameters { + return SearchParameters { + search_type: search_type.to_string(), q: self.q.clone(), apikey: self.apikey.clone(), categories: categories, @@ -212,12 +213,12 @@ pub(crate) async fn search( form: SearchForm, ) -> status::Custom> { // oh god this is horrible but it works - let parameters = form.to_parameters((**conf).clone()); + let parameters = form.to_parameters((**conf).clone(), "search"); let mut unauthorized = false; match conf.auth { Some(auth) => { - match parameters.clone().apikey { + match parameters.apikey.clone() { Some(apikey) => { if !auth(apikey).unwrap() { unauthorized = true; @@ -236,9 +237,7 @@ pub(crate) async fn search( return status::Custom(Status::Unauthorized, RawXml("401 Unauthorized".to_string())); } - let search_parameters: SearchParameters = parameters.to_search_param("search"); - - return search_handler(conf, search_parameters).await; + return search_handler(conf, parameters).await; } #[get("/api?t=tvsearch&", rank = 3)] @@ -248,7 +247,7 @@ pub(crate) async fn tv_search( form: SearchForm, ) -> status::Custom> { // oh god this is horrible but it works - let parameters = form.to_parameters((**conf).clone()); + let parameters = form.to_parameters((**conf).clone(), "tv-search"); let mut unauthorized = false; match conf.auth { @@ -272,16 +271,7 @@ pub(crate) async fn tv_search( return status::Custom(Status::Unauthorized, RawXml("401 Unauthorized".to_string())); } - let search_parameters: SearchParameters = parameters.to_search_param("tv-search"); - - /* - * return status::Custom( - * Status::NotImplemented, - * RawXml("501 Not Implemented: Search function not implemented".to_string()), - * ); - */ - - return search_handler(conf, search_parameters).await; + return search_handler(conf, parameters).await; } #[get("/api?t=movie&", rank = 4)] @@ -291,7 +281,7 @@ pub(crate) async fn movie_search( form: SearchForm, ) -> status::Custom> { // oh god this is horrible but it works - let parameters = form.to_parameters((**conf).clone()); + let parameters = form.to_parameters((**conf).clone(), "movie-search"); let mut unauthorized = false; match conf.auth { @@ -315,16 +305,7 @@ pub(crate) async fn movie_search( return status::Custom(Status::Unauthorized, RawXml("401 Unauthorized".to_string())); } - let search_parameters: SearchParameters = parameters.to_search_param("movie-search"); - - /* - * return status::Custom( - * Status::NotImplemented, - * RawXml("501 Not Implemented: Search function not implemented".to_string()), - * ); - */ - - return search_handler(conf, search_parameters).await; + return search_handler(conf, parameters).await; } #[get("/api?t=music&", rank = 5)] @@ -334,7 +315,7 @@ pub(crate) async fn music_search( form: SearchForm, ) -> status::Custom> { // oh god this is horrible but it works - let parameters = form.to_parameters((**conf).clone()); + let parameters = form.to_parameters((**conf).clone(), "audio-search"); let mut unauthorized = false; match conf.auth { @@ -358,16 +339,7 @@ pub(crate) async fn music_search( return status::Custom(Status::Unauthorized, RawXml("401 Unauthorized".to_string())); } - let search_parameters: SearchParameters = parameters.to_search_param("audio-search"); - - /* - * return status::Custom( - * Status::NotImplemented, - * RawXml("501 Not Implemented: Search function not implemented".to_string()), - * ); - */ - - return search_handler(conf, search_parameters).await; + return search_handler(conf, parameters).await; } #[get("/api?t=book&", rank = 6)] @@ -377,7 +349,7 @@ pub(crate) async fn book_search( form: SearchForm, ) -> status::Custom> { // oh god this is horrible but it works - let parameters = form.to_parameters((**conf).clone()); + let parameters = form.to_parameters((**conf).clone(), "book-search"); let mut unauthorized = false; match conf.auth { @@ -401,16 +373,7 @@ pub(crate) async fn book_search( return status::Custom(Status::Unauthorized, RawXml("401 Unauthorized".to_string())); } - let search_parameters: SearchParameters = parameters.to_search_param("book-search"); - - /* - * return status::Custom( - * Status::NotImplemented, - * RawXml("501 Not Implemented: Search function not implemented".to_string()), - * ); - */ - - return search_handler(conf, search_parameters).await; + return search_handler(conf, parameters).await; } async fn search_handler( diff --git a/src/data.rs b/src/data.rs index 16119db..b1959a7 100644 --- a/src/data.rs +++ b/src/data.rs @@ -122,45 +122,7 @@ pub struct Config { pub caps: Caps, } -/// 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 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 { - /// Converts InternalSearchParameters to SearchParmaters, i.e. add `search_type` - /// - /// Search types: `search`, `tv-search`, `movie-search`, `audio-search`, `book-search` - pub(crate) fn to_search_param(&self, search_type: &str) -> SearchParameters { - return SearchParameters { - search_type: search_type.to_string(), - 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, - }; - } -} - +#[derive(Debug, Clone, PartialEq, Eq)] /// Holds the parameters for a search query pub struct SearchParameters { /// What type of search this is diff --git a/src/notes/mod.rs b/src/notes/mod.rs index f6a5ccd..a6f9892 100644 --- a/src/notes/mod.rs +++ b/src/notes/mod.rs @@ -1,2 +1,2 @@ +pub mod implementation; pub mod usage; -pub mod implementation; \ No newline at end of file