Skip to content

Commit 7c5c7c3

Browse files
committed
Make code for Identifier match Name
1 parent eb63537 commit 7c5c7c3

4 files changed

Lines changed: 62 additions & 32 deletions

File tree

src/fontinfo.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,7 @@ mod tests {
17501750
Line::Angle { x: 1.0, y: 2.0, degrees: 0.0 },
17511751
Some(Name::new_raw(" [locked]")),
17521752
Some(Color::new(1.0, 1.0, 1.0, 1.0).unwrap()),
1753-
Some(Identifier::new("abc").unwrap()),
1753+
Some(Identifier::new_raw("abc")),
17541754
None
17551755
),
17561756
])
@@ -1907,14 +1907,14 @@ mod tests {
19071907
Line::Horizontal(10.0),
19081908
None,
19091909
None,
1910-
Some(Identifier::new("test1").unwrap()),
1910+
Some(Identifier::new_raw("test1")),
19111911
None,
19121912
),
19131913
Guideline::new(
19141914
Line::Vertical(20.0),
19151915
None,
19161916
None,
1917-
Some(Identifier::new("test2").unwrap()),
1917+
Some(Identifier::new_raw("test2")),
19181918
None,
19191919
),
19201920
]);
@@ -1925,14 +1925,14 @@ mod tests {
19251925
Line::Horizontal(10.0),
19261926
None,
19271927
None,
1928-
Some(Identifier::new("test1").unwrap()),
1928+
Some(Identifier::new_raw("test1")),
19291929
None,
19301930
),
19311931
Guideline::new(
19321932
Line::Vertical(20.0),
19331933
None,
19341934
None,
1935-
Some(Identifier::new("test1").unwrap()),
1935+
Some(Identifier::new_raw("test1")),
19361936
None,
19371937
),
19381938
]);

