| Crates.io | css-structs |
| lib.rs | css-structs |
| version | 1.0.1 |
| created_at | 2025-08-08 05:27:54.312163+00 |
| updated_at | 2025-08-08 06:37:02.667268+00 |
| description | A Rust library for parsing and manipulating CSS stylesheets, rules, and declarations |
| homepage | https://github.com/nikelaz/css-structs |
| repository | https://github.com/nikelaz/css-structs |
| max_upload_size | |
| id | 1786225 |
| size | 54,768 |
A fast and reliable CSS parser for Rust, built with nom parser combinators. Parse CSS stylesheets, rules, declarations, and declaration lists with ease.
Add this to your Cargo.toml:
[dependencies]
css-structs = "1.0.0"
use css_structs::Stylesheet;
let css = r#"
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
.header {
background-color: #333;
color: white;
padding: 1rem;
}
.button {
background: linear-gradient(45deg, #007bff, #0056b3);
border: none !important;
padding: 0.5rem 1rem;
}
"#;
let stylesheet = Stylesheet::from_string(css).unwrap();
println!("Parsed {} CSS rules", stylesheet.rules.len());
// Convert back to CSS string
println!("{}", stylesheet);
use css_structs::CSSRule;
let rule_str = ".container { max-width: 1200px; margin: 0 auto; }";
let rule = CSSRule::from_string(rule_str).unwrap();
println!("Selector: {}", rule.selector);
println!("Declarations: {}", rule.declarations.declarations.len());
use css_structs::CSSDeclarationList;
let declarations = "color: red; margin: 10px; padding: 5px !important";
let list = CSSDeclarationList::from_string(declarations).unwrap();
// Remove a specific declaration
let mut list = list;
list.remove_declaration("color");
println!("{}", list); // "margin: 10px; padding: 5px !important;"
use css_structs::CSSDeclaration;
let decl = CSSDeclaration::from_string("font-size: 16px !important").unwrap();
println!("Property: {}", decl.property);
println!("Value: {}", decl.value);
println!("Important: {}", decl.important.is_some());
use css_structs::{Stylesheet, CSSDeclaration};
let css = r#"
.card {
background: white;
border: 1px solid #ddd;
border-radius: 4px;
padding: 1rem;
}
"#;
let stylesheet = Stylesheet::from_string(css).unwrap();
let rule = &stylesheet.rules[0];
// Access rule components
println!("Selector: {}", rule.selector);
// Iterate through declarations
for declaration in &rule.declarations.declarations {
println!(" {}: {}", declaration.property, declaration.value);
if declaration.important.is_some() {
println!(" (important)");
}
}
// Modify and rebuild
let mut new_declarations = rule.declarations.clone();
new_declarations.declarations.push(
CSSDeclaration::new("box-shadow", "0 2px 4px rgba(0,0,0,0.1)", None)
);
Stylesheet - Represents a complete CSS stylesheet with multiple rulesCSSRule - Represents a single CSS rule (selector + declarations)CSSDeclarationList - Represents a list of CSS declarationsCSSDeclaration - Represents a single CSS property-value pairfrom_string() - Parse from CSS string (available on all types)new() - Create instances programmaticallyremove_declaration() - Remove declarations by property name (CSSDeclarationList)Display trait - Convert back to CSS string format!important declarations-webkit-, -moz-, etc.)@media, @font-face, @keyframes) - not supported yetThe parser returns Result types for graceful error handling:
use css_structs::Stylesheet;
let invalid_css = "body { color: red; margin: 10px"; // missing closing brace
match Stylesheet::from_string(invalid_css) {
Ok(stylesheet) => println!("Parsed successfully!"),
Err(error) => eprintln!("Parse error: {}", error),
}
Run the test suite:
# Run all tests
cargo test
# Run specific test module
cargo test stylesheet::tests