@@ -28,12 +28,15 @@ use infer::Infer;
2828/// ```
2929// NOTE: we cannot statically initialize Strings with values yet, so we keep dedicated static
3030// fields for the static strings.
31- #[ derive( Clone , Eq ) ]
31+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
3232pub struct Mime {
3333 pub ( crate ) essence : Cow < ' static , str > ,
3434 pub ( crate ) basetype : Cow < ' static , str > ,
3535 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 ) > ,
3740}
3841
3942impl Mime {
@@ -81,45 +84,24 @@ impl Mime {
8184 /// Get a reference to a param.
8285 pub fn param ( & self , name : impl Into < ParamName > ) -> Option < & ParamValue > {
8386 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)
9692 }
9793
9894 /// Remove a param from the set. Returns the `ParamValue` if it was contained within the set.
9995 pub fn remove_param ( & mut self , name : impl Into < ParamName > ) -> Option < ParamValue > {
10096 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" ) ) ) ;
121100 }
122- ret
101+ self . params
102+ . iter ( )
103+ . position ( |( k, _) | k == & name)
104+ . map ( |pos| self . params . remove ( pos) . 1 )
123105 }
124106}
125107
@@ -129,11 +111,11 @@ impl Display for Mime {
129111 }
130112}
131113
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+ // }
137119
138120impl FromStr for Mime {
139121 type Err = crate :: Error ;
@@ -164,12 +146,6 @@ impl ToHeaderValues for Mime {
164146 }
165147}
166148
167- impl PartialEq < Mime > for Mime {
168- fn eq ( & self , other : & Mime ) -> bool {
169- self . essence == other. essence
170- }
171- }
172-
173149/// A parameter name.
174150#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
175151pub struct ParamName ( Cow < ' static , str > ) ;
@@ -233,11 +209,3 @@ impl PartialEq<str> for ParamValue {
233209 self . 0 == other
234210 }
235211}
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