From 22ebd0780ef50ec1625eb03818064f747f1b8368 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Sun, 21 Mar 2021 01:29:10 -0600 Subject: [PATCH] Fix tests on comments and now accept &str and String --- Cargo.toml | 2 +- src/lib.rs | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7cf828f..bf18b86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cssifier" -version = "0.1.2" +version = "0.1.3" authors = ["kirbylife "] license = " GPL-3.0-or-later" readme = "README.md" diff --git a/src/lib.rs b/src/lib.rs index 19daaa6..2acf6bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,19 +12,22 @@ use regex::Regex; /// /// Basic usage: /// ``` -/// cssifier("//a/p") -/// Some("a p") +/// let result = cssifier::cssifier("//a/p"); +/// assert_eq!(result, Some("a p".to_string())); /// -/// cssifier("//a/p[@id='hello']") -/// Some("a b#hello") +/// let result = cssifier::cssifier("//a/p[@id='hello']"); +/// assert_eq!(result, Some("a p#hello".to_string())); /// -/// cssfier("//a/p/[contains(text(), 'hello')]") -/// Some(a b:contains(hello)) +/// let result = cssifier::cssifier("//a/p[contains(text(), 'hello')]"); +/// assert_eq!(result, Some("a p:contains(hello)".to_string())); /// -/// cssifier("*random selector//*") // Invalid selectors throw a empty string (WIP) -/// Some("") +/// let result = cssifier::cssifier("*random selector//*"); // Invalid selectors throw a empty string (WIP) +/// assert_eq!(result, Some("".to_string())); /// ``` -pub fn cssifier(xpath: &'static str) -> Option { +pub fn cssifier>(xpath: S) -> Option { + // Trait to &str + let xpath = xpath.as_ref(); + // Ultra magic regex to parse XPath selectors let reg = Regex::new(r#"(?P(^id\(["']?(?P\s*[\w/:][-/\w\s,:;.]*)["']?\)|(?P