srad_types/
traits.rs

1use crate::{metadata::MetaData, payload::DataType, value};
2
3/// Trait used to query the Sparkplug datatype(s) that an implementing type supports
4pub trait HasDataType {
5    /// Get all the Sparkplug [crate::payload::DataType]'s the type supports
6    fn supported_datatypes() -> &'static [DataType];
7
8    /// Default [crate::payload::DataType] the type maps to
9    fn default_datatype() -> DataType {
10        let supported = Self::supported_datatypes();
11        if supported.is_empty() {
12            panic!("supported_datatypes result has to contain at least one element")
13        }
14        supported[0]
15    }
16}
17
18impl<T> HasDataType for Option<T>
19where
20    T: HasDataType,
21{
22    fn supported_datatypes() -> &'static [DataType] {
23        T::supported_datatypes()
24    }
25
26    fn default_datatype() -> DataType {
27        T::default_datatype()
28    }
29}
30
31/// Trait used to represent that a type can represent a [value::MetricValue]
32pub trait MetricValue:
33    TryFrom<value::MetricValue> + Into<value::MetricValue> + HasDataType
34{
35    fn birth_metadata(&self) -> Option<MetaData> {
36        self.publish_metadata()
37    }
38
39    fn publish_metadata(&self) -> Option<MetaData> {
40        None
41    }
42}
43
44/// Trait used to represent that a type can represent a [value::PropertyValue]
45pub trait PropertyValue:
46    TryFrom<value::PropertyValue> + Into<value::PropertyValue> + HasDataType
47{
48}
49
50/// Trait used to represent that a type can represent a [value::DataSetValue]
51pub trait DataSetValue:
52    TryFrom<value::DataSetValue> + Into<value::DataSetValue> + HasDataType
53{
54}
55
56/// Trait used to represent that a type can represent a [value::ParameterValue]
57pub trait ParameterValue:
58    TryFrom<value::ParameterValue> + Into<value::ParameterValue> + HasDataType
59{
60}