diff --git a/interim-models/src/presentation.rs b/interim-models/src/presentation.rs index 754b166..02d5c6c 100644 --- a/interim-models/src/presentation.rs +++ b/interim-models/src/presentation.rs @@ -1,11 +1,22 @@ use interim_pgtypes::pg_attribute::PgAttribute; use serde::{Deserialize, Serialize}; -use strum::{EnumIter, EnumString}; +use strum::{EnumDiscriminants, EnumIter, EnumString}; pub const RFC_3339_S: &str = "yyyy-MM-dd'T'HH:mm:ssXXX"; /// Struct defining how a field's is displayed and how it accepts input in UI. -#[derive(Clone, Debug, Deserialize, EnumIter, EnumString, PartialEq, Serialize, strum::Display)] +#[derive( + Clone, + Debug, + Deserialize, + EnumDiscriminants, + EnumIter, + EnumString, + PartialEq, + Serialize, + strum::Display, +)] +#[strum_discriminants(derive(EnumString))] #[serde(tag = "t", content = "c")] pub enum Presentation { Dropdown { diff --git a/interim-server/src/presentation_form.rs b/interim-server/src/presentation_form.rs index 0f10aab..d4718c1 100644 --- a/interim-server/src/presentation_form.rs +++ b/interim-server/src/presentation_form.rs @@ -1,6 +1,8 @@ use std::iter::zip; -use interim_models::presentation::{DropdownOption, Presentation, RFC_3339_S, TextInputMode}; +use interim_models::presentation::{ + DropdownOption, Presentation, PresentationDiscriminants, RFC_3339_S, TextInputMode, +}; use serde::Deserialize; use crate::errors::AppError; @@ -28,12 +30,10 @@ impl TryFrom for Presentation { type Error = AppError; fn try_from(form_value: PresentationForm) -> Result { - // Parses the presentation tag into the correct enum variant, but without - // meaningful inner value(s). Match arms should all use the - // `MyVariant { .. }` pattern to pay attention to only the tag. - let presentation_default = Presentation::try_from(form_value.presentation_tag.as_str())?; - Ok(match presentation_default { - Presentation::Dropdown { .. } => Presentation::Dropdown { + let discriminant = + PresentationDiscriminants::try_from(form_value.presentation_tag.as_str())?; + Ok(match discriminant { + PresentationDiscriminants::Dropdown => Presentation::Dropdown { allow_custom: false, options: zip( form_value.dropdown_option_colors, @@ -42,11 +42,11 @@ impl TryFrom for Presentation { .map(|(color, value)| DropdownOption { color, value }) .collect(), }, - Presentation::Numeric { .. } => Presentation::Numeric {}, - Presentation::Text { .. } => Presentation::Text { + PresentationDiscriminants::Numeric => Presentation::Numeric {}, + PresentationDiscriminants::Text => Presentation::Text { input_mode: TextInputMode::try_from(form_value.text_input_mode.as_str())?, }, - Presentation::Timestamp { .. } => Presentation::Timestamp { + PresentationDiscriminants::Timestamp => Presentation::Timestamp { format: if form_value.timestamp_format.is_empty() { RFC_3339_S.to_owned() } else { @@ -54,7 +54,7 @@ impl TryFrom for Presentation { }, utc: true, }, - Presentation::Uuid { .. } => Presentation::Uuid {}, + PresentationDiscriminants::Uuid => Presentation::Uuid {}, }) } }