Skip to content

Commit 2dbd5b6

Browse files
committed
use ParamName in API bounds
1 parent d721ff4 commit 2dbd5b6

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/mime/mod.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ impl Mime {
8282
}
8383

8484
/// Get a reference to a param.
85-
pub fn param(&self, s: &str) -> Option<&ParamValue> {
85+
pub fn param(&self, name: impl Into<ParamName>) -> Option<&ParamValue> {
86+
let name: ParamName = name.into();
8687
self.params
8788
.as_ref()
8889
.map(|inner| match inner {
89-
ParamKind::Map(hm) => hm.get(&ParamName(s.to_owned().into())),
90-
ParamKind::Utf8 => match s {
91-
"charset" => Some(&ParamValue(Cow::Borrowed("utf8"))),
90+
ParamKind::Map(hm) => hm.get(&name),
91+
ParamKind::Utf8 => match name {
92+
ParamName(Cow::Borrowed("charset")) => Some(&ParamValue(Cow::Borrowed("utf8"))),
9293
_ => None,
9394
},
9495
})
@@ -173,6 +174,24 @@ impl Display for ParamName {
173174
}
174175
}
175176

177+
impl FromStr for ParamName {
178+
type Err = crate::Error;
179+
180+
/// Create a new `HeaderName`.
181+
///
182+
/// This checks it's valid ASCII, and lowercases it.
183+
fn from_str(s: &str) -> Result<Self, Self::Err> {
184+
crate::ensure!(s.is_ascii(), "String slice should be valid ASCII");
185+
Ok(ParamName(Cow::Owned(s.to_ascii_lowercase())))
186+
}
187+
}
188+
189+
impl<'a> From<&'a str> for ParamName {
190+
fn from(value: &'a str) -> Self {
191+
Self::from_str(value).unwrap()
192+
}
193+
}
194+
176195
/// A parameter value.
177196
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
178197
pub struct ParamValue(Cow<'static, str>);

0 commit comments

Comments
 (0)