diff --git a/Cargo.lock b/Cargo.lock index f866326..a694a0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2604,6 +2604,7 @@ dependencies = [ "anyhow", "base64 0.13.1", "chrono", + "derive_more 1.0.0", "gloo-console", "gloo-file", "gloo-net", @@ -2618,6 +2619,7 @@ dependencies = [ "rand 0.8.5", "serde", "serde_json", + "strum 0.25.0", "url-escape", "validator", "validator_derive", diff --git a/app/Cargo.toml b/app/Cargo.toml index a9b9d9d..a247cfd 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -55,6 +55,11 @@ features = [ "wasmbind" ] +[dependencies.derive_more] +features = ["debug", "display", "from", "from_str"] +default-features = false +version = "1" + [dependencies.lldap_auth] path = "../crates/auth" features = [ "opaque_client" ] @@ -73,6 +78,10 @@ version = "0.24" [dependencies.serde] workspace = true +[dependencies.strum] +features = ["derive"] +version = "0.25" + [dependencies.yew_form] git = "https://github.com/jfbilodeau/yew_form" rev = "4b9fabffb63393ec7626a4477fd36de12a07fac9" diff --git a/app/src/components/create_group_attribute.rs b/app/src/components/create_group_attribute.rs index e1832ab..6feae78 100644 --- a/app/src/components/create_group_attribute.rs +++ b/app/src/components/create_group_attribute.rs @@ -69,7 +69,7 @@ impl CommonComponent for CreateGroupAttributeForm { ); })?; let attribute_type = - serde_json::from_str::(&model.attribute_type).unwrap(); + AttributeType::try_from(model.attribute_type.as_str()).unwrap(); let req = create_group_attribute::Variables { name: model.attribute_name, attribute_type, @@ -144,7 +144,7 @@ impl Component for CreateGroupAttributeForm { oninput={link.callback(|_| Msg::Update)}> - + > diff --git a/app/src/components/create_user_attribute.rs b/app/src/components/create_user_attribute.rs index 831ace5..8195d78 100644 --- a/app/src/components/create_user_attribute.rs +++ b/app/src/components/create_user_attribute.rs @@ -73,7 +73,7 @@ impl CommonComponent for CreateUserAttributeForm { ); })?; let attribute_type = - serde_json::from_str::(&model.attribute_type).unwrap(); + AttributeType::try_from(model.attribute_type.as_str()).unwrap(); let req = create_user_attribute::Variables { name: model.attribute_name, attribute_type, @@ -146,7 +146,7 @@ impl Component for CreateUserAttributeForm { oninput={link.callback(|_| Msg::Update)}> - + > diff --git a/app/src/infra/schema.rs b/app/src/infra/schema.rs index ed73e87..c53c66a 100644 --- a/app/src/infra/schema.rs +++ b/app/src/infra/schema.rs @@ -1,24 +1,42 @@ +use derive_more::Display; use serde::{Deserialize, Serialize}; -use std::fmt::Display; +use strum::EnumString; use validator::ValidationError; -#[derive(Deserialize, Serialize, Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash, EnumString, Display)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] +#[strum(ascii_case_insensitive)] pub(crate) enum AttributeType { String, Integer, + #[strum(serialize = "DATE_TIME", serialize = "DATETIME")] DateTime, + #[strum(serialize = "JPEG_PHOTO", serialize = "JPEGPHOTO")] JpegPhoto, } -impl Display for AttributeType { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", self) - } -} - pub fn validate_attribute_type(attribute_type: &str) -> Result<(), ValidationError> { - serde_json::from_str::(attribute_type) + AttributeType::try_from(attribute_type) .map_err(|_| ValidationError::new("Invalid attribute type"))?; Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_deserialize_attribute_type() { + let attr_type: AttributeType = "STRING".try_into().unwrap(); + assert_eq!(attr_type, AttributeType::String); + + let attr_type: AttributeType = "Integer".try_into().unwrap(); + assert_eq!(attr_type, AttributeType::Integer); + + let attr_type: AttributeType = "DATE_TIME".try_into().unwrap(); + assert_eq!(attr_type, AttributeType::DateTime); + + let attr_type: AttributeType = "JpegPhoto".try_into().unwrap(); + assert_eq!(attr_type, AttributeType::JpegPhoto); + } +}