From dfbc17db741bad16129e0bb8d21cef22e0c439f4 Mon Sep 17 00:00:00 2001 From: jakka Date: Mon, 8 Sep 2025 22:57:37 +0300 Subject: continued working on api. added proper encoding and decoding for permissions and service types --- src/types.rs | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 127 insertions(+), 5 deletions(-) (limited to 'src/types.rs') diff --git a/src/types.rs b/src/types.rs index d13a8aa..c34e56f 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,5 @@ -use musli::{Decode, Encode}; +use core::panic; +use musli::{Allocator, Decode, Decoder, Encode, Encoder}; use thiserror::Error; #[derive(Error, Debug)] @@ -6,13 +7,24 @@ pub enum HydrusError { #[error("failed to connect to Hydrus")] NetworkError(ureq::Error), #[error("failed to deserialize data")] - DeserializeError(musli::Error), + DeserializeError(musli::json::Error), } -impl Into +impl From for HydrusError { + fn from(value: musli::json::Error) -> Self { + HydrusError::DeserializeError(value) + } +} + +impl From for HydrusError { + fn from(value: ureq::Error) -> Self { + HydrusError::NetworkError(value) + } +} -#[derive(Decode, Encode, PartialEq, Debug)] +#[derive(PartialEq, Debug, Clone)] #[repr(u8)] + pub enum HydrusPermissions { ImportAndEditURLs = 0, ImportAndEditFiles, @@ -30,6 +42,59 @@ pub enum HydrusPermissions { SeeLocalPaths, } +impl Encode for HydrusPermissions { + 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 From for HydrusPermissions { + fn from(value: u8) -> Self { + match value { + 0 => HydrusPermissions::ImportAndEditURLs, + 1 => HydrusPermissions::ImportAndEditFiles, + 2 => HydrusPermissions::EditFileTags, + 3 => HydrusPermissions::SearchAndFetchFiles, + 4 => HydrusPermissions::ManagePages, + 5 => HydrusPermissions::ManageCookiesAndHeaders, + 6 => HydrusPermissions::ManageDatabase, + 7 => HydrusPermissions::EditFileNotes, + 8 => HydrusPermissions::EditFileRelationships, + 9 => HydrusPermissions::EditFileRatings, + 10 => HydrusPermissions::ManagePopups, + 11 => HydrusPermissions::EditFileTimes, + 12 => HydrusPermissions::CommitPending, + 13 => HydrusPermissions::SeeLocalPaths, + _ => panic!("incorrect permission id"), + } + } +} + +impl<'de, M, A> Decode<'de, M, A> for HydrusPermissions +where + A: Allocator, +{ + #[inline] + fn decode(decoder: D) -> Result + where + D: Decoder<'de>, + { + let val: u8 = decoder.decode()?; + Ok(val.into()) + } +} + #[derive(Decode)] struct AccessKey { access_key: String, @@ -48,7 +113,7 @@ pub struct KeyInfo { human_permissions: String, } -#[derive(Decode, Encode, PartialEq, Debug)] +#[derive(PartialEq, Debug, Clone)] #[repr(u8)] pub enum ServiceType { TagRepository = 0, @@ -72,6 +137,63 @@ pub enum ServiceType { ServerAdmin = 99, } +impl Encode for ServiceType { + 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 From for ServiceType { + fn from(value: u8) -> Self { + match value { + 0 => ServiceType::TagRepository, + 1 => ServiceType::FileRepository, + 2 => ServiceType::LocalFileDomain, + 5 => ServiceType::LocalTagDomain, + 6 => ServiceType::NumericalRating, + 7 => ServiceType::BoolRating, + 10 => ServiceType::AllKnownTags, + 11 => ServiceType::AllKnownFiles, + 12 => ServiceType::LocalBooru, + 13 => ServiceType::IPFS, + 14 => ServiceType::Trash, + 15 => ServiceType::AllLocalFiles, + 17 => ServiceType::FileNotes, + 18 => ServiceType::ClientAPI, + 19 => ServiceType::DeletedFromAnywhere, + 20 => ServiceType::LocalUpdates, + 21 => ServiceType::AllMyFiles, + 22 => ServiceType::IncDecRating, + 99 => ServiceType::ServerAdmin, + _ => panic!("incorrect service id"), + } + } +} + +impl<'de, M, A> Decode<'de, M, A> for ServiceType +where + A: Allocator, +{ + #[inline] + fn decode(decoder: D) -> Result + where + D: Decoder<'de>, + { + Ok(decoder.decode()?) + } +} + #[derive(Decode)] pub struct Service { name: String, -- cgit v1.3.1