parseBlueprint function

Blueprint parseBlueprint(
  1. String text,
  2. BlueprintFormat format, {
  3. String origin = 'the blueprint',
})

Parses a blueprint written in format.

A blueprint's format is fixed when it is authored and never converted, so the caller says which one this is rather than the parser sniffing for it. A sniffed format is one that changes when somebody adds a comment.

origin names the document in any failure — a file path, or something like the editor when there is no file. Without it "unexpected character" sends the reader looking in the wrong place.

The parsed blueprint carries its own BlueprintSource, so whatever is saved can be handed back exactly as it was written, comments and all.

Implementation

Blueprint parseBlueprint(
  String text,
  BlueprintFormat format, {
  String origin = 'the blueprint',
}) {
  final decoded = switch (format) {
    BlueprintFormat.yaml => _parseYaml(text, origin),
    BlueprintFormat.json => _parseJson(text, origin),
  };

  return Blueprint.fromJson(
    decoded,
  ).withSource(BlueprintSource(format: format, text: text));
}