summaryrefslogtreecommitdiff
path: root/src/types.rs
diff options
context:
space:
mode:
authorjakka <jakka@jakka.su>2025-09-08 22:57:37 +0300
committerjakka <jakka@jakka.su>2025-09-08 22:57:37 +0300
commitdfbc17db741bad16129e0bb8d21cef22e0c439f4 (patch)
tree82651c9fe894cf557de906176aadcf6c2633803d /src/types.rs
parent4c56f91c88365a3c5ad890b02e8e9f52c314ea0c (diff)
continued working on api. added proper encoding and decoding for permissions and service types
Diffstat (limited to 'src/types.rs')
-rw-r--r--src/types.rs132
1 files changed, 127 insertions, 5 deletions
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<musli::json::Error> for HydrusError {
+ fn from(value: musli::json::Error) -> Self {
+ HydrusError::DeserializeError(value)
+ }
+}
+
+impl From<ureq::Error> 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<M> Encode<M> for HydrusPermissions {
+ 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 From<u8> 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<D>(decoder: D) -> Result<Self, D::Error>
+ 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<M> Encode<M> for ServiceType {
+ 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 From<u8> 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<D>(decoder: D) -> Result<Self, D::Error>
+ where
+ D: Decoder<'de>,
+ {
+ Ok(decoder.decode()?)
+ }
+}
+
#[derive(Decode)]
pub struct Service {
name: String,