1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::value::MAXIMUM_NAME_LENGTH;
use std::cmp::{Eq, PartialEq};
use std::convert::{From, TryFrom};
use std::fmt::{Display, Formatter, Result as FmtResult};
use thiserror::Error;
enum Input {
AlphabeticAndUnderscoreAndHyphen,
Digit,
Dot,
}
impl TryFrom<u8> for Input {
type Error = WellKnownBusNameError;
fn try_from(c: u8) -> Result<Self, Self::Error> {
if c.is_ascii_alphabetic() || c == b'_' || c == b'-' {
Ok(Input::AlphabeticAndUnderscoreAndHyphen)
} else if c.is_ascii_digit() {
Ok(Input::Digit)
} else if c == b'.' {
Ok(Input::Dot)
} else {
Err(WellKnownBusNameError::InvalidChar(c))
}
}
}
enum State {
Start,
FirstElement,
Dot,
SubsequentElement,
}
impl State {
fn consume(self, i: Input) -> Result<State, WellKnownBusNameError> {
match self {
State::Start => match i {
Input::AlphabeticAndUnderscoreAndHyphen => Ok(State::FirstElement),
Input::Digit => Err(WellKnownBusNameError::BeginDigit),
Input::Dot => Err(WellKnownBusNameError::BeginDot),
},
State::FirstElement => match i {
Input::AlphabeticAndUnderscoreAndHyphen => Ok(State::FirstElement),
Input::Digit => Ok(State::FirstElement),
Input::Dot => Ok(State::Dot),
},
State::Dot => match i {
Input::AlphabeticAndUnderscoreAndHyphen => Ok(State::SubsequentElement),
Input::Digit => Err(WellKnownBusNameError::ElementBeginDigit),
Input::Dot => Err(WellKnownBusNameError::ElementBeginDot),
},
State::SubsequentElement => match i {
Input::AlphabeticAndUnderscoreAndHyphen => Ok(State::SubsequentElement),
Input::Digit => Ok(State::SubsequentElement),
Input::Dot => Ok(State::Dot),
},
}
}
}
fn check(well_known_bus_name: &[u8]) -> Result<(), WellKnownBusNameError> {
let error_len = well_known_bus_name.len();
if MAXIMUM_NAME_LENGTH < error_len {
return Err(WellKnownBusNameError::ExceedMaximum(error_len));
}
let mut state = State::Start;
for c in well_known_bus_name {
let i = Input::try_from(*c)?;
state = state.consume(i)?;
}
match state {
State::Start => Err(WellKnownBusNameError::Empty),
State::FirstElement => Err(WellKnownBusNameError::Elements),
State::Dot => Err(WellKnownBusNameError::EndDot),
State::SubsequentElement => Ok(()),
}
}
#[derive(Debug, Clone, PartialOrd, PartialEq, Ord, Eq)]
pub struct WellKnownBusName(String);
#[derive(Debug, PartialEq, Eq, Error)]
pub enum WellKnownBusNameError {
#[error("Well-known bus name must not begin with a digit")]
BeginDigit,
#[error("Well-known bus name must not begin with a '.'")]
BeginDot,
#[error("Well-known bus name must not end with '.'")]
EndDot,
#[error("Well-known bus name element must not begin with a digit")]
ElementBeginDigit,
#[error("Well-known bus name element must not begin with a '.'")]
ElementBeginDot,
#[error("Well-known bus name is empty")]
Empty,
#[error("Well-known bus name have to be composed of 2 or more elements")]
Elements,
#[error("Well-known bus name must not exceed the maximum length: {MAXIMUM_NAME_LENGTH} < {0}")]
ExceedMaximum(usize),
#[error("Bus must only contain '[A-Z][a-z][0-9]_-.': {0}")]
InvalidChar(u8),
}
impl From<WellKnownBusName> for String {
fn from(well_known_bus_name: WellKnownBusName) -> Self {
well_known_bus_name.0
}
}
impl TryFrom<String> for WellKnownBusName {
type Error = WellKnownBusNameError;
fn try_from(well_known_bus_name: String) -> Result<Self, Self::Error> {
check(well_known_bus_name.as_bytes())?;
Ok(WellKnownBusName(well_known_bus_name))
}
}
impl TryFrom<&str> for WellKnownBusName {
type Error = WellKnownBusNameError;
fn try_from(well_known_bus_name: &str) -> Result<Self, Self::Error> {
check(well_known_bus_name.as_bytes())?;
Ok(WellKnownBusName(well_known_bus_name.to_owned()))
}
}
impl TryFrom<&[u8]> for WellKnownBusName {
type Error = WellKnownBusNameError;
fn try_from(well_known_bus_name: &[u8]) -> Result<Self, Self::Error> {
check(well_known_bus_name)?;
let well_known_bus_name = well_known_bus_name.to_vec();
unsafe {
Ok(WellKnownBusName(String::from_utf8_unchecked(
well_known_bus_name,
)))
}
}
}
impl Display for WellKnownBusName {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "{}", self.0)
}
}
impl AsRef<str> for WellKnownBusName {
fn as_ref(&self) -> &str {
&self.0
}
}
impl PartialEq<str> for WellKnownBusName {
fn eq(&self, other: &str) -> bool {
self.as_ref() == other
}
}