summaryrefslogtreecommitdiff
path: root/src/types.rs
diff options
context:
space:
mode:
authorjakka <jakka@jakka.su>2025-09-13 22:40:54 +0300
committerjakka <jakka@jakka.su>2025-09-13 22:40:54 +0300
commit2ac0735589abc0d4aacfab4a5197da20972bebbf (patch)
tree5ce054364373faea5ec16fa3c1011b63a33af697 /src/types.rs
parentd7cdf6cd80fb25deb4bbcb7af37e569c404bd0c0 (diff)
continued working on api
Diffstat (limited to 'src/types.rs')
-rw-r--r--src/types.rs125
1 files changed, 109 insertions, 16 deletions
diff --git a/src/types.rs b/src/types.rs
index ed00efb..f94dd3c 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -1,4 +1,5 @@
use musli::{Allocator, Decode, Decoder, Encode, Encoder};
+use std::collections::HashMap;
use strum_macros::FromRepr;
use thiserror::Error;
@@ -6,7 +7,7 @@ use thiserror::Error;
pub enum HydrusError {
#[error("failed to connect to Hydrus")]
NetworkError(ureq::Error),
- #[error("failed to deserialize data")]
+ #[error("failed to encode/decode data")]
DeserializeError(musli::json::Error),
#[error("api or session key needed")]
KeyNotSupplied,
@@ -70,11 +71,7 @@ where
where
D: Decoder<'de>,
{
- if let Some(val) = HydrusPermissions::from_repr(decoder.decode()?) {
- return Ok(val);
- } else {
- return Ok(Self::Null);
- }
+ Ok(HydrusPermissions::from_repr(decoder.decode()?).unwrap())
}
}
@@ -147,25 +144,121 @@ where
where
D: Decoder<'de>,
{
- if let Some(val) = ServiceType::from_repr(decoder.decode()?) {
- return Ok(val);
- } else {
- return Ok(Self::Null);
- }
+ Ok(ServiceType::from_repr(decoder.decode()?).unwrap())
}
}
-#[derive(Decode)]
+#[derive(Decode, Debug)]
pub struct Service {
pub name: String,
#[musli(default)]
pub service_key: String,
- pub servicetype: ServiceType,
+ pub r#type: ServiceType,
pub type_pretty: String,
#[musli(default)]
- pub star_shape: String,
+ pub star_shape: Option<String>,
#[musli(default)]
- pub min_stars: u8,
+ pub min_stars: Option<u8>,
#[musli(default)]
- pub max_stars: u8,
+ pub max_stars: Option<u8>,
+}
+
+#[derive(Decode, Debug)]
+pub struct ServiceResponse {
+ services: HashMap<String, Service>,
+}
+
+impl ServiceResponse {
+ pub fn service_vec(mut self) -> Vec<Service> {
+ let mut res = Vec::new();
+ for (key, mut service) in self.services.drain() {
+ service.service_key = key;
+ res.push(service);
+ }
+ res
+ }
+}
+
+pub enum FileDomain {
+ FileServiceKey(String),
+ FileServiceKeys(Vec<String>),
+ DeletedFileServiceKey(String),
+ DeletedFileServiceKeys(Vec<String>),
+}
+
+#[derive(Encode, Debug, Default)]
+pub struct AddFileRequest {
+ pub path: String,
+ #[musli(skip)]
+ pub delete_after_successful_import: Option<bool>,
+ #[musli(skip)]
+ pub file_service_key: Option<String>,
+ #[musli(skip)]
+ pub file_service_keys: Option<Vec<String>>,
+ #[musli(skip)]
+ pub deleted_file_service_key: Option<String>,
+ #[musli(skip)]
+ pub deleted_file_service_keys: Option<Vec<String>>,
+}
+
+#[derive(PartialEq, Debug, Clone, FromRepr)]
+#[repr(u8)]
+pub enum FileAddStatus {
+ SuccessfulImport = 1,
+ AlreadyInDatabase,
+ PreviouslyDeleted,
+ FailedToImport,
+ FileVetoed = 7,
+}
+
+impl<M> Encode<M> for FileAddStatus {
+ type Encode = Self;
+
+ #[inline]
+ fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
+ where
+ E: Encoder<Mode = M>,
+ {
+ encoder.encode(self.clone() as u8)
+ }
+
+ #[inline]
+ fn as_encode(&self) -> &Self::Encode {
+ self
+ }
+}
+
+impl<'de, M, A> Decode<'de, M, A> for FileAddStatus
+where
+ A: Allocator,
+{
+ #[inline]
+ fn decode<D>(decoder: D) -> Result<Self, D::Error>
+ where
+ D: Decoder<'de>,
+ {
+ Ok(FileAddStatus::from_repr(decoder.decode()?).unwrap())
+ }
+}
+
+#[derive(Decode)]
+pub struct FileAddResponse {
+ pub status: FileAddStatus,
+ pub hash: String,
+ pub note: String,
+}
+
+#[derive(Encode, Debug, Default)]
+pub struct DeleteFileRequest {
+ pub path: String,
+ #[musli(skip)]
+ pub file_service_key: Option<String>,
+ #[musli(skip)]
+ pub file_service_keys: Option<Vec<String>>,
+ #[musli(skip)]
+ pub deleted_file_service_key: Option<String>,
+ #[musli(skip)]
+ pub deleted_file_service_keys: Option<Vec<String>>,
+ #[musli(skip)]
+ pub reason: String,
}