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"),
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<u32>,
}
#[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 {

View file

@ -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<Self> {
let value = if let Ok(value) = value.extract::<u32>() {
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 {
value.extract::<String>()?
};