From faabe42aa83aeab54230fd29d8d32ca4a3148d7a Mon Sep 17 00:00:00 2001 From: Kubat <maelle.martin@proton.me> Date: Sun, 18 May 2025 07:55:35 +0200 Subject: [PATCH] API: Add the max_str_len functions for song type&origin for formating purposes --- kurisu_api/src/v2.rs | 73 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/kurisu_api/src/v2.rs b/kurisu_api/src/v2.rs index 6fd2504b..221e9b75 100644 --- a/kurisu_api/src/v2.rs +++ b/kurisu_api/src/v2.rs @@ -1,9 +1,9 @@ //! Object rules for the Kurisu's V2 API -use crate::{error::Error, SHA256}; +use crate::{SHA256, error::Error}; use derive_more::Display; use hashbrown::{HashMap, HashSet}; -use lektor_procmacros::EnumVariantCount; +use lektor_procmacros::{EnumVariantCount, EnumVariantIter}; use serde::{Deserialize, Serialize}; use std::{borrow, cmp, collections::BTreeSet, str::FromStr}; @@ -157,7 +157,17 @@ impl Infos { /// The type of a song. One the the following, one per kara. #[derive( - Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Copy, Hash, Display, EnumVariantCount, + Debug, + PartialEq, + Eq, + Serialize, + Deserialize, + Clone, + Copy, + Hash, + Display, + EnumVariantCount, + EnumVariantIter, )] #[serde(rename_all = "UPPERCASE")] #[display("{}", self.as_str())] @@ -171,7 +181,17 @@ pub enum SongType { /// The origin of a song's source. One the the following, one per kara. #[derive( - Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Copy, Hash, Display, EnumVariantCount, + Debug, + PartialEq, + Eq, + Serialize, + Deserialize, + Clone, + Copy, + Hash, + Display, + EnumVariantCount, + EnumVariantIter, )] #[serde(rename_all = "lowercase")] #[display("{}", self.as_str())] @@ -184,7 +204,7 @@ pub enum SongOrigin { } impl SongType { - pub fn as_str(&self) -> &str { + pub const fn as_str(&self) -> &str { match self { SongType::OP => "OP", SongType::ED => "ED", @@ -193,10 +213,23 @@ impl SongType { SongType::OT => "OT", } } + + pub const fn max_str_len() -> usize { + let mut i = 0_usize; + let mut max = 0_usize; + while i < SONGTYPE_LENGTH { + let variant_length = SONGTYPE_VALUES[i].as_str().len(); + if variant_length > max { + max = variant_length; + } + i += 1; + } + max + } } impl SongOrigin { - pub fn as_str(&self) -> &str { + pub const fn as_str(&self) -> &str { match self { SongOrigin::Anime => "anime", SongOrigin::VN => "vn", @@ -205,6 +238,19 @@ impl SongOrigin { SongOrigin::Other => "other", } } + + pub const fn max_str_len() -> usize { + let mut i = 0_usize; + let mut max = 0_usize; + while i < SONGORIGIN_LENGTH { + let variant_length = SONGORIGIN_VALUES[i].as_str().len(); + if variant_length > max { + max = variant_length; + } + i += 1; + } + max + } } impl FromStr for SongType { @@ -392,3 +438,18 @@ pub struct Playlist { #[serde(default)] karas: Vec<u64>, } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn song_origin_max_length() { + assert_eq!(SongOrigin::max_str_len(), 5); + } + + #[test] + fn song_type_max_length() { + assert_eq!(SongType::max_str_len(), 2); + } +} -- GitLab