hydrus_api/wrapper/
page.rs

1use crate::api_core::common::{FileIdentifier, PageInformation};
2use crate::api_core::endpoints::searching_and_fetching_files::Identifiers;
3use crate::error::Result;
4use crate::utils::split_file_identifiers_into_hashes_and_ids;
5use crate::Client;
6
7#[derive(Clone)]
8pub struct HydrusPage {
9    client: Client,
10    pub key: String,
11    pub name: String,
12    pub page_type: PageType,
13    pub children: Vec<HydrusPage>,
14}
15
16impl HydrusPage {
17    pub(crate) fn from_info(client: Client, info: PageInformation) -> Self {
18        let children = info
19            .pages
20            .into_iter()
21            .map(|i| HydrusPage::from_info(client.clone(), i))
22            .collect();
23
24        Self {
25            client,
26            key: info.page_key,
27            name: info.name,
28            page_type: PageType::from_raw_type(info.page_type),
29            children,
30        }
31    }
32
33    /// Focuses the page
34    pub async fn focus(&self) -> Result<()> {
35        self.client.focus_page(&self.key).await
36    }
37
38    /// Returns an identifier of the page
39    pub fn id(&self) -> PageIdentifier {
40        PageIdentifier::key(&self.key)
41    }
42
43    /// Adds files to a page
44    pub async fn add_files(&self, files: Vec<FileIdentifier>) -> Result<()> {
45        let (ids, mut hashes) = split_file_identifiers_into_hashes_and_ids(files);
46
47        // resolve file ids to hashes
48        hashes.append(&mut self.resolve_file_ids_to_hashes(ids).await?);
49
50        self.client
51            .add_files_to_page(&self.key, [].to_vec(), hashes)
52            .await
53    }
54
55    async fn resolve_file_ids_to_hashes(&self, ids: Vec<u64>) -> Result<Vec<String>> {
56        let mut hashes = Vec::new();
57        for id in ids {
58            let metadata = self
59                .client
60                .get_file_metadata_by_identifier::<Identifiers>(FileIdentifier::ID(id))
61                .await?;
62            hashes.push(metadata.hash);
63        }
64        Ok(hashes)
65    }
66}
67
68#[derive(Clone)]
69pub enum PageIdentifier {
70    Name(String),
71    Key(String),
72}
73
74impl PageIdentifier {
75    pub fn name<S: ToString>(name: S) -> Self {
76        Self::Name(name.to_string())
77    }
78
79    pub fn key<S: ToString>(key: S) -> Self {
80        Self::Key(key.to_string())
81    }
82}
83
84#[derive(Clone, Debug, PartialOrd, PartialEq)]
85pub enum PageType {
86    GalleryDownloader,
87    SimpleDownloader,
88    HardDriveImport,
89    Petitions,
90    FileSearch,
91    URLDownloader,
92    Duplicates,
93    ThreadWatcher,
94    PageOfPages,
95    Unknown,
96}
97
98impl PageType {
99    pub(crate) fn from_raw_type(raw_type: u32) -> Self {
100        match raw_type {
101            1 => Self::GalleryDownloader,
102            2 => Self::SimpleDownloader,
103            3 => Self::HardDriveImport,
104            4 => Self::Petitions,
105            5 => Self::FileSearch,
106            6 => Self::URLDownloader,
107            7 => Self::Duplicates,
108            8 => Self::ThreadWatcher,
109            9 => Self::PageOfPages,
110            _ => Self::Unknown,
111        }
112    }
113}