facet-svg

Crates.iofacet-svg
lib.rsfacet-svg
version0.43.1
created_at2025-12-11 16:53:46.098964+00
updated_at2026-01-23 08:53:31.619265+00
descriptionSVG (Scalable Vector Graphics) serialization for Facet types using facet-xml
homepagehttps://facet.rs
repositoryhttps://github.com/facet-rs/facet
max_upload_size
id1980107
size98,772
Amos Wenger (fasterthanlime)

documentation

README

facet-svg

Coverage Status crates.io documentation MIT/Apache-2.0 licensed Discord

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 facet_svg::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 = facet_svg::from_str(svg_str)?;
assert_eq!(svg.width, Some("100".to_string()));

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

Sponsors

Thanks to all individual sponsors:

GitHub Sponsors Patreon

...along with corporate sponsors:

AWS Zed Depot

...without whom this work could not exist.

Special thanks

The facet logo was drawn by Misiasart.

License

Licensed under either of:

at your option.

Commit count: 3380

cargo fmt