From 4485e6b05eea60dec27a337624fb7d0dff75b32f Mon Sep 17 00:00:00 2001 From: Scott Lyons Date: Fri, 6 Sep 2024 09:54:27 -0700 Subject: [PATCH] fix(tokens): fixing typesafe named tokens --- szurubooru-client/src/models.rs | 14 ++++++++++---- szurubooru-client/src/tokens.rs | 11 +++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/szurubooru-client/src/models.rs b/szurubooru-client/src/models.rs index a89013e..0a295e9 100644 --- a/szurubooru-client/src/models.rs +++ b/szurubooru-client/src/models.rs @@ -326,6 +326,7 @@ impl TagSibling { all(feature = "python"), pyclass(eq, eq_int, module = "szurubooru_client.models") )] +#[strum(serialize_all = "camelCase")] #[serde(rename_all = "camelCase")] /// The type of post pub enum PostType { @@ -352,6 +353,7 @@ pub enum PostType { all(feature = "python"), pyclass(eq, eq_int, module = "szurubooru_client.models") )] +#[strum(serialize_all = "camelCase")] #[serde(rename_all = "camelCase")] /// How SFW/NSFW the post is pub enum PostSafety { @@ -648,11 +650,12 @@ impl NoteResource { } } -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)] +#[derive(Debug, Clone, Serialize, Deserialize, AsRefStr, Eq, PartialEq)] #[cfg_attr( all(feature = "python"), pyclass(eq, eq_int, module = "szurubooru_client.models") )] +#[strum(serialize_all = "camelCase")] #[serde(rename_all = "camelCase")] /// The Rank of a given User pub enum UserRank { @@ -668,11 +671,12 @@ pub enum UserRank { Administrator, } -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)] +#[derive(Debug, Clone, Serialize, Deserialize, AsRefStr, Eq, PartialEq)] #[cfg_attr( all(feature = "python"), pyclass(eq, eq_int, module = "szurubooru_client.models") )] +#[strum(serialize_all = "camelCase")] #[serde(rename_all = "camelCase")] /// The kind of User Avatar pub enum UserAvatarStyle { @@ -1358,11 +1362,12 @@ pub struct CreateUpdateComment { pub post_id: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)] +#[derive(Debug, Clone, Serialize, Deserialize, AsRefStr, Eq, PartialEq)] #[cfg_attr( all(feature = "python"), pyclass(eq, eq_int, module = "szurubooru_client.models") )] +#[strum(serialize_all = "camelCase")] #[serde(rename_all = "camelCase")] /// The kind of snapshot that has been recorded pub enum SnapshotOperationType { @@ -1376,11 +1381,12 @@ pub enum SnapshotOperationType { Merged, } -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)] +#[derive(Debug, Clone, Serialize, Deserialize, AsRefStr, Eq, PartialEq)] #[cfg_attr( all(feature = "python"), pyclass(eq, eq_int, module = "szurubooru_client.models") )] +#[strum(serialize_all = "camelCase")] #[serde(rename_all = "camelCase")] /// The kind of resource described by this snapshot pub enum SnapshotResourceType { diff --git a/szurubooru-client/src/tokens.rs b/szurubooru-client/src/tokens.rs index 35c6b23..ebcab6f 100644 --- a/szurubooru-client/src/tokens.rs +++ b/szurubooru-client/src/tokens.rs @@ -2,6 +2,7 @@ //! warned that the types here help with the Type safety for the Tag names only. It does //! not guarantee that a given API endpoint will support the given tag. +use crate::models::{PostSafety, PostType, SnapshotOperationType, SnapshotResourceType, UserRank}; #[cfg(feature = "python")] use pyo3::{exceptions::PyValueError, prelude::*}; use std::fmt::Display; @@ -269,6 +270,16 @@ impl QueryToken { pub fn token_py(key: &Bound<'_, PyAny>, value: &Bound<'_, PyAny>) -> PyResult { let value = if let Ok(value) = value.extract::() { value.to_string() + } else if let Ok(tv) = value.extract::() { + tv.as_ref().to_string() + } else if let Ok(tv) = value.extract::() { + tv.as_ref().to_string() + } else if let Ok(tv) = value.extract::() { + tv.as_ref().to_string() + } else if let Ok(tv) = value.extract::() { + tv.as_ref().to_string() + } else if let Ok(tv) = value.extract::() { + tv.as_ref().to_string() } else { value.extract::()? };