aw_test/models/
attribute_float.rs

1#![allow(unused)]
2use serde::{Deserialize, Serialize, Deserializer};
3use std::collections::HashMap;
4use serde_json::value::Value;
5use std::fmt::Display;
6use super::*;
7
8#[derive(Debug, Serialize, Clone)]
9#[serde(deny_unknown_fields)]
10#[serde(untagged)]
11pub enum EmptyOption<T> {
12    Some(T),
13    None {},
14}
15
16impl<T> Display for EmptyOption<T>
17where
18    T: Display,
19{
20    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
21        match self {
22            EmptyOption::Some(t) => write!(f, "{}", t),
23            EmptyOption::None {} => write!(f, ""),
24        }
25    }
26}
27
28impl<'de, T> Deserialize<'de> for EmptyOption<T>
29where
30    T: Deserialize<'de>,
31{
32    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
33    where
34        D: Deserializer<'de>,
35    {
36        Option::deserialize(deserializer).map(Into::into)
37    }
38}
39
40impl<T> From<EmptyOption<T>> for Option<T> {
41    fn from(empty_option: EmptyOption<T>) -> Option<T> {
42        match empty_option {
43            EmptyOption::Some(option) => Some(option),
44            EmptyOption::None {} => None,
45        }
46    }
47}
48
49impl<T> From<Option<T>> for EmptyOption<T> {
50    fn from(option: Option<T>) -> EmptyOption<T> {
51        match option {
52            Some(option) => EmptyOption::Some(option),
53            None {} => EmptyOption::None {},
54        }
55    }
56}
57
58impl<T> EmptyOption<T> {
59    fn into_option(self) -> Option<T> {
60        self.into()
61    }
62    fn as_option(&self) -> Option<&T> {
63        match self {
64            EmptyOption::Some(option) => Some(option),
65            EmptyOption::None {} => None,
66        }
67    }
68}
69
70#[derive(Serialize, Deserialize, Debug, Clone)]
71pub struct AttributeFloat {
72        pub key: String,
73        #[serde(rename(serialize = "xtype", deserialize = "type"))]
74        pub xtype: String,
75        pub status: String,
76        pub required: bool,
77        pub array: EmptyOption<bool>,
78        pub min: EmptyOption<f64>,
79        pub max: EmptyOption<f64>,
80        pub default: EmptyOption<f64>,
81}
82
83impl Display for AttributeFloat {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        let mut formatBuffer = String::new();
86        formatBuffer.push_str(&format!("{:?}", self.key));
87        formatBuffer.push_str(&format!("{:?}", self.xtype));
88        formatBuffer.push_str(&format!("{:?}", self.status));
89        formatBuffer.push_str(&format!("{:?}", self.required));
90        formatBuffer.push_str(&format!("{:?}", self.array));
91        formatBuffer.push_str(&format!("{:?}", self.min));
92        formatBuffer.push_str(&format!("{:?}", self.max));
93        formatBuffer.push_str(&format!("{:?}", self.default));
94
95        write!(f, "{}", formatBuffer)
96    }
97}
98
99impl AttributeFloat {
100    pub fn new(key: String, xtype: String, status: String, required: bool, array: EmptyOption<bool>, min: EmptyOption<f64>, max: EmptyOption<f64>, default: EmptyOption<f64>, ) -> Self {
101        Self {
102            key: key,
103            xtype: xtype,
104            status: status,
105            required: required,
106            array: EmptyOption::from(array),
107            min: EmptyOption::from(min),
108            max: EmptyOption::from(max),
109            default: EmptyOption::from(default),
110            }
111    }
112}