facet-svg
Provides strongly-typed SVG parsing for Facet types using facet-format-xml.
Why facet-svg?
SVG is ubiquitous in diagram generation—tools like pikchr, Graphviz, Mermaid, and countless design applications output SVG as their primary format. However, SVG parsing in Rust typically requires either:
- Generic XML libraries that lose type information and require error-prone casting
- Special-purpose SVG libraries that add heavyweight dependencies and force you into their abstractions
- Manual string parsing that's fragile and doesn't scale
facet-svg solves this by providing strongly-typed, compile-time-safe SVG structures derived from Facet's reflection system. You get:
- Type Safety: The Rust compiler catches mismatches between your SVG structure and actual data
- Zero Dependencies: Built on facet-format-xml, which uses only quick-xml for parsing
- Graceful Degradation: Unknown elements are safely ignored, so SVGs from any tool work without modification
- Structured Access: Navigate SVG geometry programmatically with full IDE support and type checking
This makes facet-svg ideal for:
- Processing SVG output from diagram tools in build pipelines
- Extracting geometric data (paths, shapes, text) for analysis or transformation
- Building Rust applications that need to consume or validate SVGs at compile time
Difference from facet-svg
This crate uses facet-format-xml instead of facet-xml. The facet-format-* crates are the
next-generation format infrastructure for Facet, featuring a unified parser/serializer architecture.
Supported Elements
The following SVG elements are fully supported for parsing and type-safe access:
- Shapes:
<rect>,<circle>,<ellipse>,<line>,<path>,<polygon>,<polyline> - Text:
<text>with styling attributes - Grouping:
<g>with transform support,<defs>,<style>,<symbol> - References:
<use>for referencing defined elements - Media:
<image>for embedded or linked images - Metadata:
<title>,<desc>for accessibility and documentation
Unsupported elements (such as <filter>, <marker>, <tspan>, and specialized SVG filters) are gracefully ignored during parsing, allowing real-world SVG files to be processed without errors.
Basic Usage
use Svg;
let svg_str = r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<rect x="10" y="10" width="80" height="80" fill="blue"/>
</svg>"#;
let svg: Svg = from_str?;
assert_eq!;
Features
- Type-safe parsing: Strongly-typed SVG structures via Facet's derive macro
- Attribute support: Full attribute parsing for colors, dimensions, transforms, and styling
- Namespace handling: Proper SVG namespace support
- Graceful degradation: Unknown elements are safely ignored
- Path parsing: Complex SVG path data with multiple commands
Use Cases
- Parsing SVG output from diagram tools (like pikchr, Graphviz)
- Extracting geometric data from SVG files
- Type-safe SVG manipulation in Rust applications
LLM contribution policy
Sponsors
Thanks to all individual sponsors:
...along with corporate sponsors:
...without whom this work could not exist.
Special thanks
The facet logo was drawn by Misiasart.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.