Fragment::parse(): New convenience function

To avoid going through Href::with_fragment()
parent 9969002b
......@@ -93,6 +93,16 @@ impl Fragment {
Fragment(uri, fragment)
}
pub fn parse(href: &str) -> Result<Fragment, HrefError> {
let href = Href::with_fragment(href)?;
if let Href::WithFragment(f) = href {
Ok(f)
} else {
unreachable!();
}
}
pub fn uri(&self) -> Option<&str> {
self.0.as_ref().map(|s| s.as_str())
}
......@@ -301,4 +311,19 @@ mod tests {
assert_eq!(Href::with_fragment("uri"), Err(HrefError::FragmentRequired));
}
#[test]
fn fragment_parse() {
assert_eq!(
Fragment::parse("#foo").unwrap(),
Fragment::new(None, "foo".to_string())
);
assert_eq!(
Fragment::parse("uri#foo").unwrap(),
Fragment::new(Some("uri".to_string()), "foo".to_string())
);
assert_eq!(Fragment::parse("uri"), Err(HrefError::FragmentRequired));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment