Skip to content

Commit 92ed9d7

Browse files
authored
Add a FromStr implementation for ContentType (#129)
1 parent aad2004 commit 92ed9d7

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/common/content_type.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff 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)]
139149
mod 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!(

0 commit comments

Comments
 (0)