Add wrapper for hydrus pages

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/2/head
trivernis 3 years ago
parent 92ff53e163
commit 7b8d04bc7d
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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<S: AsRef<str>>(&self, page_key: S) -> Result<HydrusPage> {
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<HydrusPage> {
let pages_response = self.client.get_pages().await?;
Ok(HydrusPage::from_info(
self.client.clone(),
pages_response.pages,
))
}
}

@ -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<HydrusPage>,
}
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,
}
}
}

@ -2,3 +2,4 @@ mod test_files;
mod test_hydrus;
mod test_import;
mod test_url;
mod test_page;

@ -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();
}
Loading…
Cancel
Save