wayver's git archive


an obsidian renderer
git clone https://git.wayver.dev/sable

sable-canvas/src/id.rs@337ba67f65eaa17b44e371af7c0f0c761d6aa914

raw
Date Commit Message Author Files + -
2026-02-23 01:55 initial mvp wayverd 139 17808 0
...

1/// An empty ID of a [`Node`] or [`Edge`].
2///
3/// [`Edge`]: crate::Edge
4/// [`Node`]: crate::Node
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, thiserror::Error)]
6#[error("ID is empty")]
7pub struct EmptyId;
8
9/// The unique ID of an [`Node`].
10///
11/// [`Node`]: crate::Node
12#[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    /// Returns the inner [`String`] representation.
21    #[must_use]
22    pub fn into_inner(self) -> String {
23        self.0
24    }
25
26    /// Returns a reference to the inner [`String`] representation.
27    #[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/// The unique ID of an [`Edge`].
62///
63/// [`Edge`]: crate::Edge
64#[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    /// Returns the inner [`String`] representation.
73    #[must_use]
74    pub fn into_inner(self) -> String {
75        self.0
76    }
77
78    /// Returns a reference to the inner [`String`] representation.
79    #[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