diff --git a/src/wrapper/builders/mod.rs b/src/wrapper/builders/mod.rs index c3426b8..6961791 100644 --- a/src/wrapper/builders/mod.rs +++ b/src/wrapper/builders/mod.rs @@ -4,4 +4,5 @@ pub mod notes_builder; pub mod or_chain_builder; pub mod search_builder; pub mod tag_builder; +pub mod tag_search_builder; pub mod tagging_builder; diff --git a/src/wrapper/builders/tag_search_builder.rs b/src/wrapper/builders/tag_search_builder.rs new file mode 100644 index 0000000..a89c5a1 --- /dev/null +++ b/src/wrapper/builders/tag_search_builder.rs @@ -0,0 +1,52 @@ +use crate::{ + api_core::{ + common::ServiceIdentifier, + endpoints::adding_tags::{TagDisplayType, TagSearchOptions, TagWithCount}, + }, + error::Result, + wrapper::tag::Tag, + Client, +}; + +pub struct TagSearchBuilder { + client: Client, + query: String, + options: TagSearchOptions, +} + +impl TagSearchBuilder { + pub(crate) fn new(client: Client, query: String) -> Self { + Self { + client, + query, + options: TagSearchOptions::default(), + } + } + + /// Returns a list of tags as displayed in hydrus + /// rather than what is stored in the database. + pub fn as_displayed(mut self) -> Self { + self.options = self.options.display_type(TagDisplayType::Display); + self + } + + /// Adds an additioinal filter for the tag service + pub fn tag_service(mut self, tag_service: ServiceIdentifier) -> Self { + self.options = self.options.tag_service(tag_service); + self + } + + /// Runs the search + pub async fn run(self) -> Result> { + let tags = self + .client + .search_tags(self.query, self.options) + .await? + .tags + .into_iter() + .map(|TagWithCount { value, count }| (count, Tag::from(value))) + .collect(); + + Ok(tags) + } +} diff --git a/src/wrapper/hydrus.rs b/src/wrapper/hydrus.rs index 52a3294..fe8a6df 100644 --- a/src/wrapper/hydrus.rs +++ b/src/wrapper/hydrus.rs @@ -14,6 +14,8 @@ use crate::wrapper::version::Version; use crate::Client; use std::fmt::Debug; +use super::builders::tag_search_builder::TagSearchBuilder; + /// A high level wrapper for the hydrus API for easier management of files, tags /// urls etc. pub struct Hydrus { @@ -94,6 +96,11 @@ impl Hydrus { SearchBuilder::new(self.client.clone()) } + /// Starts a search request for tags with additional filter options + pub fn search_tags(&self, query: S) -> TagSearchBuilder { + TagSearchBuilder::new(self.client.clone(), query.to_string()) + } + /// Returns a hydrus page by page key pub async fn page + Debug>(&self, page_key: S) -> Result { let info_response = self.client.get_page_info(page_key).await?; diff --git a/tests/wrapper/test_hydrus.rs b/tests/wrapper/test_hydrus.rs index 4abd3b8..a86efbe 100644 --- a/tests/wrapper/test_hydrus.rs +++ b/tests/wrapper/test_hydrus.rs @@ -78,3 +78,9 @@ async fn it_sets_the_user_agent() { .await .unwrap(); } + +#[tokio::test] +async fn it_searches_for_tags() { + let hydrus = common::get_hydrus(); + hydrus.search_tags("t").as_displayed().run().await.unwrap(); +}