Skip to main content

Crate vexy_vsvg_plugin_sdk

Crate vexy_vsvg_plugin_sdk 

Source
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 plugins module — 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

  1. Define a struct for your plugin state.
  2. Implement Plugin — give it a name, description, and apply() method.
  3. Implement Visitor — override the hooks that matter (usually visit_element_enter).
  4. Call walk_document from apply() 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.
PluginConfig
Plugin configuration
VexyError

Traits§

Plugin
Plugin trait using composition over inheritance
PluginWithParams
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