CSS Structs
A fast and reliable CSS parser for Rust, built with nom parser combinators. Parse CSS stylesheets, rules, declarations, and declaration lists with ease.
Features
- 🚀 Fast: Built with nom parser combinators for excellent performance
- 🔧 Flexible: Parse complete stylesheets or individual components
- 🔄 Roundtrip: Parse CSS and convert back to string representation
Quick Start
Add this to your Cargo.toml
:
[]
= "1.0.0"
Usage
Parse a Complete Stylesheet
use 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 = from_string.unwrap;
println!;
// Convert back to CSS string
println!;
Parse Individual CSS Rules
use CSSRule;
let rule_str = ".container { max-width: 1200px; margin: 0 auto; }";
let rule = from_string.unwrap;
println!;
println!;
Parse Declaration Lists
use CSSDeclarationList;
let declarations = "color: red; margin: 10px; padding: 5px !important";
let list = from_string.unwrap;
// Remove a specific declaration
let mut list = list;
list.remove_declaration;
println!; // "margin: 10px; padding: 5px !important;"
Parse Individual Declarations
use CSSDeclaration;
let decl = from_string.unwrap;
println!;
println!;
println!;
Working with Parsed Data
use ;
let css = r#"
.card {
background: white;
border: 1px solid #ddd;
border-radius: 4px;
padding: 1rem;
}
"#;
let stylesheet = from_string.unwrap;
let rule = &stylesheet.rules;
// Access rule components
println!;
// Iterate through declarations
for declaration in &rule.declarations.declarations
// Modify and rebuild
let mut new_declarations = rule.declarations.clone;
new_declarations.declarations.push;
API Overview
Core Types
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 pair
Key Methods
from_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
CSS Features Supported
- ✅ Basic selectors (element, class, ID, universal)
- ✅ Complex selectors (descendant, child, sibling, pseudo-classes)
- ✅ All CSS properties and values
- ✅
!important
declarations - ✅ Vendor prefixes (
-webkit-
,-moz-
, etc.) - ✅ CSS custom properties (CSS variables)
- ✅ Whitespace handling and normalization
- ❌ Comments - not supported yet
- ❌ At-rules (e.g.,
@media
,@font-face
,@keyframes
) - not supported yet - ❌ Nested rules - not supported yet
Error Handling
The parser returns Result
types for graceful error handling:
use Stylesheet;
let invalid_css = "body { color: red; margin: 10px"; // missing closing brace
match from_string
Testing
Run the test suite:
# Run all tests
# Run specific test module