sable-canvas/src/id.rs@main
raw
1#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, thiserror::Error)]
6#[error("ID is empty")]
7pub struct EmptyId;
8
9#[derive(
13 Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
14)]
15#[repr(transparent)]
16#[serde(transparent)]
17pub struct NodeId(pub(self) String);
18
19impl NodeId {
20 #[must_use]
22 pub fn into_inner(self) -> String {
23 self.0
24 }
25
26 #[must_use]
28 pub const fn as_str(&self) -> &str {
29 self.0.as_str()
30 }
31}
32
33impl std::fmt::Display for NodeId {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 self.0.fmt(f)
36 }
37}
38
39impl std::str::FromStr for NodeId {
40 type Err = EmptyId;
41
42 fn from_str(value: &str) -> Result<Self, Self::Err> {
43 if value.is_empty() {
44 return Err(EmptyId);
45 }
46 Ok(Self(value.to_string()))
47 }
48}
49
50impl TryFrom<String> for NodeId {
51 type Error = EmptyId;
52
53 fn try_from(value: String) -> Result<Self, Self::Error> {
54 if value.is_empty() {
55 return Err(EmptyId);
56 }
57 Ok(Self(value))
58 }
59}
60
61#[derive(
65 Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
66)]
67#[repr(transparent)]
68#[serde(transparent)]
69pub struct EdgeId(pub(self) String);
70
71impl EdgeId {
72 #[must_use]
74 pub fn into_inner(self) -> String {
75 self.0
76 }
77
78 #[must_use]
80 pub const fn as_str(&self) -> &str {
81 self.0.as_str()
82 }
83}
84
85impl std::fmt::Display for EdgeId {
86 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87 self.0.fmt(f)
88 }
89}
90
91impl std::str::FromStr for EdgeId {
92 type Err = EmptyId;
93
94 fn from_str(value: &str) -> Result<Self, Self::Err> {
95 if value.is_empty() {
96 return Err(EmptyId);
97 }
98 Ok(Self(value.to_string()))
99 }
100}
101
102impl TryFrom<String> for EdgeId {
103 type Error = EmptyId;
104
105 fn try_from(value: String) -> Result<Self, Self::Error> {
106 if value.is_empty() {
107 return Err(EmptyId);
108 }
109 Ok(Self(value))
110 }
111}
112