dora_message/
id.rs

1use std::{borrow::Borrow, convert::Infallible, str::FromStr};
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6#[derive(
7    Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
8)]
9pub struct NodeId(pub(crate) String);
10
11#[derive(Debug)]
12pub struct NodeIdContainsSlash;
13
14impl std::fmt::Display for NodeIdContainsSlash {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        write!(f, "NodeId must not contain `/`")
17    }
18}
19
20impl std::error::Error for NodeIdContainsSlash {}
21
22impl FromStr for NodeId {
23    type Err = NodeIdContainsSlash;
24
25    fn from_str(s: &str) -> Result<Self, Self::Err> {
26        if s.contains('/') {
27            return Err(NodeIdContainsSlash);
28        }
29        Ok(Self(s.to_owned()))
30    }
31}
32
33impl From<String> for NodeId {
34    fn from(id: String) -> Self {
35        Self(id)
36    }
37}
38
39impl std::fmt::Display for NodeId {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        std::fmt::Display::fmt(&self.0, f)
42    }
43}
44
45impl AsRef<str> for NodeId {
46    fn as_ref(&self) -> &str {
47        &self.0
48    }
49}
50
51#[derive(
52    Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
53)]
54pub struct OperatorId(String);
55
56impl FromStr for OperatorId {
57    type Err = Infallible;
58
59    fn from_str(s: &str) -> Result<Self, Self::Err> {
60        Ok(Self(s.to_owned()))
61    }
62}
63
64impl From<String> for OperatorId {
65    fn from(id: String) -> Self {
66        Self(id)
67    }
68}
69
70impl std::fmt::Display for OperatorId {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        std::fmt::Display::fmt(&self.0, f)
73    }
74}
75
76impl AsRef<str> for OperatorId {
77    fn as_ref(&self) -> &str {
78        &self.0
79    }
80}
81
82#[derive(
83    Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
84)]
85pub struct DataId(String);
86
87impl From<DataId> for String {
88    fn from(id: DataId) -> Self {
89        id.0
90    }
91}
92
93impl From<String> for DataId {
94    fn from(id: String) -> Self {
95        Self(id)
96    }
97}
98
99impl std::fmt::Display for DataId {
100    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101        std::fmt::Display::fmt(&self.0, f)
102    }
103}
104
105impl std::ops::Deref for DataId {
106    type Target = String;
107
108    fn deref(&self) -> &Self::Target {
109        &self.0
110    }
111}
112
113impl AsRef<String> for DataId {
114    fn as_ref(&self) -> &String {
115        &self.0
116    }
117}
118
119impl AsRef<str> for DataId {
120    fn as_ref(&self) -> &str {
121        &self.0
122    }
123}
124
125impl Borrow<String> for DataId {
126    fn borrow(&self) -> &String {
127        &self.0
128    }
129}
130
131impl Borrow<str> for DataId {
132    fn borrow(&self) -> &str {
133        &self.0
134    }
135}