app: Fix attribute type parsing

This commit is contained in:
Valentin Tolmer
2025-07-30 00:56:53 +02:00
committed by nitnelave
parent 3c7e4c3dec
commit 363ef106e2
5 changed files with 42 additions and 13 deletions
Generated
+2
View File
@@ -2604,6 +2604,7 @@ dependencies = [
"anyhow", "anyhow",
"base64 0.13.1", "base64 0.13.1",
"chrono", "chrono",
"derive_more 1.0.0",
"gloo-console", "gloo-console",
"gloo-file", "gloo-file",
"gloo-net", "gloo-net",
@@ -2618,6 +2619,7 @@ dependencies = [
"rand 0.8.5", "rand 0.8.5",
"serde", "serde",
"serde_json", "serde_json",
"strum 0.25.0",
"url-escape", "url-escape",
"validator", "validator",
"validator_derive", "validator_derive",
+9
View File
@@ -55,6 +55,11 @@ features = [
"wasmbind" "wasmbind"
] ]
[dependencies.derive_more]
features = ["debug", "display", "from", "from_str"]
default-features = false
version = "1"
[dependencies.lldap_auth] [dependencies.lldap_auth]
path = "../crates/auth" path = "../crates/auth"
features = [ "opaque_client" ] features = [ "opaque_client" ]
@@ -73,6 +78,10 @@ version = "0.24"
[dependencies.serde] [dependencies.serde]
workspace = true workspace = true
[dependencies.strum]
features = ["derive"]
version = "0.25"
[dependencies.yew_form] [dependencies.yew_form]
git = "https://github.com/jfbilodeau/yew_form" git = "https://github.com/jfbilodeau/yew_form"
rev = "4b9fabffb63393ec7626a4477fd36de12a07fac9" rev = "4b9fabffb63393ec7626a4477fd36de12a07fac9"
+2 -2
View File
@@ -69,7 +69,7 @@ impl CommonComponent<CreateGroupAttributeForm> for CreateGroupAttributeForm {
); );
})?; })?;
let attribute_type = let attribute_type =
serde_json::from_str::<AttributeType>(&model.attribute_type).unwrap(); AttributeType::try_from(model.attribute_type.as_str()).unwrap();
let req = create_group_attribute::Variables { let req = create_group_attribute::Variables {
name: model.attribute_name, name: model.attribute_name,
attribute_type, attribute_type,
@@ -144,7 +144,7 @@ impl Component for CreateGroupAttributeForm {
oninput={link.callback(|_| Msg::Update)}> oninput={link.callback(|_| Msg::Update)}>
<option selected=true value="String">{"String"}</option> <option selected=true value="String">{"String"}</option>
<option value="Integer">{"Integer"}</option> <option value="Integer">{"Integer"}</option>
<option value="Jpeg">{"Jpeg"}</option> <option value="JpegPhoto">{"Jpeg"}</option>
<option value="DateTime">{"DateTime"}</option> <option value="DateTime">{"DateTime"}</option>
</Select<CreateGroupAttributeModel>> </Select<CreateGroupAttributeModel>>
<CheckBox<CreateGroupAttributeModel> <CheckBox<CreateGroupAttributeModel>
+2 -2
View File
@@ -73,7 +73,7 @@ impl CommonComponent<CreateUserAttributeForm> for CreateUserAttributeForm {
); );
})?; })?;
let attribute_type = let attribute_type =
serde_json::from_str::<AttributeType>(&model.attribute_type).unwrap(); AttributeType::try_from(model.attribute_type.as_str()).unwrap();
let req = create_user_attribute::Variables { let req = create_user_attribute::Variables {
name: model.attribute_name, name: model.attribute_name,
attribute_type, attribute_type,
@@ -146,7 +146,7 @@ impl Component for CreateUserAttributeForm {
oninput={link.callback(|_| Msg::Update)}> oninput={link.callback(|_| Msg::Update)}>
<option selected=true value="String">{"String"}</option> <option selected=true value="String">{"String"}</option>
<option value="Integer">{"Integer"}</option> <option value="Integer">{"Integer"}</option>
<option value="Jpeg">{"Jpeg"}</option> <option value="JpegPhoto">{"Jpeg"}</option>
<option value="DateTime">{"DateTime"}</option> <option value="DateTime">{"DateTime"}</option>
</Select<CreateUserAttributeModel>> </Select<CreateUserAttributeModel>>
<CheckBox<CreateUserAttributeModel> <CheckBox<CreateUserAttributeModel>
+27 -9
View File
@@ -1,24 +1,42 @@
use derive_more::Display;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt::Display; use strum::EnumString;
use validator::ValidationError; 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")] #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[strum(ascii_case_insensitive)]
pub(crate) enum AttributeType { pub(crate) enum AttributeType {
String, String,
Integer, Integer,
#[strum(serialize = "DATE_TIME", serialize = "DATETIME")]
DateTime, DateTime,
#[strum(serialize = "JPEG_PHOTO", serialize = "JPEGPHOTO")]
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> { pub fn validate_attribute_type(attribute_type: &str) -> Result<(), ValidationError> {
serde_json::from_str::<AttributeType>(attribute_type) AttributeType::try_from(attribute_type)
.map_err(|_| ValidationError::new("Invalid attribute type"))?; .map_err(|_| ValidationError::new("Invalid attribute type"))?;
Ok(()) 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);
}
}