src/glyph/builder.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ mod tests {
203203
fn builder_basic() -> Result<(), ErrorKind> {
204204
let mut outline_builder = OutlineBuilder::new();
205205
outline_builder
206-
.begin_path(Some(Identifier::new("abc").unwrap()))?
206+
.begin_path(Some(Identifier::new_raw("abc")))?
207207
.add_point((173.0, 536.0), PointType::Line, false, None, None)?
208208
.add_point((85.0, 536.0), PointType::Line, false, None, None)?
209209
.add_point((85.0, 0.0), PointType::Line, false, None, None)?
@@ -212,13 +212,13 @@ mod tests {
212212
PointType::Line,
213213
false,
214214
None,
215-
Some(Identifier::new("def").unwrap()),
215+
Some(Identifier::new_raw("def")),
216216
)?
217217
.end_path()?
218218
.add_component(
219219
Name::new_raw("hallo"),
220220
AffineTransform::default(),
221-
Some(Identifier::new("xyz").unwrap()),
221+
Some(Identifier::new_raw("xyz")),
222222
);
223223
let (contours, components) = outline_builder.finish()?;
224224

@@ -235,11 +235,11 @@ mod tests {
235235
PointType::Line,
236236
false,
237237
None,
238-
Some(Identifier::new("def").unwrap()),
238+
Some(Identifier::new_raw("def")),
239239
None,
240240
),
241241
],
242-
Some(Identifier::new("abc").unwrap()),
242+
Some(Identifier::new_raw("abc")),
243243
None,
244244
)]
245245
);
@@ -256,7 +256,7 @@ mod tests {
256256
x_offset: 0.0,
257257
y_offset: 0.0,
258258
},
259-
Some(Identifier::new("xyz").unwrap()),
259+
Some(Identifier::new_raw("xyz")),
260260
None,
261261
)]
262262
);
@@ -268,15 +268,15 @@ mod tests {
268268
#[should_panic(expected = "UnfinishedDrawing")]
269269
fn outline_builder_unfinished_drawing() {
270270
let mut outline_builder = OutlineBuilder::new();
271-
outline_builder.begin_path(Some(Identifier::new("abc").unwrap())).unwrap();
271+
outline_builder.begin_path(Some(Identifier::new_raw("abc"))).unwrap();
272272
outline_builder.finish().unwrap();
273273
}
274274

275275
#[test]
276276
#[should_panic(expected = "UnfinishedDrawing")]
277277
fn outline_builder_unfinished_drawing2() {
278278
OutlineBuilder::new()
279-
.begin_path(Some(Identifier::new("abc").unwrap()))
279+
.begin_path(Some(Identifier::new_raw("abc")))
280280
.unwrap()
281281
.begin_path(None)
282282
.unwrap();

src/guideline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ mod tests {
186186
Line::Angle { x: 10.0, y: 20.0, degrees: 360.0 },
187187
Some(Name::new_raw("hello")),
188188
Some(Color::new(0.0, 0.5, 0.0, 0.5).unwrap()),
189-
Some(Identifier::new("abcABC123").unwrap()),
189+
Some(Identifier::new_raw("abcABC123")),
190190
None,
191191
);
192192
assert_tokens(

src/identifier.rs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::hash::Hash;
2-
use std::str::FromStr;
32
use std::sync::Arc;
43

54
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
@@ -19,45 +18,76 @@ use crate::error::ErrorKind;
1918
pub struct Identifier(Arc<str>);
2019

2120
impl Identifier {
22-
/// Create a new [`Identifier`] from a [`String`], if it is valid.
21+
/// Create a new [`Identifier`] from a string, if it is valid.
2322
///
2423
/// A valid identifier must have between 0 and 100 characters, and each
2524
/// character must be in the printable ASCII range, 0x20 to 0x7E.
26-
pub fn new(s: impl Into<Arc<str>>) -> Result<Self, ErrorKind> {
27-
let string = s.into();
28-
if is_valid_identifier(&string) {
29-
Ok(Identifier(string))
25+
pub fn new(string: &str) -> Result<Self, ErrorKind> {
26+
if is_valid_identifier(string) {
27+
Ok(Identifier(string.into()))
3028
} else {
3129
Err(ErrorKind::BadIdentifier)
3230
}
3331
}
3432

33+
/// Creates a new `Identifier`, panicking if the given identifier is invalid.
34+
#[cfg(test)]
35+
pub(crate) fn new_raw(string: &str) -> Self {
36+
assert!(is_valid_identifier(string));
37+
Self(string.into())
38+
}
39+
3540
/// Create a new [`Identifier`] from a UUID v4 identifier.
3641
pub fn from_uuidv4() -> Self {
37-
Self::new(uuid::Uuid::new_v4().to_string()).unwrap()
42+
Self::new(uuid::Uuid::new_v4().to_string().as_ref()).unwrap()
3843
}
3944

4045
/// Return the raw identifier, as a `&str`.
4146
pub fn as_str(&self) -> &str {
42-
&self.0
47+
self.as_ref()
4348
}
4449
}
4550

46-
impl PartialEq<String> for Identifier {
47-
fn eq(&self, other: &String) -> bool {
48-
*self.0 == *other
51+
fn is_valid_identifier(s: &str) -> bool {
52+
s.len() <= 100 && s.bytes().all(|b| (0x20..=0x7E).contains(&b))
53+
}
54+
55+
impl AsRef<str> for Identifier {
56+
fn as_ref(&self) -> &str {
57+
self.0.as_ref()
4958
}
5059
}
5160

52-
impl FromStr for Identifier {
53-
type Err = ErrorKind;
54-
fn from_str(s: &str) -> Result<Self, Self::Err> {
55-
Identifier::new(s)
61+
impl std::ops::Deref for Identifier {
62+
type Target = str;
63+
fn deref(&self) -> &Self::Target {
64+
self.0.as_ref()
5665
}
5766
}
5867

59-
fn is_valid_identifier(s: &Arc<str>) -> bool {
60-
s.len() <= 100 && s.bytes().all(|b| (0x20..=0x7E).contains(&b))
68+
// so that assert_eq! macros work
69+
impl<'a> PartialEq<&'a str> for Identifier {
70+
fn eq(&self, other: &&'a str) -> bool {
71+
self.0.as_ref() == *other
72+
}
73+
}
74+
75+
impl<'a> PartialEq<Identifier> for &'a str {
76+
fn eq(&self, other: &Identifier) -> bool {
77+
other == self
78+
}
79+
}
80+
81+
impl std::fmt::Display for Identifier {
82+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
83+
std::fmt::Display::fmt(&self.0, f)
84+
}
85+
}
86+
87+
impl std::borrow::Borrow<str> for Identifier {
88+
fn borrow(&self) -> &str {
89+
self.0.as_ref()
90+
}
6191
}
6292

6393
impl Serialize for Identifier {
@@ -79,7 +109,7 @@ impl<'de> Deserialize<'de> for Identifier {
79109
D: Deserializer<'de>,
80110
{
81111
let string = String::deserialize(deserializer)?;
82-
Identifier::new(string).map_err(de::Error::custom)
112+
Identifier::new(string.as_str()).map_err(de::Error::custom)
83113
}
84114
}
85115

0 commit comments

Comments
 (0)