def canon

yaml-for-bend@0.1.1.0 · value.bend:108

import yaml-for-bend@0.1.1.0/value.bend as Value
import Base

108# A value as text that two values share only when they are the same, to
109# look keys up by. A string is its length and text; "[" goes before each
110# item of a sequence and "]" after them, and "{" before each entry of a
111# mapping and "}" after them.
112def canon(v: Yaml) -> String:
113  match v:
114    case Null{}:
115      "~"
116    case Boolean{on}:
117      Bool.pick(String, on, "t", "f")
118    case Int{t}:
119      "i" ++ t ++ ";"
120    case Float{t}:
121      "d" ++ t ++ ";"
122    case Str{+t}:
123      "s" ++ Nat.show(String.length(t)) ++ ":" ++ t
124    case Seq{xs}:
125      match xs:
126        case Nil{}:
127          "]"
128        case Con{x, rest}:
129          "[" ++ canon(x) ++ canon(Seq{rest})
130    case Mapping{es}:
131      match es:
132        case Nil{}:
133          "}"
134        case Con{Entry{k, x}, rest}:
135          "{" ++ canon(k) ++ canon(x) ++ canon(Mapping{rest})
136    case Tagged{+tag, x}:
137      "!" ++ Nat.show(String.length(tag)) ++ ":" ++ tag ++ canon(x)

Uses (8)

Used by (3)