Skip to main content

ez_tui/components/concrete/forms/
result.rs

1use crate::{EzCptIds, FormField};
2use chrono::{DateTime, Utc};
3
4/// All possible values types for a form field.
5#[derive(Debug)]
6pub enum FormFieldValue {
7    /// A text
8    Text(String),
9    /// A boolean
10    Bool(bool),
11    /// A number
12    Number(f64),
13    /// A range of numbers
14    Range((f64, f64)),
15    /// A date and time
16    DateTime(DateTime<Utc>),
17}
18
19/// A type wrapping a form field and its value.
20#[derive(Debug)]
21pub struct FieldResult<FID>
22where
23    FID: EzCptIds,
24{
25    form_field: FormField<FID>,
26    form_field_value: FormFieldValue,
27}
28
29/// A type wrapping all the [`FieldResult`]s of a form into a [`Vec`].
30#[derive(Debug)]
31pub struct FormResult<FID>
32where
33    FID: EzCptIds,
34{
35    results: Vec<FieldResult<FID>>,
36}
37
38impl<FID> FormResult<FID>
39where
40    FID: EzCptIds,
41{
42    /// Get the value of a specific field result by its field id.
43    pub fn get(&self, fid: &FID) -> Option<&FieldResult<FID>> {
44        self.results.iter().find(|r| r.form_field.id() == fid)
45    }
46}
47impl<FID> From<Vec<(FormField<FID>, FormFieldValue)>> for FormResult<FID>
48where
49    FID: EzCptIds,
50{
51    fn from(value: Vec<(FormField<FID>, FormFieldValue)>) -> Self {
52        Self {
53            results: value.into_iter().map(std::convert::Into::into).collect(),
54        }
55    }
56}
57impl<FID> FieldResult<FID>
58where
59    FID: EzCptIds,
60{
61    /// Try to return this field result as a text value.
62    pub fn as_text(&self) -> Option<String> {
63        if let FormFieldValue::Text(txt) = &self.form_field_value {
64            Some(txt.clone())
65        } else {
66            None
67        }
68    }
69
70    /// Try to return this field result as a boolean value.
71    pub fn as_bool(&self) -> Option<bool> {
72        if let FormFieldValue::Bool(b) = &self.form_field_value {
73            Some(*b)
74        } else {
75            None
76        }
77    }
78
79    /// Try to return this field result as a number value.
80    pub fn as_number(&self) -> Option<f64> {
81        if let FormFieldValue::Number(n) = &self.form_field_value {
82            Some(*n)
83        } else {
84            None
85        }
86    }
87    /// Try to return this field result as a range value.
88    pub fn as_range(&self) -> Option<(f64, f64)> {
89        if let FormFieldValue::Range(r) = &self.form_field_value {
90            Some(*r)
91        } else {
92            None
93        }
94    }
95    /// Try to return this field result as a date and time value.
96    pub fn as_datetime(&self) -> Option<DateTime<Utc>> {
97        if let FormFieldValue::DateTime(dt) = &self.form_field_value {
98            Some(*dt)
99        } else {
100            None
101        }
102    }
103}
104impl<FID> From<(FormField<FID>, FormFieldValue)> for FieldResult<FID>
105where
106    FID: EzCptIds,
107{
108    fn from(value: (FormField<FID>, FormFieldValue)) -> Self {
109        Self {
110            form_field: value.0,
111            form_field_value: value.1,
112        }
113    }
114}