Debian Codegraph @

src/name.rs

raw symbols SCIP
1 use super::parse::*;
2 use alloc::{borrow::Cow, fmt, string::String};
3 use core::{
4 cmp::Ordering,
5 hash::{Hash, Hasher},
6 };
7
8 /// A media-type name.
9 ///
10 /// A valid name has the following requirements:
11 ///
12 /// - The first character must be an alphabet or a number.
13 /// - The subsequent characters must be alphabets, numbers or `!#$&-^_.+%*'`.
14 /// - Length of the name can not exceed 127 bytes.
15 #[derive(Debug, Copy, Clone)]
16 pub struct Name<'a>(&'a str);
17
18 impl<'a> Name<'a> {
19 /// Constructs a `Name`.
20 ///
21 /// If the string is not valid as a name, returns `None`.
22 #[must_use]
23 pub fn new(s: &'a str) -> Option<Self> {
24 if is_restricted_name(s) {
25 Some(Self(s))
26 } else {
27 None
28 }
29 }
30
31 /// Returns the underlying string.
32 #[must_use]
33 pub const fn as_str(&self) -> &'a str {
34 self.0
35 }
36
37 /// The maximum byte length of a name.
38 pub const MAX_LENGTH: usize = 127;
39
40 /// Constructs a `Name` without validation.
41 pub const fn new_unchecked(s: &'a str) -> Self {
42 Self(s)
43 }
44 }
45
46 impl fmt::Display for Name<'_> {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 f.write_str(self.0)
49 }
50 }
51
52 impl AsRef<str> for Name<'_> {
53 fn as_ref(&self) -> &str {
54 self.0
55 }
56 }
57
58 impl PartialEq for Name<'_> {
59 fn eq(&self, other: &Self) -> bool {
60 self.0.eq_ignore_ascii_case(other.as_ref())
61 }
62 }
63
64 impl Eq for Name<'_> {}
65
66 impl PartialOrd for Name<'_> {
67 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
68 Some(self.cmp(other))
69 }
70 }
71
72 impl Ord for Name<'_> {
73 fn cmp(&self, other: &Self) -> Ordering {
74 self.0
75 .chars()
76 .map(|c| c.to_ascii_lowercase())
77 .cmp(other.0.chars().map(|c| c.to_ascii_lowercase()))
78 }
79 }
80
81 impl Hash for Name<'_> {
82 fn hash<H: Hasher>(&self, state: &mut H) {
83 self.0.to_ascii_lowercase().hash(state);
84 }
85 }
86
87 impl PartialEq<String> for Name<'_> {
88 fn eq(&self, other: &String) -> bool {
89 self.eq(other.as_str())
90 }
91 }
92
93 impl PartialOrd<String> for Name<'_> {
94 fn partial_cmp(&self, other: &String) -> Option<Ordering> {
95 self.partial_cmp(other.as_str())
96 }
97 }
98
99 impl PartialEq<&String> for Name<'_> {
100 fn eq(&self, other: &&String) -> bool {
101 self.eq(other.as_str())
102 }
103 }
104
105 impl PartialOrd<&String> for Name<'_> {
106 fn partial_cmp(&self, other: &&String) -> Option<Ordering> {
107 self.partial_cmp(other.as_str())
108 }
109 }
110
111 impl PartialEq<str> for Name<'_> {
112 fn eq(&self, other: &str) -> bool {
113 self.0.eq_ignore_ascii_case(other)
114 }
115 }
116
117 impl PartialOrd<str> for Name<'_> {
118 fn partial_cmp(&self, other: &str) -> Option<Ordering> {
119 Some(
120 self.0
121 .chars()
122 .map(|c| c.to_ascii_lowercase())
123 .cmp(other.chars().map(|c| c.to_ascii_lowercase())),
124 )
125 }
126 }
127
128 impl PartialEq<&str> for Name<'_> {
129 fn eq(&self, other: &&str) -> bool {
130 self.eq(*other)
131 }
132 }
133
134 impl PartialOrd<&str> for Name<'_> {
135 fn partial_cmp(&self, other: &&str) -> Option<Ordering> {
136 self.partial_cmp(*other)
137 }
138 }
139
140 impl PartialEq<Cow<'_, str>> for Name<'_> {
141 fn eq(&self, other: &Cow<'_, str>) -> bool {
142 self.eq(other.as_ref())
143 }
144 }
145
146 impl PartialOrd<Cow<'_, str>> for Name<'_> {
147 fn partial_cmp(&self, other: &Cow<'_, str>) -> Option<Ordering> {
148 self.partial_cmp(other.as_ref())
149 }
150 }
151
152 impl PartialEq<&Cow<'_, str>> for Name<'_> {
153 fn eq(&self, other: &&Cow<'_, str>) -> bool {
154 self.eq(other.as_ref())
155 }
156 }
157
158 impl PartialOrd<&Cow<'_, str>> for Name<'_> {
159 fn partial_cmp(&self, other: &&Cow<'_, str>) -> Option<Ordering> {
160 self.partial_cmp(other.as_ref())
161 }
162 }