Skip to main content

Css

Struct Css 

Source
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

Source

pub fn new() -> Css

A parser with the default options (lowercase_properties: false, position: false).

Source

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}
Source

pub fn options(&self) -> Options

The options this parser was built with.

Source

pub fn tabnas(&self) -> &Tabnas

The engine this parser runs: jsonic with the plugin installed. Its own parse returns the tree form plugin describes.

Source

pub fn parse(&self, src: &str) -> Result<Value, Error>

Parse a CSS document into its AST.

A zero-length source yields an empty stylesheet, as reworkcss does. Any non-empty source — even one that is only whitespace, or only a comment — also yields a stylesheet node.

Examples found in repository?
examples/parse.rs (line 62)
61fn report(css: &Css, src: &str) -> String {
62    match css.parse(src) {
63        Ok(ast) => ast.to_json(),
64        Err(err) => format!("ERROR:{}", err.code),
65    }
66}

Trait Implementations§

Source§

impl Debug for Css

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Css

Source§

fn default() -> Css

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.