summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjakka <jakka@jakka.su>2025-10-01 12:57:32 +0300
committerjakka <jakka@jakka.su>2025-10-01 12:57:32 +0300
commit228f24cb3020b719540fc9665e4aff21528fc69e (patch)
tree83194bfda6c6d97ffec81aef2545802b859b4e5f /src
parentc177ba7a49caf63582719a4086e5032e2c939cfa (diff)
broke down modules and renamed them
Diffstat (limited to 'src')
-rw-r--r--src/async_lib.rs33
-rw-r--r--src/async_lib/client.rs (renamed from src/client_async.rs)2
-rw-r--r--src/async_lib/traits.rs (renamed from src/traits_async.rs)2
-rw-r--r--src/async_lib/types.rs (renamed from src/types.rs)0
-rw-r--r--src/lib.rs17
-rw-r--r--src/sync_lib.rs3
-rw-r--r--src/sync_lib/client.rs0
-rw-r--r--src/sync_lib/traits.rs67
-rw-r--r--src/sync_lib/types.rs249
-rw-r--r--src/tests.rs27
10 files changed, 360 insertions, 40 deletions
diff --git a/src/async_lib.rs b/src/async_lib.rs
new file mode 100644
index 0000000..6bbdd22
--- /dev/null
+++ b/src/async_lib.rs
@@ -0,0 +1,33 @@
+pub mod client;
+pub mod traits;
+pub mod types;
+
+#[cfg(test)]
+mod tests {
+ use crate::async_lib::{client::*, traits::*};
+
+ #[tokio::test]
+ async fn test_service_name_info() {
+ let mut client: HydrusClient = HydrusClient::new("http://127.0.0.1:51251/");
+ client.set_api_key("7ab7accf6cf12b2c6c30436cd8fe16361aee33679dbd90da279b5c22b33d622a");
+ let _ = client.get_service_name("all my files").await.unwrap();
+ }
+
+ #[tokio::test]
+ async fn test_service_key_info() {
+ let mut client: HydrusClient = HydrusClient::new("http://127.0.0.1:51251/");
+ client.set_api_key("7ab7accf6cf12b2c6c30436cd8fe16361aee33679dbd90da279b5c22b33d622a");
+ let _ = client
+ .get_service_key("616c6c206c6f63616c206d65646961")
+ .await
+ .unwrap();
+ }
+
+ #[tokio::test]
+ async fn test_get_services() {
+ let mut client: HydrusClient = HydrusClient::new("http://127.0.0.1:51251/");
+ client.set_api_key("7ab7accf6cf12b2c6c30436cd8fe16361aee33679dbd90da279b5c22b33d622a");
+ let res = client.get_services().await.unwrap();
+ assert!(!res.is_empty())
+ }
+}
diff --git a/src/client_async.rs b/src/async_lib/client.rs
index c189a06..58e3b83 100644
--- a/src/client_async.rs
+++ b/src/async_lib/client.rs
@@ -5,7 +5,7 @@ use reqwest::{Body, RequestBuilder};
use serde::Deserialize;
use tokio_util::codec::{BytesCodec, FramedRead};
-use crate::{traits_async::*, types::*};
+use crate::async_lib::{traits::*, types::*};
type Result<T> = std::result::Result<T, HydrusError>;
diff --git a/src/traits_async.rs b/src/async_lib/traits.rs
index 9c4de18..8177968 100644
--- a/src/traits_async.rs
+++ b/src/async_lib/traits.rs
@@ -1,6 +1,6 @@
use std::{collections::HashMap, path::PathBuf};
-use crate::types::*;
+use crate::async_lib::types::*;
use async_trait::async_trait;
type Result<T> = std::result::Result<T, HydrusError>;
diff --git a/src/types.rs b/src/async_lib/types.rs
index 7762065..7762065 100644
--- a/src/types.rs
+++ b/src/async_lib/types.rs
diff --git a/src/lib.rs b/src/lib.rs
index c2a0350..d059b6b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,13 +1,8 @@
-//! crate with hydrus client and traits necessary for accesing hydrus API
+//! crate with hydrus client and traits necessary for fully using hydrus API
-/// this crate's hydrus client async implementation
+/// async traits, types and client implementation
#[cfg(feature = "async")]
-pub mod client_async;
-/// traits for accessing hydrus API
-#[cfg(feature = "async")]
-pub mod traits_async;
-/// various objects for de/serializing requests
-pub mod types;
-
-#[cfg(test)]
-mod tests;
+pub mod async_lib;
+/// sync traits, types and client implementation
+#[cfg(feature = "sync")]
+pub mod sync_lib;
diff --git a/src/sync_lib.rs b/src/sync_lib.rs
new file mode 100644
index 0000000..fd28c4c
--- /dev/null
+++ b/src/sync_lib.rs
@@ -0,0 +1,3 @@
+pub mod client;
+pub mod traits;
+pub mod types;
diff --git a/src/sync_lib/client.rs b/src/sync_lib/client.rs
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/sync_lib/client.rs
diff --git a/src/sync_lib/traits.rs b/src/sync_lib/traits.rs
new file mode 100644
index 0000000..1f8e1dd
--- /dev/null
+++ b/src/sync_lib/traits.rs
@@ -0,0 +1,67 @@
+use std::{collections::HashMap, path::PathBuf};
+
+use crate::sync_lib::types::*;
+
+type Result<T> = std::result::Result<T, HydrusError>;
+
+/// Trait for accessing and managing keys and services.
+pub trait AccessManagement {
+ /// Register a new external program with the client. This requires the 'add from api request' mini-dialog under services->review services to be open, otherwise it will 403.
+ fn request_new_permissions(
+ &self,
+ name: &str,
+ permissions: &[HydrusPermissions],
+ ) -> Result<String>;
+ /// Get a new session key.
+ fn get_session_key(&self) -> Result<String>;
+ /// Check your access key is valid.
+ fn verify_access_key(&self, key: &str) -> Result<KeyInfo>;
+ /// Ask the client about a specific service by providing its name.
+ fn get_service_name(&self, name: &str) -> Result<Service>;
+ /// Ask the client about a specific service by providing its key.
+ fn get_service_key(&self, key: &str) -> Result<Service>;
+ /// Ask the client about its services.
+ fn get_services(&self) -> Result<HashMap<String, Service>>;
+}
+
+/// Trait for importing and deleting files.
+pub trait ImportingAndDeletingFiles {
+ /// Tell the client to import a file by providing a local (hydrus-local) file path.
+ fn add_file_via_path(
+ &self,
+ path: PathBuf,
+ delete: Option<bool>,
+ domains: Option<FileDomain>,
+ ) -> Result<AddFileResponse>;
+ /// Tell the client to import a file by sending the file.
+ fn add_file_via_file(&self, file: PathBuf) -> Result<AddFileResponse>;
+ /// Tell the client to send files to the trash.
+ fn delete_files(
+ &self,
+ file: HydrusFile,
+ domain: Option<FileDomain>,
+ reason: Option<String>,
+ ) -> Result<()>;
+ /// Tell the client to restore files that were previously deleted to their old file service(s).
+ fn undelete_files(&self, file: HydrusFile, domain: Option<FileDomain>) -> Result<()>;
+ /// Tell the client to forget that it once deleted files.
+ fn clear_file_deletion_records(&self, file: HydrusFile) -> Result<()>;
+ /// Copy files from one local file domain to another.
+ fn migrate_files(&self, file: HydrusFile, domain: FileDomain) -> Result<()>;
+ /// Tell the client to archive inboxed files.
+ fn archive_files(&self, file: HydrusFile) -> Result<()>;
+ /// Tell the client re-inbox archived files.
+ fn unarchive_files(&self, file: HydrusFile) -> Result<()>;
+ /// Generate hashes for an arbitrary file by providing a local path to the file.
+ fn generate_hashes_for_path(&self, file: PathBuf) -> Result<HashResponse>;
+ /// Generate hashes for an arbitrary file by sending the file.
+ fn generate_hashes_for_file(&self, file: PathBuf) -> Result<HashResponse>;
+}
+
+pub trait ImportingAndEditingUrls {
+ fn get_url_files(
+ &self,
+ url: &str,
+ doublecheck_file_system: Option<bool>,
+ ) -> Result<FilesUrlResponse>;
+}
diff --git a/src/sync_lib/types.rs b/src/sync_lib/types.rs
new file mode 100644
index 0000000..7762065
--- /dev/null
+++ b/src/sync_lib/types.rs
@@ -0,0 +1,249 @@
+use std::path::PathBuf;
+
+use serde::{Deserialize, Serialize};
+use serde_repr::{Deserialize_repr, Serialize_repr};
+use thiserror::Error;
+
+/// Error wrapper
+#[derive(Error, Debug)]
+pub enum HydrusError {
+ #[error("failed to connect to Hydrus")]
+ NetworkError(reqwest::Error),
+ #[error("failed to encode/Deserialize data")]
+ DeserializeError(serde_json::Error),
+ #[error("io error")]
+ IOError(std::io::Error),
+ #[error("api or session key needed")]
+ KeyNotSupplied,
+}
+
+impl From<serde_json::Error> for HydrusError {
+ fn from(value: serde_json::Error) -> Self {
+ HydrusError::DeserializeError(value)
+ }
+}
+
+impl From<std::io::Error> for HydrusError {
+ fn from(value: std::io::Error) -> Self {
+ HydrusError::IOError(value)
+ }
+}
+
+impl From<reqwest::Error> for HydrusError {
+ fn from(value: reqwest::Error) -> Self {
+ HydrusError::NetworkError(value)
+ }
+}
+
+/// Hydrus serivce permissions object
+#[derive(PartialEq, Debug, Clone, Serialize_repr, Deserialize_repr)]
+#[repr(u8)]
+pub enum HydrusPermissions {
+ ImportAndEditURLs = 0,
+ ImportAndEditFiles,
+ EditFileTags,
+ SearchAndFetchFiles,
+ ManagePages,
+ ManageCookiesAndHeaders,
+ ManageDatabase,
+ EditFileNotes,
+ EditFileRelationships,
+ EditFileRatings,
+ ManagePopups,
+ EditFileTimes,
+ CommitPending,
+ SeeLocalPaths,
+ Null = 255,
+}
+
+/// Hydrus key information struct
+#[derive(Deserialize, Debug)]
+pub struct KeyInfo {
+ pub name: String,
+ pub permits_everything: bool,
+ pub basic_permissions: Vec<HydrusPermissions>,
+ pub human_permissions: String,
+}
+
+/// Hydrus service type object
+#[derive(PartialEq, Debug, Clone, Serialize_repr, Deserialize_repr)]
+#[repr(u8)]
+pub enum ServiceType {
+ TagRepository = 0,
+ FileRepository,
+ LocalFileDomain,
+ LocalTagDomain = 5,
+ NumericalRating,
+ BoolRating,
+ AllKnownTags = 10,
+ AllKnownFiles,
+ LocalBooru,
+ IPFS,
+ Trash,
+ AllLocalFiles,
+ FileNotes = 17,
+ ClientAPI,
+ DeletedFromAnywhere,
+ LocalUpdates,
+ AllMyFiles,
+ IncDecRating,
+ ServerAdmin = 99,
+ Null = 255,
+}
+
+/// Hydrus service struct
+#[derive(Deserialize, Debug, Clone)]
+pub struct Service {
+ pub name: String,
+ #[serde(default)]
+ pub service_key: String,
+ pub r#type: ServiceType,
+ pub type_pretty: String,
+ #[serde(default)]
+ pub star_shape: Option<String>,
+ #[serde(default)]
+ pub min_stars: Option<u8>,
+ #[serde(default)]
+ pub max_stars: Option<u8>,
+}
+
+/// Hydrus file domains
+pub enum FileDomain {
+ FileServiceKey(String),
+ FileServiceKeys(Vec<String>),
+ DeletedFileServiceKey(String),
+ DeletedFileServiceKeys(Vec<String>),
+}
+
+/// Payload for importing a file via providing a local path
+#[derive(Serialize, Debug, Default)]
+pub struct AddFileRequest {
+ pub path: PathBuf,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub delete_after_successful_import: Option<bool>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub file_service_key: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub file_service_keys: Option<Vec<String>>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub deleted_file_service_key: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub deleted_file_service_keys: Option<Vec<String>>,
+}
+/// File importing status
+#[derive(PartialEq, Debug, Clone, Serialize_repr, Deserialize_repr)]
+#[repr(u8)]
+pub enum AddFileStatus {
+ SuccessfulImport = 1,
+ AlreadyInDatabase,
+ PreviouslyDeleted,
+ FailedToImport,
+ FileVetoed = 7,
+}
+/// File importing api response
+#[derive(Deserialize)]
+pub struct AddFileResponse {
+ pub status: AddFileStatus,
+ pub hash: String,
+ pub note: String,
+}
+
+/// Hydrus file object
+#[derive(Debug, Clone, Serialize)]
+pub enum HydrusFile {
+ #[serde(rename(serialize = "file_id"))]
+ FileId(String),
+ #[serde(rename(serialize = "file_ids"))]
+ FileIds(Vec<String>),
+ #[serde(rename(serialize = "hash"))]
+ Hash(String),
+ #[serde(rename(serialize = "hashes"))]
+ Hashes(Vec<String>),
+}
+
+impl Default for HydrusFile {
+ fn default() -> Self {
+ Self::FileId(String::from(""))
+ }
+}
+
+/// Payload for various file-related requests
+#[derive(Debug, Default)]
+pub struct FileRequest {
+ pub file: HydrusFile,
+ pub delete_after_successful_import: Option<bool>,
+ pub file_service_key: Option<String>,
+ pub file_service_keys: Option<Vec<String>>,
+ pub deleted_file_service_key: Option<String>,
+ pub deleted_file_service_keys: Option<Vec<String>>,
+ pub reason: Option<String>,
+}
+
+impl Serialize for FileRequest {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: serde::Serializer,
+ {
+ use serde::ser::SerializeMap;
+ let mut map = serializer.serialize_map(Some(1))?;
+
+ match &self.file {
+ HydrusFile::FileId(id) => map.serialize_entry("file_id", &id)?,
+ HydrusFile::FileIds(ids) => map.serialize_entry("file_ids", &ids)?,
+ HydrusFile::Hash(hash) => map.serialize_entry("hash", &hash)?,
+ HydrusFile::Hashes(hashes) => map.serialize_entry("hashes", &hashes)?,
+ }
+
+ if let Some(val) = &self.reason {
+ map.serialize_entry("reason", &val)?;
+ }
+
+ if let Some(val) = &self.file_service_key {
+ map.serialize_entry("file_service_key", &val)?;
+ }
+
+ if let Some(val) = &self.file_service_keys {
+ map.serialize_entry("file_service_keys", &val)?;
+ }
+
+ if let Some(val) = &self.deleted_file_service_key {
+ map.serialize_entry("deleted_file_service_key", &val)?;
+ }
+
+ if let Some(val) = &self.deleted_file_service_keys {
+ map.serialize_entry("deleted_file_service_keys", &val)?;
+ }
+
+ map.end()
+ }
+}
+
+#[derive(Debug, Deserialize)]
+pub struct HashResponse {
+ pub hash: String,
+ #[serde(default)]
+ pub perceptual_hashes: Option<Vec<String>>,
+ #[serde(default)]
+ pub pixel_hash: Option<String>,
+}
+
+#[derive(Debug, Deserialize_repr)]
+#[repr(u8)]
+pub enum UrlStatus {
+ NotInDatabase = 0,
+ AlreadyInDatabase = 2,
+ PreviouslyDeleted,
+}
+
+#[derive(Debug, Deserialize)]
+pub struct UrlFileStatus {
+ pub status: UrlStatus,
+ pub hash: String,
+ pub note: String,
+}
+
+#[derive(Debug, Deserialize)]
+pub struct FilesUrlResponse {
+ pub normalised_url: String,
+ pub url_file_statuses: Vec<UrlFileStatus>,
+}
diff --git a/src/tests.rs b/src/tests.rs
deleted file mode 100644
index 8cb725e..0000000
--- a/src/tests.rs
+++ /dev/null
@@ -1,27 +0,0 @@
-use crate::client_async::HydrusClient;
-use crate::{traits_async::*, types::*};
-
-#[tokio::test]
-async fn test_service_name_info() {
- let mut client: HydrusClient = HydrusClient::new("http://127.0.0.1:51251/");
- client.set_api_key("7ab7accf6cf12b2c6c30436cd8fe16361aee33679dbd90da279b5c22b33d622a");
- let _ = client.get_service_name("all my files").await.unwrap();
-}
-
-#[tokio::test]
-async fn test_service_key_info() {
- let mut client: HydrusClient = HydrusClient::new("http://127.0.0.1:51251/");
- client.set_api_key("7ab7accf6cf12b2c6c30436cd8fe16361aee33679dbd90da279b5c22b33d622a");
- let _ = client
- .get_service_key("616c6c206c6f63616c206d65646961")
- .await
- .unwrap();
-}
-
-#[tokio::test]
-async fn test_get_services() {
- let mut client: HydrusClient = HydrusClient::new("http://127.0.0.1:51251/");
- client.set_api_key("7ab7accf6cf12b2c6c30436cd8fe16361aee33679dbd90da279b5c22b33d622a");
- let res = client.get_services().await.unwrap();
- assert!(!res.is_empty())
-}