File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -135,6 +135,16 @@ impl fmt::Display for ContentType {
135135 }
136136}
137137
138+ impl std:: str:: FromStr for ContentType {
139+ type Err = :: Error ;
140+
141+ fn from_str ( s : & str ) -> Result < ContentType , Self :: Err > {
142+ s. parse :: < Mime > ( )
143+ . map ( |m| m. into ( ) )
144+ . map_err ( |_| :: Error :: invalid ( ) )
145+ }
146+ }
147+
138148#[ cfg( test) ]
139149mod tests {
140150 use super :: super :: test_decode;
@@ -148,6 +158,15 @@ mod tests {
148158 ) ;
149159 }
150160
161+ #[ test]
162+ fn from_str ( ) {
163+ assert_eq ! (
164+ "application/json" . parse:: <ContentType >( ) . unwrap( ) ,
165+ ContentType :: json( ) ,
166+ ) ;
167+ assert ! ( "invalid-mimetype" . parse:: <ContentType >( ) . is_err( ) ) ;
168+ }
169+
151170 bench_header ! ( bench_plain, ContentType , "text/plain" ) ;
152171 bench_header ! ( bench_json, ContentType , "application/json" ) ;
153172 bench_header ! (
You can’t perform that action at this time.
0 commit comments