diff --git a/src/selector.rs b/src/selector.rs index e9fb9b28..ecd5f2d7 100644 --- a/src/selector.rs +++ b/src/selector.rs @@ -1,5 +1,6 @@ //! CSS selectors. +use std::convert::TryFrom; use std::fmt; use smallvec::SmallVec; @@ -129,3 +130,33 @@ impl cssparser::ToCss for PseudoElement { dest.write_str("") } } + +impl<'i> TryFrom<&'i str> for Selector { + type Error = cssparser::ParseError<'i, SelectorParseErrorKind<'i>>; + + fn try_from(s: &'i str) -> Result { + Selector::parse(s) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::convert::TryInto; + + #[test] + fn selector_conversions() { + let s = "#testid.testclass"; + let _sel: Selector = s.try_into().unwrap(); + + let s = s.to_owned(); + let _sel: Selector = (*s).try_into().unwrap(); + } + + #[test] + #[should_panic] + fn invalid_selector_conversions() { + let s = ""; + let _sel: Selector = s.try_into().unwrap(); + } +}