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
18/// Trait used to represent that a type can represent a [value::MetricValue]
19pub trait MetricValue:
20    TryFrom<value::MetricValue> + Into<value::MetricValue> + HasDataType
21{
22    fn birth_metadata(&self) -> Option<MetaData> {
23        self.publish_metadata()
24    }
25    fn publish_metadata(&self) -> Option<MetaData> {
26        None
27    }
28}
29
30/// Trait used to represent that a type can represent a [value::PropertyValue]
31pub trait PropertyValue:
32    TryFrom<value::PropertyValue> + Into<value::PropertyValue> + HasDataType
33{
34}
35
36/// Trait used to represent that a type can represent a [value::DataSetValue]
37pub trait DataSetValue:
38    TryFrom<value::DataSetValue> + Into<value::DataSetValue> + HasDataType
39{
40}
41
42/// Trait used to represent that a type can represent a [value::ParameterValue]
43pub trait ParameterValue:
44    TryFrom<value::ParameterValue> + Into<value::ParameterValue> + HasDataType
45{
46}