weavatrix_graph/model/
id.rs1use crate::{GraphError, Result, String};
2use core::borrow::Borrow;
3use core::fmt::{Display, Formatter};
4use core::str::FromStr;
5use serde::{Deserialize, Deserializer, Serialize, de::Error as _};
6
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
8#[serde(transparent)]
9pub struct NodeId(String);
10
11impl NodeId {
12 pub fn new(value: impl Into<String>) -> Result<Self> {
18 let value = value.into();
19 if value.is_empty() {
20 Err(GraphError::EmptyNodeId)
21 } else {
22 Ok(Self(value))
23 }
24 }
25
26 #[must_use]
27 pub fn as_str(&self) -> &str {
28 &self.0
29 }
30}
31
32impl Display for NodeId {
33 fn fmt(&self, formatter: &mut Formatter<'_>) -> core::fmt::Result {
34 formatter.write_str(&self.0)
35 }
36}
37
38impl Borrow<str> for NodeId {
39 fn borrow(&self) -> &str {
40 &self.0
41 }
42}
43
44impl FromStr for NodeId {
45 type Err = GraphError;
46
47 fn from_str(value: &str) -> Result<Self> {
48 Self::new(value)
49 }
50}
51
52impl<'de> Deserialize<'de> for NodeId {
53 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
54 where
55 D: Deserializer<'de>,
56 {
57 Self::new(String::deserialize(deserializer)?).map_err(D::Error::custom)
58 }
59}