sparkplug_rs/
node_message_types.rs1use std::error::Error;
2use std::fmt::{Debug, Display, Formatter};
3use std::str::FromStr;
4
5#[derive(Debug, Clone, PartialEq)]
25pub enum NodeMessageType {
26 NBIRTH,
27 NDEATH,
28 NDATA,
29 NCMD,
30}
31
32impl ToString for NodeMessageType {
33 fn to_string(&self) -> String {
34 match self {
35 NodeMessageType::NBIRTH => "NBIRTH".to_string(),
36 NodeMessageType::NDEATH => "NDEATH".to_string(),
37 NodeMessageType::NDATA => "NDATA".to_string(),
38 NodeMessageType::NCMD => "NCMD".to_string(),
39 }
40 }
41}
42
43pub struct NodeMessageTypeParseError;
44
45impl Debug for NodeMessageTypeParseError {
46 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
47 Display::fmt(self, f)
48 }
49}
50
51impl Display for NodeMessageTypeParseError {
52 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53 f.write_str("Unknown NodeMessageType")
54 }
55}
56
57impl Error for NodeMessageTypeParseError {}
58
59impl FromStr for NodeMessageType {
60 type Err = NodeMessageTypeParseError;
61
62 fn from_str(s: &str) -> Result<Self, Self::Err> {
63 match s {
64 "NBIRTH" => Ok(NodeMessageType::NBIRTH),
65 "NDEATH" => Ok(NodeMessageType::NDEATH),
66 "NDATA" => Ok(NodeMessageType::NDATA),
67 "NCMD" => Ok(NodeMessageType::NCMD),
68 _ => Err(NodeMessageTypeParseError),
69 }
70 }
71}