pub struct Css { /* private fields */ }Expand description
A reusable CSS parser.
Building one installs the grammar on a new engine, so keep an instance
around rather than making a fresh one per document — tests/perf.rs
pins that reuse is worth it. parse does this for you for the default
options.
use tabnas_css::{Css, Options};
let css = Css::with_options(Options { lowercase_properties: true, ..Options::default() });
let ast = css.parse("A { COLOR: Red }").unwrap();
assert!(ast.to_json().contains(r#""property":"color""#));Implementations§
Source§impl Css
impl Css
Sourcepub fn new() -> Css
pub fn new() -> Css
A parser with the default options (lowercase_properties: false,
position: false).
Sourcepub fn with_options(options: Options) -> Css
pub fn with_options(options: Options) -> Css
A parser with the given options.
Examples found in repository?
examples/parse.rs (line 44)
22fn main() {
23 let mut options = Options::default();
24 let mut lines = false;
25 for arg in std::env::args().skip(1) {
26 match arg.as_str() {
27 "--position" => options.position = true,
28 "--lowercase-properties" => options.lowercase_properties = true,
29 "--lines" => lines = true,
30 other => {
31 eprintln!("parse: unknown argument {other:?}");
32 eprintln!("usage: parse [--position] [--lowercase-properties] [--lines]");
33 std::process::exit(2);
34 }
35 }
36 }
37
38 let mut input = String::new();
39 if std::io::stdin().read_to_string(&mut input).is_err() {
40 eprintln!("parse: stdin is not valid UTF-8");
41 std::process::exit(2);
42 }
43
44 let css = Css::with_options(options);
45 if lines {
46 // Split on '\n' only: `str::lines` also strips a trailing '\r',
47 // which would quietly drop the very characters a carriage-return
48 // probe is there to test.
49 let mut rows: Vec<&str> = input.split('\n').collect();
50 if Some(&"") == rows.last() {
51 rows.pop();
52 }
53 for line in rows {
54 println!("{}", report(&css, &decode(line)));
55 }
56 } else {
57 println!("{}", report(&css, &input));
58 }
59}Sourcepub fn tabnas(&self) -> &Tabnas
pub fn tabnas(&self) -> &Tabnas
The engine this parser runs: jsonic with the plugin installed. Its
own parse returns the tree form plugin describes.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Css
impl !RefUnwindSafe for Css
impl !UnwindSafe for Css
impl Send for Css
impl Sync for Css
impl Unpin for Css
impl UnsafeUnpin for Css
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more