rocket_community/form/name/
name.rs1use std::ops::Deref;
2
3use ref_cast::RefCast;
4
5use crate::form::name::*;
6use crate::http::RawStr;
7
8#[repr(transparent)]
29#[derive(RefCast)]
30pub struct Name(str);
31
32impl Name {
33 pub fn new<S: AsRef<str> + ?Sized>(string: &S) -> &Name {
45 Name::ref_cast(string.as_ref())
46 }
47
48 pub fn keys(&self) -> impl Iterator<Item = &Key> {
63 struct Keys<'v>(NameView<'v>);
64
65 impl<'v> Iterator for Keys<'v> {
66 type Item = &'v Key;
67
68 fn next(&mut self) -> Option<Self::Item> {
69 if self.0.exhausted() {
70 return None;
71 }
72
73 let key = self.0.key_lossy();
74 self.0.shift();
75 Some(key)
76 }
77 }
78
79 Keys(NameView::new(self))
80 }
81
82 pub fn prefixes(&self) -> impl Iterator<Item = &Name> {
100 struct Prefixes<'v>(NameView<'v>);
101
102 impl<'v> Iterator for Prefixes<'v> {
103 type Item = &'v Name;
104
105 fn next(&mut self) -> Option<Self::Item> {
106 if self.0.exhausted() {
107 return None;
108 }
109
110 let name = self.0.as_name();
111 self.0.shift();
112 Some(name)
113 }
114 }
115
116 Prefixes(NameView::new(self))
117 }
118
119 pub fn as_str(&self) -> &str {
131 &self.0
132 }
133}
134
135impl serde::Serialize for Name {
136 fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
137 where
138 S: serde::Serializer,
139 {
140 self.0.serialize(ser)
141 }
142}
143
144impl<'de: 'a, 'a> serde::Deserialize<'de> for &'a Name {
145 fn deserialize<D>(de: D) -> Result<Self, D::Error>
146 where
147 D: serde::Deserializer<'de>,
148 {
149 <&'a str as serde::Deserialize<'de>>::deserialize(de).map(Name::new)
150 }
151}
152
153impl<'a, S: AsRef<str> + ?Sized> From<&'a S> for &'a Name {
154 #[inline]
155 fn from(string: &'a S) -> Self {
156 Name::new(string)
157 }
158}
159
160impl Deref for Name {
161 type Target = str;
162
163 fn deref(&self) -> &Self::Target {
164 &self.0
165 }
166}
167
168impl<I: core::slice::SliceIndex<str, Output = str>> core::ops::Index<I> for Name {
169 type Output = Name;
170
171 #[inline]
172 fn index(&self, index: I) -> &Self::Output {
173 self.0[index].into()
174 }
175}
176
177impl PartialEq for Name {
178 fn eq(&self, other: &Self) -> bool {
179 self.keys().eq(other.keys())
180 }
181}
182
183impl PartialEq<str> for Name {
184 fn eq(&self, other: &str) -> bool {
185 self == Name::new(other)
186 }
187}
188
189impl PartialEq<Name> for str {
190 fn eq(&self, other: &Name) -> bool {
191 Name::new(self) == other
192 }
193}
194
195impl PartialEq<&str> for Name {
196 fn eq(&self, other: &&str) -> bool {
197 self == Name::new(other)
198 }
199}
200
201impl PartialEq<Name> for &str {
202 fn eq(&self, other: &Name) -> bool {
203 Name::new(self) == other
204 }
205}
206
207impl AsRef<Name> for str {
208 fn as_ref(&self) -> &Name {
209 Name::new(self)
210 }
211}
212
213impl AsRef<Name> for RawStr {
214 fn as_ref(&self) -> &Name {
215 Name::new(self)
216 }
217}
218
219impl AsRef<Name> for Name {
220 fn as_ref(&self) -> &Name {
221 self
222 }
223}
224
225impl Eq for Name {}
226
227impl std::hash::Hash for Name {
228 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
229 self.keys().for_each(|k| k.hash(state))
230 }
231}
232
233impl std::fmt::Display for Name {
234 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
235 self.0.fmt(f)
236 }
237}
238
239impl std::fmt::Debug for Name {
240 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
241 self.0.fmt(f)
242 }
243}