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 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 {

View file

@ -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<PresentationForm> for Presentation {
type Error = AppError;
fn try_from(form_value: PresentationForm) -> Result<Self, Self::Error> {
// 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<PresentationForm> 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<PresentationForm> for Presentation {
},
utc: true,
},
Presentation::Uuid { .. } => Presentation::Uuid {},
PresentationDiscriminants::Uuid => Presentation::Uuid {},
})
}
}