snops_common/state/
node_key.rs

1use std::{fmt::Write, str::FromStr};
2
3use serde::de::Error;
4
5use super::{NodeType, NODE_KEY_REGEX};
6use crate::format::{DataFormat, DataFormatReader, DataFormatWriter, DataHeaderOf};
7
8#[derive(Debug, Clone, Hash, PartialEq, Eq)]
9pub struct NodeKey {
10    pub ty: NodeType,
11    pub id: String,
12    /// The node key namespace. If `None`, is a local node.
13    pub ns: Option<String>, // TODO: string interning or otherwise not duplicating namespace
14}
15
16impl FromStr for NodeKey {
17    type Err = &'static str;
18
19    fn from_str(s: &str) -> Result<Self, Self::Err> {
20        let Some(captures) = NODE_KEY_REGEX.captures(s) else {
21            return Err("invalid node key string");
22        };
23
24        // match the type
25        let ty = NodeType::from_str(&captures["ty"]).unwrap();
26
27        // match the node ID
28        let id = String::from(&captures["id"]);
29
30        // match the namespace
31        let ns = match captures.name("ns") {
32            // local; either explicitly stated, or empty
33            Some(id) if id.as_str() == "local" => None,
34            None => None,
35
36            // literal namespace
37            Some(id) => Some(id.as_str().into()),
38        };
39
40        Ok(Self { ty, id, ns })
41    }
42}
43
44impl<'de> serde::Deserialize<'de> for NodeKey {
45    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
46    where
47        D: serde::Deserializer<'de>,
48    {
49        let s = String::deserialize(deserializer)?;
50        Self::from_str(&s).map_err(D::Error::custom)
51    }
52}
53
54impl std::fmt::Display for NodeKey {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        write!(f, "{}/{}", self.ty, self.id)?;
57        if let Some(ns) = &self.ns {
58            f.write_char('@')?;
59            f.write_str(ns)?;
60        }
61
62        Ok(())
63    }
64}
65
66impl serde::Serialize for NodeKey {
67    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
68    where
69        S: serde::Serializer,
70    {
71        serializer.serialize_str(&self.to_string())
72    }
73}
74
75impl DataFormat for NodeKey {
76    type Header = (u8, DataHeaderOf<NodeType>);
77    const LATEST_HEADER: Self::Header = (1, NodeType::LATEST_HEADER);
78
79    fn write_data<W: std::io::Write>(
80        &self,
81        writer: &mut W,
82    ) -> Result<usize, crate::format::DataWriteError> {
83        let mut written = 0;
84        written += writer.write_data(&self.ty)?;
85        written += writer.write_data(&self.id)?;
86        written += writer.write_data(&self.ns)?;
87        Ok(written)
88    }
89
90    fn read_data<R: std::io::Read>(
91        reader: &mut R,
92        header: &Self::Header,
93    ) -> Result<Self, crate::format::DataReadError> {
94        if header.0 != Self::LATEST_HEADER.0 {
95            return Err(crate::format::DataReadError::unsupported(
96                "NodeKey",
97                Self::LATEST_HEADER.0,
98                header.0,
99            ));
100        }
101
102        let ty = reader.read_data(&header.1)?;
103        let id = reader.read_data(&())?;
104        let ns = reader.read_data(&())?;
105
106        Ok(Self { ty, id, ns })
107    }
108}