Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type conversion from &str to Selector #39

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/selector.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! CSS selectors.

use std::convert::TryFrom;
use std::fmt;

use smallvec::SmallVec;
Expand Down Expand Up @@ -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<Self, Self::Error> {
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 = "<failing selector>";
let _sel: Selector = s.try_into().unwrap();
}
}