fix(tokens): fixing typesafe named tokens

This commit is contained in:
Scott Lyons 2024-09-06 09:54:27 -07:00
commit 4485e6b05e
2 changed files with 21 additions and 4 deletions

View file

@ -326,6 +326,7 @@ impl TagSibling {
all(feature = "python"), all(feature = "python"),
pyclass(eq, eq_int, module = "szurubooru_client.models") pyclass(eq, eq_int, module = "szurubooru_client.models")
)] )]
#[strum(serialize_all = "camelCase")]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
/// The type of post /// The type of post
pub enum PostType { pub enum PostType {
@ -352,6 +353,7 @@ pub enum PostType {
all(feature = "python"), all(feature = "python"),
pyclass(eq, eq_int, module = "szurubooru_client.models") pyclass(eq, eq_int, module = "szurubooru_client.models")
)] )]
#[strum(serialize_all = "camelCase")]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
/// How SFW/NSFW the post is /// How SFW/NSFW the post is
pub enum PostSafety { 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( #[cfg_attr(
all(feature = "python"), all(feature = "python"),
pyclass(eq, eq_int, module = "szurubooru_client.models") pyclass(eq, eq_int, module = "szurubooru_client.models")
)] )]
#[strum(serialize_all = "camelCase")]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
/// The Rank of a given User /// The Rank of a given User
pub enum UserRank { pub enum UserRank {
@ -668,11 +671,12 @@ pub enum UserRank {
Administrator, Administrator,
} }
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, AsRefStr, Eq, PartialEq)]
#[cfg_attr( #[cfg_attr(
all(feature = "python"), all(feature = "python"),
pyclass(eq, eq_int, module = "szurubooru_client.models") pyclass(eq, eq_int, module = "szurubooru_client.models")
)] )]
#[strum(serialize_all = "camelCase")]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
/// The kind of User Avatar /// The kind of User Avatar
pub enum UserAvatarStyle { pub enum UserAvatarStyle {
@ -1358,11 +1362,12 @@ pub struct CreateUpdateComment {
pub post_id: Option<u32>, pub post_id: Option<u32>,
} }
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, AsRefStr, Eq, PartialEq)]
#[cfg_attr( #[cfg_attr(
all(feature = "python"), all(feature = "python"),
pyclass(eq, eq_int, module = "szurubooru_client.models") pyclass(eq, eq_int, module = "szurubooru_client.models")
)] )]
#[strum(serialize_all = "camelCase")]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
/// The kind of snapshot that has been recorded /// The kind of snapshot that has been recorded
pub enum SnapshotOperationType { pub enum SnapshotOperationType {
@ -1376,11 +1381,12 @@ pub enum SnapshotOperationType {
Merged, Merged,
} }
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, AsRefStr, Eq, PartialEq)]
#[cfg_attr( #[cfg_attr(
all(feature = "python"), all(feature = "python"),
pyclass(eq, eq_int, module = "szurubooru_client.models") pyclass(eq, eq_int, module = "szurubooru_client.models")
)] )]
#[strum(serialize_all = "camelCase")]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
/// The kind of resource described by this snapshot /// The kind of resource described by this snapshot
pub enum SnapshotResourceType { pub enum SnapshotResourceType {

View file

@ -2,6 +2,7 @@
//! warned that the types here help with the Type safety for the Tag names only. It does //! 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. //! not guarantee that a given API endpoint will support the given tag.
use crate::models::{PostSafety, PostType, SnapshotOperationType, SnapshotResourceType, UserRank};
#[cfg(feature = "python")] #[cfg(feature = "python")]
use pyo3::{exceptions::PyValueError, prelude::*}; use pyo3::{exceptions::PyValueError, prelude::*};
use std::fmt::Display; use std::fmt::Display;
@ -269,6 +270,16 @@ impl QueryToken {
pub fn token_py(key: &Bound<'_, PyAny>, value: &Bound<'_, PyAny>) -> PyResult<Self> { pub fn token_py(key: &Bound<'_, PyAny>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
let value = if let Ok(value) = value.extract::<u32>() { let value = if let Ok(value) = value.extract::<u32>() {
value.to_string() value.to_string()
} else if let Ok(tv) = value.extract::<PostSafety>() {
tv.as_ref().to_string()
} else if let Ok(tv) = value.extract::<PostType>() {
tv.as_ref().to_string()
} else if let Ok(tv) = value.extract::<UserRank>() {
tv.as_ref().to_string()
} else if let Ok(tv) = value.extract::<SnapshotOperationType>() {
tv.as_ref().to_string()
} else if let Ok(tv) = value.extract::<SnapshotResourceType>() {
tv.as_ref().to_string()
} else { } else {
value.extract::<String>()? value.extract::<String>()?
}; };