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
|
use std::fmt;
use crate::normalize::{ident, nfc};
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize, sqlx::Type)]
#[serde(from = "String", into = "String")]
pub struct Name {
display: nfc::String,
canonical: ident::String,
}
impl Name {
pub fn new<D, C>(display: D, canonical: C) -> Result<Self, Error>
where
D: AsRef<str>,
C: AsRef<str>,
{
let name = Self::from(display);
if name.canonical.as_str() == canonical.as_ref() {
Ok(name)
} else {
Err(Error::CanonicalMismatch(
canonical.as_ref().into(),
name.canonical,
name.display,
))
}
}
pub fn optional<D, C>(display: Option<D>, canonical: Option<C>) -> Result<Option<Self>, Error>
where
D: AsRef<str>,
C: AsRef<str>,
{
display
.zip(canonical)
.map(|(display, canonical)| Self::new(display, canonical))
.transpose()
}
pub fn display(&self) -> &nfc::String {
&self.display
}
pub fn canonical(&self) -> &ident::String {
&self.canonical
}
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("stored canonical form {stored:#?} does not match computed canonical form {computed:#?} for name {expected:#?}", stored=.0, computed=.1.as_str(), expected=.2.as_str())]
CanonicalMismatch(String, ident::String, nfc::String),
}
impl Default for Name {
fn default() -> Self {
Self::from(String::default())
}
}
impl fmt::Display for Name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.display.fmt(f)
}
}
impl<S> From<S> for Name
where
S: AsRef<str>,
{
fn from(name: S) -> Self {
let display = nfc::String::from(&name);
let canonical = ident::String::from(&name);
Self { display, canonical }
}
}
impl From<Name> for String {
fn from(name: Name) -> Self {
name.display.into()
}
}
|