From 2ac0735589abc0d4aacfab4a5197da20972bebbf Mon Sep 17 00:00:00 2001 From: jakka Date: Sat, 13 Sep 2025 22:40:54 +0300 Subject: continued working on api --- src/types.rs | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 109 insertions(+), 16 deletions(-) (limited to 'src/types.rs') 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, #[musli(default)] - pub min_stars: u8, + pub min_stars: Option, #[musli(default)] - pub max_stars: u8, + pub max_stars: Option, +} + +#[derive(Decode, Debug)] +pub struct ServiceResponse { + services: HashMap, +} + +impl ServiceResponse { + pub fn service_vec(mut self) -> Vec { + 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), + DeletedFileServiceKey(String), + DeletedFileServiceKeys(Vec), +} + +#[derive(Encode, Debug, Default)] +pub struct AddFileRequest { + pub path: String, + #[musli(skip)] + pub delete_after_successful_import: Option, + #[musli(skip)] + pub file_service_key: Option, + #[musli(skip)] + pub file_service_keys: Option>, + #[musli(skip)] + pub deleted_file_service_key: Option, + #[musli(skip)] + pub deleted_file_service_keys: Option>, +} + +#[derive(PartialEq, Debug, Clone, FromRepr)] +#[repr(u8)] +pub enum FileAddStatus { + SuccessfulImport = 1, + AlreadyInDatabase, + PreviouslyDeleted, + FailedToImport, + FileVetoed = 7, +} + +impl Encode for FileAddStatus { + type Encode = Self; + + #[inline] + fn encode(&self, encoder: E) -> Result<(), E::Error> + where + E: Encoder, + { + 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(decoder: D) -> Result + 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, + #[musli(skip)] + pub file_service_keys: Option>, + #[musli(skip)] + pub deleted_file_service_key: Option, + #[musli(skip)] + pub deleted_file_service_keys: Option>, + #[musli(skip)] + pub reason: String, } -- cgit v1.3.1