Skip to main content

parse

Function parse 

Source
pub fn parse(json: &str) -> Result<Diagram, ParseError>
Expand description

Reads a description and hands back the diagram it holds, and nothing else.

The whole of this crate’s public surface for reading, in one call: there is no value to build, hold or convert afterwards, so no method can be called in the wrong order and none leaves a diagram half-converted. The window is not here — which part of a diagram to draw is the caller’s question, and a caller that wants one names it on the command line or on the line that opens a render marker.

§Errors

ParseError when the text is not a description this crate reads — a field left out, a kind it does not name, an identity written as free text, a next_id of zero. The message names the field it was refused on and is the one serde_json produced.

§Examples

let diagram = parse(
    r#"{
        "next_id": 2,
        "shapes": [ { "kind": "box", "id": 1, "at": { "x": 0, "y": 0 },
                      "size": { "width": 4, "height": 3 }, "stroke": "light" } ]
    }"#,
)
.expect("a well-formed description");

assert!(diagram
    .get(monospace_diagram::ShapeId::new(std::num::NonZeroU16::new(1).expect("one is nonzero")))
    .is_some());