srad_types/
payload.rs

1use serde::{Deserialize, Serialize};
2
3pub use crate::generated::sparkplug_payload::{payload::*, *};
4
5pub use prost::Message;
6
7impl Metric {
8    pub fn new() -> Self {
9        Self {
10            name: None,
11            alias: None,
12            timestamp: None,
13            datatype: None,
14            is_historical: None,
15            is_transient: None,
16            is_null: None,
17            metadata: None,
18            properties: None,
19            value: None,
20        }
21    }
22
23    pub fn set_name(&mut self, name: String) -> &mut Self {
24        self.name = Some(name);
25        self
26    }
27
28    pub fn set_alias(&mut self, alias: u64) -> &mut Self {
29        self.alias = Some(alias);
30        self
31    }
32
33    pub fn set_datatype(&mut self, datatype: DataType) -> &mut Self {
34        self.datatype = Some(datatype as u32);
35        self
36    }
37
38    pub fn set_timestamp(&mut self, timestamp: u64) -> &mut Self {
39        self.timestamp = Some(timestamp);
40        self
41    }
42
43    pub fn set_value(&mut self, value: metric::Value) -> &mut Self {
44        self.value = Some(value);
45        self.is_null = None;
46        self
47    }
48
49    pub fn set_null(&mut self) -> &mut Self {
50        self.value = None;
51        self.is_null = Some(true);
52        self
53    }
54}
55
56impl From<Payload> for Vec<u8> {
57    fn from(value: Payload) -> Self {
58        let mut bytes = Vec::new();
59        value.encode(&mut bytes).unwrap();
60        bytes
61    }
62}
63
64impl TryFrom<u32> for DataType {
65    type Error = ();
66
67    fn try_from(v: u32) -> Result<Self, Self::Error> {
68        match v {
69            x if x == DataType::Unknown as u32 => Ok(DataType::Unknown),
70            x if x == DataType::Int8 as u32 => Ok(DataType::Int8),
71            x if x == DataType::Int16 as u32 => Ok(DataType::Int16),
72            x if x == DataType::Int32 as u32 => Ok(DataType::Int32),
73            x if x == DataType::Int64 as u32 => Ok(DataType::Int64),
74            x if x == DataType::UInt8 as u32 => Ok(DataType::UInt8),
75            x if x == DataType::UInt16 as u32 => Ok(DataType::UInt16),
76            x if x == DataType::UInt32 as u32 => Ok(DataType::UInt32),
77            x if x == DataType::UInt64 as u32 => Ok(DataType::UInt64),
78            x if x == DataType::Float as u32 => Ok(DataType::Float),
79            x if x == DataType::Double as u32 => Ok(DataType::Double),
80            x if x == DataType::Boolean as u32 => Ok(DataType::Boolean),
81            x if x == DataType::String as u32 => Ok(DataType::String),
82            x if x == DataType::DateTime as u32 => Ok(DataType::DateTime),
83            x if x == DataType::Text as u32 => Ok(DataType::Text),
84            x if x == DataType::Uuid as u32 => Ok(DataType::Uuid),
85            x if x == DataType::DataSet as u32 => Ok(DataType::DataSet),
86            x if x == DataType::Bytes as u32 => Ok(DataType::Bytes),
87            x if x == DataType::File as u32 => Ok(DataType::File),
88            x if x == DataType::Template as u32 => Ok(DataType::Template),
89            x if x == DataType::PropertySet as u32 => Ok(DataType::PropertySet),
90            x if x == DataType::PropertySetList as u32 => Ok(DataType::PropertySetList),
91            x if x == DataType::Int8Array as u32 => Ok(DataType::Int8Array),
92            x if x == DataType::Int16Array as u32 => Ok(DataType::Int16Array),
93            x if x == DataType::Int32Array as u32 => Ok(DataType::Int32Array),
94            x if x == DataType::Int64Array as u32 => Ok(DataType::Int64Array),
95            x if x == DataType::UInt8Array as u32 => Ok(DataType::UInt8Array),
96            x if x == DataType::UInt16Array as u32 => Ok(DataType::UInt16Array),
97            x if x == DataType::UInt32Array as u32 => Ok(DataType::UInt32Array),
98            x if x == DataType::UInt64Array as u32 => Ok(DataType::UInt64Array),
99            x if x == DataType::FloatArray as u32 => Ok(DataType::FloatArray),
100            x if x == DataType::DoubleArray as u32 => Ok(DataType::DoubleArray),
101            x if x == DataType::BooleanArray as u32 => Ok(DataType::BooleanArray),
102            x if x == DataType::StringArray as u32 => Ok(DataType::StringArray),
103            x if x == DataType::DateTimeArray as u32 => Ok(DataType::DateTimeArray),
104            _ => Err(()),
105        }
106    }
107}
108
109#[derive(Serialize, Deserialize)]
110pub struct StateBirthDeathCertificate {
111    pub timestamp: u64,
112    pub online: bool,
113}
114
115impl TryFrom<StateBirthDeathCertificate> for Vec<u8> {
116    type Error = String;
117    fn try_from(value: StateBirthDeathCertificate) -> Result<Self, Self::Error> {
118        match serde_json::to_vec(&value) {
119            Ok(v) => Ok(v),
120            Err(e) => Err(e.to_string()),
121        }
122    }
123}
124
125impl TryFrom<&[u8]> for StateBirthDeathCertificate {
126    type Error = String;
127    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
128        match serde_json::from_slice::<StateBirthDeathCertificate>(value) {
129            Ok(v) => Ok(v),
130            Err(e) => Err(e.to_string()),
131        }
132    }
133}