def enc

bend-schema-lib@0.4.1.0 · core.bend:1480

import bend-schema-lib@0.4.1.0/core.bend as Core
import Base

1480def enc(s: Schema, x: Meaning(s)) -> Raw:
1481  match s x:
1482    case SNat{} n:
1483      RNum{n}
1484    case SNatIn{lo_, hi_} n:
1485      RNum{n}
1486    case SStr{} x:
1487      RStr{x}
1488    case SStrLen{lo_, hi_, s2} v:
1489      enc(s2, v)
1490    case SListLen{lo_, hi_, s2} v:
1491      enc(s2, v)
1492    case SOpt{i} None{}:
1493      RNull{}
1494    case SOpt{i} Some{v}:
1495      enc(i, v)
1496    case SOptional{i} None{}:
1497      RMissing{}  # absent: the host writes no key where this one lands
1498    case SOptional{i} Some{v}:
1499      enc(i, v)
1500    case SList{e} Nil{}:
1501      RNil{}
1502    case SList{+e} h <> t:
1503      RCons{enc(e, h), enc(SList{e}, t)}
1504    case SField{n, fs, rest} Both{a, b}:
1505      RKey{n, enc(fs, a), enc(rest, b)}
1506    case SEnd{} Unit{}:
1507      REnd{}
1508    case SRule{s2, tag} v:
1509      enc(s2, v)
1510    case SStrict{s2} v:
1511      enc(s2, v)
1512    case STagged{k, n, cs, rest} Inl{a}:
1513      RKey{k, RStr{n}, enc(cs, a)}
1514    case STagged{k, n, cs, rest} Inr{b}:
1515      enc(rest, b)
1516    case STagEnd{k} e:
1517      Empty.absurd(Raw, e)
1518    case STrue{} Unit{}:
1519      RBool{True{}}
1520    case SBool{} b:
1521      RBool{b}
1522    case SEnum{names} x:
1523      RStr{x}
1524    case SVariant{n, vs, rest} Inl{a}:
1525      RKey{n, enc(vs, a), REnd{}}
1526    case SVariant{n, vs, rest} Inr{b}:
1527      enc(rest, b)
1528    case SVEnd{} e:
1529      Empty.absurd(Raw, e)
1530    case STuple{ts, rest} Both{a, b}:
1531      RCons{enc(ts, a), enc(rest, b)}
1532    case STEnd{} Unit{}:
1533      RNil{}
1534    case SJson{} value:
1535      RJson{value}
1536    case SInt{} i:
1537      int_raw(i)
1538    case SIntIn{lo_, hi_} i:
1539      int_raw(i)
1540    case SEither{l, r} Inl{a}:
1541      enc(l, a)
1542    case SEither{l, r} Inr{b}:
1543      enc(r, b)

Uses (11)

Used by (1)