derive presentation discriminant enum

This commit is contained in:
Brent Schroeter 2025-11-11 06:42:54 +00:00
parent 275129933d
commit bb49aedbfb
2 changed files with 24 additions and 13 deletions

View file

@ -1,11 +1,22 @@
use interim_pgtypes::pg_attribute::PgAttribute; use interim_pgtypes::pg_attribute::PgAttribute;
use serde::{Deserialize, Serialize}; 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"; 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. /// 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")] #[serde(tag = "t", content = "c")]
pub enum Presentation { pub enum Presentation {
Dropdown { Dropdown {

View file

@ -1,6 +1,8 @@
use std::iter::zip; 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 serde::Deserialize;
use crate::errors::AppError; use crate::errors::AppError;
@ -28,12 +30,10 @@ impl TryFrom<PresentationForm> for Presentation {
type Error = AppError; type Error = AppError;
fn try_from(form_value: PresentationForm) -> Result<Self, Self::Error> { fn try_from(form_value: PresentationForm) -> Result<Self, Self::Error> {
// Parses the presentation tag into the correct enum variant, but without let discriminant =
// meaningful inner value(s). Match arms should all use the PresentationDiscriminants::try_from(form_value.presentation_tag.as_str())?;
// `MyVariant { .. }` pattern to pay attention to only the tag. Ok(match discriminant {
let presentation_default = Presentation::try_from(form_value.presentation_tag.as_str())?; PresentationDiscriminants::Dropdown => Presentation::Dropdown {
Ok(match presentation_default {
Presentation::Dropdown { .. } => Presentation::Dropdown {
allow_custom: false, allow_custom: false,
options: zip( options: zip(
form_value.dropdown_option_colors, form_value.dropdown_option_colors,
@ -42,11 +42,11 @@ impl TryFrom<PresentationForm> for Presentation {
.map(|(color, value)| DropdownOption { color, value }) .map(|(color, value)| DropdownOption { color, value })
.collect(), .collect(),
}, },
Presentation::Numeric { .. } => Presentation::Numeric {}, PresentationDiscriminants::Numeric => Presentation::Numeric {},
Presentation::Text { .. } => Presentation::Text { PresentationDiscriminants::Text => Presentation::Text {
input_mode: TextInputMode::try_from(form_value.text_input_mode.as_str())?, 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() { format: if form_value.timestamp_format.is_empty() {
RFC_3339_S.to_owned() RFC_3339_S.to_owned()
} else { } else {
@ -54,7 +54,7 @@ impl TryFrom<PresentationForm> for Presentation {
}, },
utc: true, utc: true,
}, },
Presentation::Uuid { .. } => Presentation::Uuid {}, PresentationDiscriminants::Uuid => Presentation::Uuid {},
}) })
} }
} }