8 releases

0.1.0 Jan 16, 2024
0.0.7 Nov 11, 2023

#5 in #concrete

Download history 2/week @ 2024-01-06 119/week @ 2024-01-13 116/week @ 2024-02-03 93/week @ 2024-02-10 2/week @ 2024-02-17 18/week @ 2024-02-24 16/week @ 2024-03-02 21/week @ 2024-03-09 18/week @ 2024-03-16 10/week @ 2024-03-23 115/week @ 2024-03-30

165 downloads per month

MIT license

94KB
2.5K SLoC

svg-path-cst

Crates.io Tests License

Concrete Syntax Tree parser for SVG v1.1 paths.

Install

cargo add svg-path-cst

Usage

use svg_path_cst::{
    svg_path_cst,
    SVGPathCSTNode,
    SVGPathSegment,
    SVGPathCommand,
    WSP,
    Sign,
    SyntaxError,
};

let cst: Result<Vec<SVGPathCSTNode>, SyntaxError> = svg_path_cst(b"m0 0 L1,-1");

let expected_cst: Vec<SVGPathCSTNode> = vec![
    SVGPathCSTNode::Segment(SVGPathSegment{
        command: &SVGPathCommand::MovetoLower,
        args: vec![0.0, 0.0],
        start: 0,
        end: 4,
        chained: false,
        chain_start: 0,
        chain_end: 4,
        cst: vec![
            SVGPathCSTNode::Command(&SVGPathCommand::MovetoLower),
            SVGPathCSTNode::Number{
                raw_number: "0".to_string(),
                value: 0.0,
                start: 1,
                end: 2,
            },
            SVGPathCSTNode::Whitespace{
                wsp: &WSP::Space,
                start: 2,
                end: 3,
            },
            SVGPathCSTNode::Number{
                raw_number: "0".to_string(),
                value: 0.0,
                start: 3,
                end: 4,
            },
        ],
    }),
    SVGPathCSTNode::Whitespace{
        wsp: &WSP::Space,
        start: 4,
        end: 5,
    },
    SVGPathCSTNode::Segment(SVGPathSegment{
        command: &SVGPathCommand::LinetoUpper,
        args: vec![1.0, -1.0],
        start: 5,
        end: 10,
        chained: false,
        chain_start: 5,
        chain_end: 10,
        cst: vec![
            SVGPathCSTNode::Command(&SVGPathCommand::LinetoUpper),
            SVGPathCSTNode::Number{
                raw_number: "1".to_string(),
                value: 1.0,
                start: 6,
                end: 7,
            },
            SVGPathCSTNode::Comma{ start: 7 },
            SVGPathCSTNode::Sign{ sign: &Sign::Minus, start: 8 },
            SVGPathCSTNode::Number{
                raw_number: "1".to_string(),
                value: 1.0,
                start: 9,
                end: 10,
            },
        ],
    }),
];

assert_eq!(cst, Ok(expected_cst));

Compatibility

This crate is compatible with SVG v1.1 paths, as defined in the W3C SVG 1.1 and no_std environments.

Dependencies

~0.4–1MB
~21K SLoC