summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock23
-rw-r--r--Cargo.toml1
-rw-r--r--src/types.rs71
3 files changed, 37 insertions, 58 deletions
diff --git a/Cargo.lock b/Cargo.lock
index c449b96..34d785e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -102,6 +102,12 @@ dependencies = [
]
[[package]]
+name = "heck"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
+
+[[package]]
name = "http"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -123,6 +129,7 @@ name = "hydrus-api-rs"
version = "0.1.0"
dependencies = [
"musli",
+ "strum_macros",
"thiserror",
"ureq",
"urlencoding",
@@ -409,6 +416,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
[[package]]
+name = "strum_macros"
+version = "0.27.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7695ce3845ea4b33927c055a39dc438a45b059f7c1b3d91d38d10355fb8cbca7"
+dependencies = [
+ "heck",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -505,9 +524,9 @@ dependencies = [
[[package]]
name = "unicode-ident"
-version = "1.0.18"
+version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
+checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d"
[[package]]
name = "untrusted"
diff --git a/Cargo.toml b/Cargo.toml
index 18d1186..c9ba256 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,6 +5,7 @@ edition = "2024"
[dependencies]
musli = { version = "0.0.145", features = ["json"] }
+strum_macros = "0.27.2"
thiserror = "2.0.16"
ureq = "3.1.0"
urlencoding = "2.1.3"
diff --git a/src/types.rs b/src/types.rs
index 5472906..ed00efb 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -1,5 +1,5 @@
-use core::panic;
use musli::{Allocator, Decode, Decoder, Encode, Encoder};
+use strum_macros::FromRepr;
use thiserror::Error;
#[derive(Error, Debug)]
@@ -24,7 +24,7 @@ impl From<ureq::Error> for HydrusError {
}
}
-#[derive(PartialEq, Debug, Clone)]
+#[derive(PartialEq, Debug, Clone, FromRepr)]
#[repr(u8)]
pub enum HydrusPermissions {
ImportAndEditURLs = 0,
@@ -41,6 +41,7 @@ pub enum HydrusPermissions {
EditFileTimes,
CommitPending,
SeeLocalPaths,
+ Null = 255,
}
impl<M> Encode<M> for HydrusPermissions {
@@ -60,28 +61,6 @@ impl<M> Encode<M> for HydrusPermissions {
}
}
-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,
@@ -91,8 +70,11 @@ where
where
D: Decoder<'de>,
{
- let val: u8 = decoder.decode()?;
- Ok(val.into())
+ if let Some(val) = HydrusPermissions::from_repr(decoder.decode()?) {
+ return Ok(val);
+ } else {
+ return Ok(Self::Null);
+ }
}
}
@@ -114,7 +96,7 @@ pub struct KeyInfo {
pub human_permissions: String,
}
-#[derive(PartialEq, Debug, Clone)]
+#[derive(PartialEq, Debug, Clone, FromRepr)]
#[repr(u8)]
pub enum ServiceType {
TagRepository = 0,
@@ -136,6 +118,7 @@ pub enum ServiceType {
AllMyFiles,
IncDecRating,
ServerAdmin = 99,
+ Null = 255,
}
impl<M> Encode<M> for ServiceType {
@@ -155,33 +138,6 @@ impl<M> Encode<M> for ServiceType {
}
}
-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,
@@ -191,8 +147,11 @@ where
where
D: Decoder<'de>,
{
- let val: u8 = decoder.decode()?;
- Ok(val.into())
+ if let Some(val) = ServiceType::from_repr(decoder.decode()?) {
+ return Ok(val);
+ } else {
+ return Ok(Self::Null);
+ }
}
}