melodium_core/descriptor/
data.rs

1use core::fmt::{Debug, Display, Formatter};
2use melodium_common::{
3    descriptor::{
4        Attribuable, Attributes, Data as DataDescriptor, DataTrait, Documented, Identified,
5        Identifier,
6    },
7    executive::Value,
8};
9use serde::de::Error;
10use std::sync::Arc;
11
12pub type FnBoundedMin = Box<dyn Fn() -> melodium_common::executive::Value + Sync + Send>;
13
14pub type FnBoundedMax = Box<dyn Fn() -> melodium_common::executive::Value + Sync + Send>;
15
16pub type FnFloatInfinity = Box<dyn Fn() -> melodium_common::executive::Value + Sync + Send>;
17
18pub type FnFloatNegInfinity = Box<dyn Fn() -> melodium_common::executive::Value + Sync + Send>;
19
20pub type FnFloatNan = Box<dyn Fn() -> melodium_common::executive::Value + Sync + Send>;
21
22pub type FnDeserialize = Box<
23    dyn Fn(
24            &mut dyn erased_serde::Deserializer,
25        ) -> Result<melodium_common::executive::Value, erased_serde::Error>
26        + Sync
27        + Send,
28>;
29
30pub struct Data {
31    identifier: Identifier,
32    #[cfg(feature = "doc")]
33    documentation: String,
34    attributes: Attributes,
35    implements: Vec<DataTrait>,
36
37    // Trait no-value functions
38    bounded_min: Option<FnBoundedMin>,
39    bounded_max: Option<FnBoundedMax>,
40    float_infinity: Option<FnFloatInfinity>,
41    float_neg_infinity: Option<FnFloatNegInfinity>,
42    float_nan: Option<FnFloatNan>,
43    deserialize: Option<FnDeserialize>,
44}
45
46impl Data {
47    pub fn new(
48        identifier: Identifier,
49        documentation: String,
50        attributes: Attributes,
51        implements: Vec<DataTrait>,
52        bounded_min: Option<FnBoundedMin>,
53        bounded_max: Option<FnBoundedMax>,
54        float_infinity: Option<FnFloatInfinity>,
55        float_neg_infinity: Option<FnFloatNegInfinity>,
56        float_nan: Option<FnFloatNan>,
57        deserialize: Option<FnDeserialize>,
58    ) -> Arc<Self> {
59        #[cfg(not(feature = "doc"))]
60        let _ = documentation;
61        Arc::new(Self {
62            identifier,
63            #[cfg(feature = "doc")]
64            documentation,
65            attributes,
66            implements,
67            bounded_min,
68            bounded_max,
69            float_infinity,
70            float_neg_infinity,
71            float_nan,
72            deserialize,
73        })
74    }
75}
76
77impl Attribuable for Data {
78    fn attributes(&self) -> &Attributes {
79        &self.attributes
80    }
81}
82
83impl Identified for Data {
84    fn identifier(&self) -> &Identifier {
85        &self.identifier
86    }
87
88    fn make_use(&self, _identifier: &Identifier) -> bool {
89        false
90    }
91
92    fn uses(&self) -> Vec<Identifier> {
93        vec![]
94    }
95}
96
97impl Documented for Data {
98    fn documentation(&self) -> &str {
99        #[cfg(feature = "doc")]
100        {
101            &self.documentation
102        }
103        #[cfg(not(feature = "doc"))]
104        {
105            &""
106        }
107    }
108}
109
110impl Display for Data {
111    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
112        write!(f, "data {}", self.identifier.to_string(),)?;
113
114        Ok(())
115    }
116}
117
118impl DataDescriptor for Data {
119    fn implements(&self) -> &[DataTrait] {
120        &self.implements
121    }
122
123    fn bounded_min(&self) -> Value {
124        self.bounded_min
125            .as_ref()
126            .map(|func| func())
127            .expect(&format!("Bounded not implemeted by {}", self.identifier))
128    }
129
130    fn bounded_max(&self) -> Value {
131        self.bounded_max
132            .as_ref()
133            .map(|func| func())
134            .expect(&format!("Bounded not implemeted by {}", self.identifier))
135    }
136
137    fn float_infinity(&self) -> Value {
138        self.float_infinity
139            .as_ref()
140            .map(|func| func())
141            .expect(&format!("Float not implemeted by {}", self.identifier))
142    }
143
144    fn float_neg_infinity(&self) -> Value {
145        self.float_neg_infinity
146            .as_ref()
147            .map(|func| func())
148            .expect(&format!("Float not implemeted by {}", self.identifier))
149    }
150
151    fn float_nan(&self) -> Value {
152        self.float_nan
153            .as_ref()
154            .map(|func| func())
155            .expect(&format!("Float not implemeted by {}", self.identifier))
156    }
157
158    fn deserialize(
159        &self,
160        deserializer: &mut dyn erased_serde::Deserializer,
161    ) -> Result<Value, erased_serde::Error> {
162        self.deserialize
163            .as_ref()
164            .map(|func| func(deserializer))
165            .unwrap_or_else(|| {
166                Err(erased_serde::Error::custom(format!(
167                    "Deserialize not implemeted by {}",
168                    self.identifier
169                )))
170            })
171    }
172}
173
174impl Debug for Data {
175    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
176        f.debug_struct("Data")
177            .field("identifier", &self.identifier)
178            .field("attributes", &self.attributes)
179            .field("implements", &self.implements)
180            .field(
181                "deserialize",
182                if self.deserialize.is_some() {
183                    &"implemented"
184                } else {
185                    &"none"
186                },
187            )
188            .finish()
189    }
190}