character type

This commit is contained in:
Brandon Thomas committed 2026-04-02 19:38:10 -04:00
1 parent 02cab25242
commit a389cc31cf
5 files changed
+118

No files matched your search

@@ -3,6 +3,7 @@ use log::info;
use sqlx::{Executor, MySql};
use std::marker::PhantomData;
use enums::by_table::characters::character_type::CharacterType;
use tokens::tokens::characters::CharacterToken;
use tokens::tokens::generic_inference_jobs::InferenceJobToken;
use tokens::tokens::media_files::MediaFileToken;
@@ -12,6 +13,9 @@ use tokens::tokens::users::UserToken;
pub struct CreatePendingCharacterArgs<'e, 'c, E>
where E: 'e + Executor<'c, Database = MySql>
{
/// The type of character (e.g. KinoviSeedance).
pub character_type: CharacterType,
/// User's display name for the character (truncated to 255 chars by caller).
pub name: &'e str,
@@ -56,6 +60,7 @@ pub async fn create_pending_character<'e, 'c: 'e, E>(
INSERT INTO characters
SET
token = ?,
character_type = ?,
is_active = false,
name = ?,
maybe_description = ?,
@@ -70,6 +75,7 @@ SET
maybe_generic_inference_job_token = ?
"#,
token.as_str(),
args.character_type.to_str(),
args.name,
args.maybe_description,
args.maybe_original_upload_media_token.map(|t| t.as_str()),
@@ -0,0 +1,108 @@
use std::collections::BTreeSet;
#[cfg(test)]
use strum::EnumCount;
#[cfg(test)]
use strum::EnumIter;
/// Used in the `characters` table in `VARCHAR(24)` field `character_type`.
///
/// Identifies the type/backend used to create the character.
///
/// YOU CAN ADD NEW VALUES, BUT DO NOT CHANGE EXISTING VALUES WITHOUT A MIGRATION STRATEGY.
#[cfg_attr(test, derive(EnumIter, EnumCount))]
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Deserialize, Serialize)]
pub enum CharacterType {
/// Character created via Kinovi / Seedance2Pro.
#[serde(rename = "kinovi_seedance")]
KinoviSeedance,
}
impl_enum_display_and_debug_using_to_str!(CharacterType);
impl_mysql_enum_coders!(CharacterType);
impl CharacterType {
pub fn to_str(&self) -> &'static str {
match self {
Self::KinoviSeedance => "kinovi_seedance",
}
}
pub fn from_str(value: &str) -> Result<Self, String> {
match value {
"kinovi_seedance" => Ok(Self::KinoviSeedance),
_ => Err(format!("invalid value: {:?}", value)),
}
}
pub fn all_variants() -> BTreeSet<Self> {
BTreeSet::from([
Self::KinoviSeedance,
])
}
}
#[cfg(test)]
mod tests {
use crate::by_table::characters::character_type::CharacterType;
use crate::test_helpers::assert_serialization;
mod explicit_checks {
use super::*;
#[test]
fn test_serialization() {
assert_serialization(CharacterType::KinoviSeedance, "kinovi_seedance");
}
#[test]
fn to_str() {
assert_eq!(CharacterType::KinoviSeedance.to_str(), "kinovi_seedance");
}
#[test]
fn from_str() {
assert_eq!(CharacterType::from_str("kinovi_seedance").unwrap(), CharacterType::KinoviSeedance);
assert!(CharacterType::from_str("invalid").is_err());
}
#[test]
fn all_variants() {
const EXPECTED_COUNT: usize = 1;
assert_eq!(CharacterType::all_variants().len(), EXPECTED_COUNT);
use strum::IntoEnumIterator;
assert_eq!(CharacterType::all_variants().len(), CharacterType::iter().len());
}
}
mod mechanical_checks {
use super::*;
#[test]
fn variant_length() {
use strum::IntoEnumIterator;
assert_eq!(CharacterType::all_variants().len(), CharacterType::iter().len());
}
#[test]
fn round_trip() {
for variant in CharacterType::all_variants() {
assert_eq!(variant, CharacterType::from_str(variant.to_str()).unwrap());
assert_eq!(variant, CharacterType::from_str(&format!("{}", variant)).unwrap());
assert_eq!(variant, CharacterType::from_str(&format!("{:?}", variant)).unwrap());
}
}
#[test]
fn serialized_length_ok_for_database() {
const MAX_LENGTH: usize = 24;
for variant in CharacterType::all_variants() {
let serialized = variant.to_str();
assert!(!serialized.is_empty(), "variant {:?} is too short", variant);
assert!(serialized.len() <= MAX_LENGTH, "variant {:?} is too long for VARCHAR({})", variant, MAX_LENGTH);
}
}
}
}
@@ -0,0 +1 @@
pub mod character_type;
@@ -1,6 +1,7 @@
// ===== MySql =====
pub mod audit_logs;
pub mod batch_generations;
pub mod characters;
pub mod beta_keys;
pub mod comments;
pub mod email_sender_jobs;
@@ -7,6 +7,7 @@ use log::{error, info, warn};
use sqlx::Acquire;
use artcraft_api_defs::characters::create_character::{CreateCharacterRequest, CreateCharacterResponse};
use enums::by_table::characters::character_type::CharacterType;
use enums::common::visibility::Visibility;
use http_server_common::request::get_request_ip::get_request_ip;
use mysql_queries::queries::characters::create_pending_character::{create_pending_character, CreatePendingCharacterArgs};
@@ -158,6 +159,7 @@ pub async fn create_character_handler(
})?;
let character_token = create_pending_character(CreatePendingCharacterArgs {
character_type: CharacterType::KinoviSeedance,
name: &name,
maybe_description: description.as_deref(),
maybe_original_upload_media_token: Some(&request.image_media_token),