Expand description
Plugin SDK for vexy-vsvg — everything you need to build an SVG optimization plugin.
This crate ships 52 ready-to-use plugins and the machinery to write your own.
Each plugin implements the Plugin trait and walks the SVG AST using
the Visitor pattern. The SDK handles registration, parameter parsing,
and CSS selector matching so you can focus on the optimization logic.
§What’s inside
- 52 built-in plugins in the
pluginsmodule — ports of every SVGO plugin, same names, same behavior. registry— pre-populated registry of all plugins, ready to use.selector— CSS selector engine for plugins that target elements by selector.css_matching— matches CSS selectors against AST elements.PluginWithParams— trait for plugins that accept JSON configuration.
§Write a plugin in 4 steps
- Define a struct for your plugin state.
- Implement
Plugin— give it a name, description, andapply()method. - Implement
Visitor— override the hooks that matter (usuallyvisit_element_enter). - Call
walk_documentfromapply()to kick off traversal.
§Example
ⓘ
use vexy_vsvg_plugin_sdk::{Plugin, Visitor, Document, Element, walk_document};
use anyhow::Result;
struct MyPlugin;
impl Plugin for MyPlugin {
fn name(&self) -> &'static str { "myPlugin" }
fn description(&self) -> &'static str { "Does something cool" }
fn apply(&self, doc: &mut Document) -> Result<()> {
let mut visitor = MyVisitor;
walk_document(&mut visitor, doc)?;
Ok(())
}
}
struct MyVisitor;
impl Visitor<'_> for MyVisitor {
fn visit_element_enter(&mut self, element: &mut Element) -> Result<(), vexy_vsvg::VexyError> {
// Modify element here
Ok(())
}
}Modules§
- css_
matching - Advanced CSS selector matching with full tree traversal support.
- plugins
- All 52 SVGO-compatible optimization plugins.
- registry
- Plugin registry factory for Vexy Vsvg’s 52 optimization plugins.
- selector
- CSS selector matching for Vexy Vsvg’s AST.
Structs§
- Config
- Main configuration structure
- Document
- Represents a complete SVG document, including the root element, surrounding nodes, and metadata.
- Element
- Represents an XML element within the SVG document. Memory optimized with IndexMap for attributes (better cache locality than HashMap) and pre-allocated capacity for common use cases.
Enums§
- Node
- An enum representing the different types of nodes that can exist in the SVG document tree.
- Plugin
Config - Plugin configuration
- Vexy
Error
Traits§
- Plugin
- Plugin trait using composition over inheritance
- Plugin
With Params - Extended trait for plugins that accept JSON parameters from
svgo.config.json. - Visitor
- Visitor trait for AST traversal
Functions§
- walk_
document - Walk through a document and call visitor methods
- walk_
element - Walk through an element and call visitor methods
- walk_
node - Walk through a node and call appropriate visitor methods