@@ -28,12 +28,15 @@ use infer::Infer;
28
28
/// ```
29
29
// NOTE: we cannot statically initialize Strings with values yet, so we keep dedicated static
30
30
// fields for the static strings.
31
- #[ derive( Clone , Eq ) ]
31
+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
32
32
pub struct Mime {
33
33
pub ( crate ) essence : Cow < ' static , str > ,
34
34
pub ( crate ) basetype : Cow < ' static , str > ,
35
35
pub ( crate ) subtype : Cow < ' static , str > ,
36
- pub ( crate ) params : Option < ParamKind > ,
36
+ // NOTE(yosh): this is a hack because we can't populate vecs in const yet.
37
+ // This enables us to encode media types as utf-8 at compilation.
38
+ pub ( crate ) is_utf8 : bool ,
39
+ pub ( crate ) params : Vec < ( ParamName , ParamValue ) > ,
37
40
}
38
41
39
42
impl Mime {
@@ -81,45 +84,24 @@ impl Mime {
81
84
/// Get a reference to a param.
82
85
pub fn param ( & self , name : impl Into < ParamName > ) -> Option < & ParamValue > {
83
86
let name: ParamName = name. into ( ) ;
84
- self . params
85
- . as_ref ( )
86
- . map ( |inner| match inner {
87
- ParamKind :: Vec ( v) => v
88
- . iter ( )
89
- . find_map ( |( k, v) | if k == & name { Some ( v) } else { None } ) ,
90
- ParamKind :: Utf8 => match name {
91
- ParamName ( Cow :: Borrowed ( "charset" ) ) => Some ( & ParamValue ( Cow :: Borrowed ( "utf8" ) ) ) ,
92
- _ => None ,
93
- } ,
94
- } )
95
- . flatten ( )
87
+ if name. as_str ( ) == "charset" && self . is_utf8 {
88
+ return Some ( & ParamValue ( Cow :: Borrowed ( "utf-8" ) ) ) ;
89
+ }
90
+
91
+ self . params . iter ( ) . find ( |( k, _) | k == & name) . map ( |( _, v) | v)
96
92
}
97
93
98
94
/// Remove a param from the set. Returns the `ParamValue` if it was contained within the set.
99
95
pub fn remove_param ( & mut self , name : impl Into < ParamName > ) -> Option < ParamValue > {
100
96
let name: ParamName = name. into ( ) ;
101
- let mut unset_params = false ;
102
- let ret = self
103
- . params
104
- . as_mut ( )
105
- . map ( |inner| match inner {
106
- ParamKind :: Vec ( v) => match v. iter ( ) . position ( |( k, _) | k == & name) {
107
- Some ( index) => Some ( v. remove ( index) . 1 ) ,
108
- None => None ,
109
- } ,
110
- ParamKind :: Utf8 => match name {
111
- ParamName ( Cow :: Borrowed ( "charset" ) ) => {
112
- unset_params = true ;
113
- Some ( ParamValue ( Cow :: Borrowed ( "utf8" ) ) )
114
- }
115
- _ => None ,
116
- } ,
117
- } )
118
- . flatten ( ) ;
119
- if unset_params {
120
- self . params = None ;
97
+ if name. as_str ( ) == "charset" && self . is_utf8 {
98
+ self . is_utf8 = false ;
99
+ return Some ( ParamValue ( Cow :: Borrowed ( "utf-8" ) ) ) ;
121
100
}
122
- ret
101
+ self . params
102
+ . iter ( )
103
+ . position ( |( k, _) | k == & name)
104
+ . map ( |pos| self . params . remove ( pos) . 1 )
123
105
}
124
106
}
125
107
@@ -129,11 +111,11 @@ impl Display for Mime {
129
111
}
130
112
}
131
113
132
- impl Debug for Mime {
133
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
134
- Debug :: fmt ( & self . essence , f)
135
- }
136
- }
114
+ // impl Debug for Mime {
115
+ // fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116
+ // Debug::fmt(&self.essence, f)
117
+ // }
118
+ // }
137
119
138
120
impl FromStr for Mime {
139
121
type Err = crate :: Error ;
@@ -164,12 +146,6 @@ impl ToHeaderValues for Mime {
164
146
}
165
147
}
166
148
167
- impl PartialEq < Mime > for Mime {
168
- fn eq ( & self , other : & Mime ) -> bool {
169
- self . essence == other. essence
170
- }
171
- }
172
-
173
149
/// A parameter name.
174
150
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
175
151
pub struct ParamName ( Cow < ' static , str > ) ;
@@ -233,11 +209,3 @@ impl PartialEq<str> for ParamValue {
233
209
self . 0 == other
234
210
}
235
211
}
236
-
237
- /// This is a hack that allows us to mark a trait as utf8 during compilation. We
238
- /// can remove this once we can construct HashMap during compilation.
239
- #[ derive( Debug , Clone , PartialEq , Eq ) ]
240
- pub ( crate ) enum ParamKind {
241
- Utf8 ,
242
- Vec ( Vec < ( ParamName , ParamValue ) > ) ,
243
- }
0 commit comments