Add wrapper for hydrus tag search

develop
trivernis 2 years ago
parent 982f747976
commit 203815734a
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -4,4 +4,5 @@ pub mod notes_builder;
pub mod or_chain_builder; pub mod or_chain_builder;
pub mod search_builder; pub mod search_builder;
pub mod tag_builder; pub mod tag_builder;
pub mod tag_search_builder;
pub mod tagging_builder; pub mod tagging_builder;

@ -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<Vec<(u64, Tag)>> {
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)
}
}

@ -14,6 +14,8 @@ use crate::wrapper::version::Version;
use crate::Client; use crate::Client;
use std::fmt::Debug; 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 /// A high level wrapper for the hydrus API for easier management of files, tags
/// urls etc. /// urls etc.
pub struct Hydrus { pub struct Hydrus {
@ -94,6 +96,11 @@ impl Hydrus {
SearchBuilder::new(self.client.clone()) SearchBuilder::new(self.client.clone())
} }
/// Starts a search request for tags with additional filter options
pub fn search_tags<S: ToString>(&self, query: S) -> TagSearchBuilder {
TagSearchBuilder::new(self.client.clone(), query.to_string())
}
/// Returns a hydrus page by page key /// Returns a hydrus page by page key
pub async fn page<S: AsRef<str> + Debug>(&self, page_key: S) -> Result<HydrusPage> { pub async fn page<S: AsRef<str> + Debug>(&self, page_key: S) -> Result<HydrusPage> {
let info_response = self.client.get_page_info(page_key).await?; let info_response = self.client.get_page_info(page_key).await?;

@ -78,3 +78,9 @@ async fn it_sets_the_user_agent() {
.await .await
.unwrap(); .unwrap();
} }
#[tokio::test]
async fn it_searches_for_tags() {
let hydrus = common::get_hydrus();
hydrus.search_tags("t").as_displayed().run().await.unwrap();
}

Loading…
Cancel
Save