trillium_http/headers/
entry.rs1use super::{HeaderName, HeaderValues, KnownHeaderName, UnknownHeaderName};
2use hashbrown::hash_map;
3use std::fmt::{self, Debug, Formatter};
4
5#[derive(Debug)]
7pub enum Entry<'a> {
8 Vacant(VacantEntry<'a>),
11 Occupied(OccupiedEntry<'a>),
13}
14
15pub struct VacantEntry<'a>(pub(super) VacantEntryInner<'a>);
19
20impl Debug for VacantEntry<'_> {
21 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
22 f.debug_struct("VacantEntry")
23 .field("name", &self.name())
24 .finish()
25 }
26}
27
28pub(super) enum VacantEntryInner<'a> {
29 Known(hash_map::VacantEntry<'a, KnownHeaderName, HeaderValues>),
30 Unknown(hash_map::VacantEntry<'a, UnknownHeaderName<'static>, HeaderValues>),
31}
32
33pub struct OccupiedEntry<'a>(pub(super) OccupiedEntryInner<'a>);
37
38impl Debug for OccupiedEntry<'_> {
39 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
40 f.debug_struct("OccupiedEntry")
41 .field("name", &self.name())
42 .field("values", &self.values())
43 .finish()
44 }
45}
46
47pub(super) enum OccupiedEntryInner<'a> {
48 Known(hash_map::OccupiedEntry<'a, KnownHeaderName, HeaderValues>),
49 Unknown(hash_map::OccupiedEntry<'a, UnknownHeaderName<'static>, HeaderValues>),
50}
51
52impl<'a> Entry<'a> {
53 pub fn name(&self) -> HeaderName<'_> {
55 match self {
56 Entry::Vacant(v) => v.name(),
57 Entry::Occupied(o) => o.name(),
58 }
59 }
60
61 pub fn insert(self, values: impl Into<HeaderValues>) -> &'a mut HeaderValues {
65 match self {
66 Entry::Vacant(v) => v.insert(values),
67 Entry::Occupied(mut o) => {
68 o.insert(values);
69 o.into_mut()
70 }
71 }
72 }
73
74 pub fn append(self, values: impl Into<HeaderValues>) -> &'a mut HeaderValues {
77 match self {
78 Entry::Vacant(v) => v.insert(values),
79 Entry::Occupied(mut o) => {
80 o.values_mut().extend(values);
81 o.into_mut()
82 }
83 }
84 }
85
86 pub fn or_insert_with<V: Into<HeaderValues>>(
89 self,
90 values: impl FnOnce() -> V,
91 ) -> &'a mut HeaderValues {
92 match self {
93 Self::Occupied(entry) => entry.into_mut(),
94 Self::Vacant(entry) => entry.insert(values()),
95 }
96 }
97
98 pub fn or_insert(self, values: impl Into<HeaderValues>) -> &'a mut HeaderValues {
101 match self {
102 Entry::Vacant(vacant) => vacant.insert(values),
103 Entry::Occupied(occupied) => occupied.into_mut(),
104 }
105 }
106
107 #[must_use]
110 pub fn and_modify(self, f: impl FnOnce(&mut HeaderValues)) -> Self {
111 match self {
112 Self::Occupied(mut entry) => {
113 f(entry.values_mut());
114 Self::Occupied(entry)
115 }
116 Self::Vacant(entry) => Self::Vacant(entry),
117 }
118 }
119
120 pub fn is_vacant(&self) -> bool {
122 matches!(self, Self::Vacant(_))
123 }
124
125 pub fn is_occupied(&self) -> bool {
127 matches!(self, Self::Occupied(_))
128 }
129
130 pub fn occupied(self) -> Option<OccupiedEntry<'a>> {
132 match self {
133 Entry::Vacant(_) => None,
134 Entry::Occupied(o) => Some(o),
135 }
136 }
137
138 pub fn vacant(self) -> Option<VacantEntry<'a>> {
140 match self {
141 Entry::Vacant(v) => Some(v),
142 Entry::Occupied(_) => None,
143 }
144 }
145}
146
147impl<'a> VacantEntry<'a> {
148 pub fn name(&self) -> HeaderName<'_> {
150 match &self.0 {
151 VacantEntryInner::Known(k) => (*k.key()).into(),
152 VacantEntryInner::Unknown(u) => u.key().reborrow().into(),
153 }
154 }
155
156 pub fn insert(self, values: impl Into<HeaderValues>) -> &'a mut HeaderValues {
158 match self.0 {
159 VacantEntryInner::Known(k) => k.insert(values.into()),
160 VacantEntryInner::Unknown(u) => u.insert(values.into()),
161 }
162 }
163}
164
165impl<'a> OccupiedEntry<'a> {
166 pub fn name(&self) -> HeaderName<'_> {
168 match &self.0 {
169 OccupiedEntryInner::Known(known) => (*known.key()).into(),
170 OccupiedEntryInner::Unknown(unknown) => unknown.key().reborrow().into(),
171 }
172 }
173
174 pub fn values(&self) -> &HeaderValues {
176 match &self.0 {
177 OccupiedEntryInner::Known(known) => known.get(),
178 OccupiedEntryInner::Unknown(unknown) => unknown.get(),
179 }
180 }
181
182 pub fn values_mut(&mut self) -> &mut HeaderValues {
184 match &mut self.0 {
185 OccupiedEntryInner::Known(known) => known.get_mut(),
186 OccupiedEntryInner::Unknown(unknown) => unknown.get_mut(),
187 }
188 }
189
190 pub fn remove_entry(self) -> (HeaderName<'static>, HeaderValues) {
193 match self.0 {
194 OccupiedEntryInner::Known(known) => {
195 let (n, v) = known.remove_entry();
196 (n.into(), v)
197 }
198 OccupiedEntryInner::Unknown(unknown) => {
199 let (n, v) = unknown.remove_entry();
200 (n.into(), v)
201 }
202 }
203 }
204
205 pub fn remove(self) -> HeaderValues {
207 match self.0 {
208 OccupiedEntryInner::Known(known) => known.remove(),
209 OccupiedEntryInner::Unknown(unknown) => unknown.remove(),
210 }
211 }
212
213 pub fn into_mut(self) -> &'a mut HeaderValues {
218 match self.0 {
219 OccupiedEntryInner::Known(k) => k.into_mut(),
220 OccupiedEntryInner::Unknown(u) => u.into_mut(),
221 }
222 }
223
224 pub fn insert(&mut self, values: impl Into<HeaderValues>) -> HeaderValues {
226 match &mut self.0 {
227 OccupiedEntryInner::Known(k) => k.insert(values.into()),
228 OccupiedEntryInner::Unknown(u) => u.insert(values.into()),
229 }
230 }
231
232 pub fn append(&mut self, values: impl Into<HeaderValues>) {
234 self.values_mut().extend(values);
235 }
236}