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