hydrus_api/wrapper/
url.rs1use crate::api_core::endpoints::adding_urls::{
2 URL_TYPE_FILE, URL_TYPE_GALLERY, URL_TYPE_POST, URL_TYPE_WATCHABLE,
3};
4use crate::error::Result;
5use crate::wrapper::address::Address;
6use crate::wrapper::builders::import_builder::UrlImportBuilder;
7use crate::wrapper::hydrus_file::HydrusFile;
8use crate::Client;
9
10#[derive(Clone, Debug, PartialOrd, PartialEq)]
11pub enum UrlType {
12 Post,
13 File,
14 Gallery,
15 Watchable,
16 Unknown,
17}
18
19impl Eq for UrlType {}
20
21impl From<u8> for UrlType {
22 fn from(value: u8) -> Self {
23 match value {
24 v if v == URL_TYPE_POST => Self::Post,
25 v if v == URL_TYPE_FILE => Self::File,
26 v if v == URL_TYPE_GALLERY => Self::Gallery,
27 v if v == URL_TYPE_WATCHABLE => Self::Watchable,
28 _ => Self::Unknown,
29 }
30 }
31}
32
33#[derive(Clone)]
34pub struct Url {
35 pub url: String,
36 pub(crate) client: Client,
37 pub normalised_url: String,
38 pub url_type: UrlType,
39 pub match_name: String,
40 pub can_parse: bool,
41}
42
43impl Url {
44 pub async fn files(&mut self) -> Result<Vec<HydrusFile>> {
46 let response = self.client.get_url_files(&self.url).await?;
47 let files = response
48 .url_file_statuses
49 .into_iter()
50 .map(|file| {
51 HydrusFile::from_raw_status_and_hash(self.client.clone(), file.status, file.hash)
52 })
53 .collect();
54
55 Ok(files)
56 }
57
58 pub fn import(&mut self) -> UrlImportBuilder {
60 UrlImportBuilder::new(self.client.clone(), &self.url)
61 }
62
63 pub fn address(&self) -> Address {
65 let url = self
66 .normalised_url
67 .trim_start_matches("http://")
68 .trim_start_matches("https://");
69
70 Address::from_str(self.client.clone(), url)
71 }
72
73 pub async fn associate(&mut self, hashes: Vec<String>) -> Result<()> {
75 self.client
76 .associate_urls(vec![self.url.clone()], hashes)
77 .await
78 }
79
80 pub async fn disassociate(&mut self, hashes: Vec<String>) -> Result<()> {
82 self.client
83 .disassociate_urls(vec![self.url.clone()], hashes)
84 .await
85 }
86}