Skip to content

Expose Parse and ToCss traits#140

Merged
devongovett merged 1 commit intomasterfrom
parse-tocss
Apr 6, 2022
Merged

Expose Parse and ToCss traits#140
devongovett merged 1 commit intomasterfrom
parse-tocss

Conversation

@devongovett
Copy link
Member

Part of #5.

This exposes the Parse and ToCss traits, which can be used to parse and serialize individual CSS rules, properties, and values rather than only entire stylesheets. I also added parse_string and to_css_string convenience functions to parse and serialize from a string, without manually constructing a parser/printer from scratch.

By extension, this means the Printer is also exposed. I've refactored it a little to accept an options struct and also to hide internal fields.

Example:

use parcel_css::{
  properties::Property,
  rules::CssRule,
  stylesheet::{ParserOptions, PrinterOptions},
  traits::{Parse, ToCss},
  values::color::CssColor,
};

// Parse and serialize an individual value
let color = CssColor::parse_string("#f0f")?;
color.to_css_string(PrinterOptions::default())?; // => "#f0f"

// Parse a property by name
let property = Property::parse_string("color", "#f0f", ParserOptions::default())?;
property.to_css_string(/* important */ false, PrinterOptions::default())?; // => "color: #f0f"

// Parse a single rule
let rule = CssRule::parse_string(".foo { color: red; }", ParserOptions::default())?;
rule.to_css_string(PrinterOptions::default())?; // => ".foo {\n  color: red;\n}"

cc. @lucacasonato

Copy link
Contributor

@lucacasonato lucacasonato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

@devongovett devongovett merged commit 7c1db82 into master Apr 6, 2022
@devongovett devongovett deleted the parse-tocss branch April 6, 2022 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants