From 7b8d04bc7d59641dede2546cde6f5d57562d48f6 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 18 Jul 2021 17:22:08 +0200 Subject: [PATCH] Add wrapper for hydrus pages Signed-off-by: trivernis --- src/wrapper/hydrus.rs | 21 ++++++++++++ src/wrapper/page.rs | 69 +++++++++++++++++++++++++++++++++++++- tests/wrapper/mod.rs | 1 + tests/wrapper/test_page.rs | 32 ++++++++++++++++++ 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 tests/wrapper/test_page.rs diff --git a/src/wrapper/hydrus.rs b/src/wrapper/hydrus.rs index 0cf58ab..33583b9 100644 --- a/src/wrapper/hydrus.rs +++ b/src/wrapper/hydrus.rs @@ -5,6 +5,7 @@ use crate::utils::tag_list_to_string_list; use crate::wrapper::builders::import_builder::ImportBuilder; use crate::wrapper::builders::tagging_builder::TaggingBuilder; use crate::wrapper::hydrus_file::HydrusFile; +use crate::wrapper::page::HydrusPage; use crate::wrapper::service::Services; use crate::wrapper::tag::Tag; use crate::wrapper::url::Url; @@ -94,4 +95,24 @@ impl Hydrus { Ok(files) } + + /// Returns a hydrus page by page key + pub async fn page>(&self, page_key: S) -> Result { + let info_response = self.client.get_page_info(page_key).await?; + + Ok(HydrusPage::from_info( + self.client.clone(), + info_response.page_info, + )) + } + + /// Returns the root page in the client + pub async fn root_page(&self) -> Result { + let pages_response = self.client.get_pages().await?; + + Ok(HydrusPage::from_info( + self.client.clone(), + pages_response.pages, + )) + } } diff --git a/src/wrapper/page.rs b/src/wrapper/page.rs index de4e914..1338a49 100644 --- a/src/wrapper/page.rs +++ b/src/wrapper/page.rs @@ -1,6 +1,42 @@ +use crate::api_core::common::PageInformation; +use crate::error::Result; +use crate::Client; + #[derive(Clone)] pub struct HydrusPage { - pub id: PageIdentifier, + client: Client, + pub key: String, + pub name: String, + pub page_type: PageType, + pub children: Vec, +} + +impl HydrusPage { + pub(crate) fn from_info(client: Client, info: PageInformation) -> Self { + let children = info + .pages + .into_iter() + .map(|i| HydrusPage::from_info(client.clone(), i)) + .collect(); + + Self { + client, + key: info.page_key, + name: info.name, + page_type: PageType::from_raw_type(info.page_type), + children, + } + } + + /// Focuses the page + pub async fn focus(&self) -> Result<()> { + self.client.focus_page(&self.key).await + } + + /// Returns an identifier of the page + pub fn id(&self) -> PageIdentifier { + PageIdentifier::key(&self.key) + } } #[derive(Clone)] @@ -18,3 +54,34 @@ impl PageIdentifier { Self::Key(key.to_string()) } } + +#[derive(Clone, Debug, PartialOrd, PartialEq)] +pub enum PageType { + GalleryDownloader, + SimpleDownloader, + HardDriveImport, + Petitions, + FileSearch, + URLDownloader, + Duplicates, + ThreadWatcher, + PageOfPages, + Unknown, +} + +impl PageType { + pub(crate) fn from_raw_type(raw_type: u32) -> Self { + match raw_type { + 1 => Self::GalleryDownloader, + 2 => Self::SimpleDownloader, + 3 => Self::HardDriveImport, + 4 => Self::Petitions, + 5 => Self::FileSearch, + 6 => Self::URLDownloader, + 7 => Self::Duplicates, + 8 => Self::ThreadWatcher, + 9 => Self::PageOfPages, + _ => Self::Unknown, + } + } +} diff --git a/tests/wrapper/mod.rs b/tests/wrapper/mod.rs index 44e1c9a..2133569 100644 --- a/tests/wrapper/mod.rs +++ b/tests/wrapper/mod.rs @@ -2,3 +2,4 @@ mod test_files; mod test_hydrus; mod test_import; mod test_url; +mod test_page; diff --git a/tests/wrapper/test_page.rs b/tests/wrapper/test_page.rs new file mode 100644 index 0000000..9bcf39a --- /dev/null +++ b/tests/wrapper/test_page.rs @@ -0,0 +1,32 @@ +use super::super::common; +use hydrus_api::wrapper::page::HydrusPage; + +async fn get_page() -> HydrusPage { + let hydrus = common::get_hydrus(); + + hydrus.root_page().await.unwrap() +} + +#[tokio::test] +async fn it_can_be_focused() { + let page = get_page().await; + page.focus().await.unwrap(); +} + +#[tokio::test] +async fn it_has_a_name() { + let page = get_page().await; + assert!(page.name.len() > 0) +} + +#[tokio::test] +async fn it_has_a_key() { + let page = get_page().await; + assert!(page.key.len() > 0) +} + +#[tokio::test] +async fn it_has_a_id() { + let page = get_page().await; + page.id(); +}