Rust API Reference
import { Code } from ‘@astrojs/starlight/components’;
The edgesvg Rust crate is the core engine. All other SDKs (Python, Node.js, WASM) are thin wrappers around it.
Installation
Section titled “Installation”[dependencies]edgesvg = "0.1"Or with Cargo:
cargo add edgesvgMain Entry Points
Section titled “Main Entry Points”vectorize_file
Section titled “vectorize_file”Convert an image file to SVG.
use edgesvg::{vectorize_file, VectorizationConfig};
let config = VectorizationConfig::default();let response = vectorize_file("input.png", Some("output.svg"), &config)?;
println!("SSIM: {:.4}", response.report.metrics.ssim);println!("Paths: {}", response.report.metrics.path_count);vectorize_bytes
Section titled “vectorize_bytes”Convert image bytes in memory (used by WASM and FFI bindings).
use edgesvg::{vectorize_bytes, VectorizationConfig};
let image_bytes: Vec<u8> = std::fs::read("input.png")?;let config = VectorizationConfig::default();let response = vectorize_bytes(&image_bytes, &config)?;
let svg_string = response.svg;Configuration
Section titled “Configuration”VectorizationConfig
Section titled “VectorizationConfig”use edgesvg::{VectorizationConfig, VectorizeMethod};
let config = VectorizationConfig { method: VectorizeMethod::Auto, target_ssim: Some(0.995), max_file_size: Some(512 * 1024), // 512 KB output_path: Some("output.svg".into()), json: false, verbose: false, ..Default::default()};| Field | Type | Default | Description |
|---|---|---|---|
method | VectorizeMethod | Auto | Vectorization strategy |
target_ssim | Option<f64> | None | Target SSIM for smart mode |
max_file_size | Option<u64> | None | Max output size in bytes |
output_path | Option<PathBuf> | None | Output SVG path (None = in-memory) |
json | bool | false | Print JSON report to stdout |
verbose | bool | false | Full verbose output |
VectorizeMethod
Section titled “VectorizeMethod”pub enum VectorizeMethod { Auto, Logo, Premium, Smart, Convert,}Parse from string:
let method: VectorizeMethod = "logo".parse()?;Response Types
Section titled “Response Types”VectorizeResponse
Section titled “VectorizeResponse”pub struct VectorizeResponse { pub svg: String, // The generated SVG markup pub report: QualityReport, // Metrics and scoring pub requested_method: VectorizeMethod, pub effective_method: VectorizeMethod, pub fallback_from: Option<VectorizeMethod>, pub decision: Option<String>, // Human-readable explanation (auto mode)}QualityReport
Section titled “QualityReport”pub struct QualityReport { pub metrics: QualityMetrics, pub score: f64, // 0.0 – 1.0 composite score pub grade: String, // "A+", "A", "B", etc. pub summary: String,}QualityMetrics
Section titled “QualityMetrics”pub struct QualityMetrics { pub ssim: f64, pub ssim_perceptual: f64, pub edge_similarity: f64, pub edge_precision: f64, pub edge_recall: f64, pub edge_f1: f64, pub foreground_iou: f64, pub color_similarity: f64, pub fidelity_score: f64, pub delta_e: f64, pub topology_score: f64, pub psnr: f64, pub mae: f64, pub file_size: u64, pub path_count: u32, pub weighted_path_count: f64,}Error Handling
Section titled “Error Handling”All functions return Result<_, edgesvg::Error>:
use edgesvg::Error;
match vectorize_file("input.png", None, &config) { Ok(response) => println!("{}", response.svg), Err(Error::Io(e)) => eprintln!("File error: {e}"), Err(Error::Decode(e)) => eprintln!("Image decode error: {e}"), Err(Error::Vectorize(e)) => eprintln!("Vectorization failed: {e}"), Err(e) => eprintln!("Other error: {e}"),}Examples
Section titled “Examples”Batch processing
Section titled “Batch processing”use edgesvg::{vectorize_file, VectorizationConfig, VectorizeMethod};use std::path::Path;
fn batch_convert(inputs: &[&str], out_dir: &str) -> anyhow::Result<()> { let config = VectorizationConfig { method: VectorizeMethod::Auto, ..Default::default() };
for input in inputs { let stem = Path::new(input).file_stem().unwrap().to_str().unwrap(); let output = format!("{}/{}.svg", out_dir, stem); let response = vectorize_file(input, Some(&output), &config)?; println!("{} → {} (SSIM={:.4})", input, output, response.report.metrics.ssim); } Ok(())}Quality gating
Section titled “Quality gating”fn convert_with_quality_check(path: &str) -> anyhow::Result<()> { let config = VectorizationConfig { method: VectorizeMethod::Smart, target_ssim: Some(0.995), ..Default::default() };
let response = vectorize_file(path, None, &config)?;
if response.report.metrics.ssim < 0.99 { anyhow::bail!("SSIM {:.4} below threshold", response.report.metrics.ssim); }
println!("Grade: {}", response.report.grade); Ok(())}