GizStack
View .mdOpen in ChatGPTOpen in Claude

gizlang:cbor

CBOR (RFC 8949) as a value, for the binary structures of WebAuthn and COSE keys. It reads any definite length and writes the shortest one: integers within i64, byte and text strings, arrays, maps, false, true and null. Tags, floats, other simple values, indefinite lengths and integers past i64 are errors: no protocol this module serves needs them, and a reader that refuses them cannot misread them.

gizlang
import * as Cbor from "gizlang:cbor";

Types

Value

gizlang
type Value = | { kind: "int"; value: i64; } | { kind: "bytes"; value: bytes; } | { kind: "text"; value: string; } | { kind: "array"; items: Vec<Value>; } | { kind: "map"; keys: Vec<Value>; values: Vec<Value>; } | { kind: "bool"; value: bool; } | { kind: "null"; }

A CBOR item. A map keeps its keys and values in order, as two Vecs of equal length; a repeated key keeps both.

  • int
  • bytes
  • text
  • array
  • map
  • bool
  • null

CborError

gizlang
type CborError = { offset: i64; message: string; }

Where the bytes stop being CBOR, in bytes, and why.

  • offset: i64
  • message: string

Read

gizlang
type Read = { value: Value; next: i64; }

An item read, and the position after it.

  • value: gizlang_cbor.Value
  • next: i64

Functions

read

gizlang
function read(data: bytes, at: i64): Result<Read, CborError>

The item at AT in DATA and the position after it: for items that sit back to back, as the key in authenticator data.

Effects: fail.

decode

gizlang
function decode(data: bytes): Value throws CborError

The one item that DATA holds, and nothing after it.

Effects: fail.

encode

gizlang
function encode(value: Value): bytes

The CBOR bytes of VALUE: definite lengths, each in its shortest form.

Effects: fail.

lookup

gizlang
function lookup(container: Value, key: Value): Option<Value>

The item of MAP at KEY (an int, bytes or text item), or undefined. A repeated key gives its first value.

Effects: fail.

bytesOf

gizlang
function bytesOf(v: Value): Option<bytes>

The bytes of a byte string item, or undefined.

textOf

gizlang
function textOf(v: Value): Option<string>

The text of a text string item, or undefined.

intOf

gizlang
function intOf(v: Value): Option<i64>

The integer of an integer item, or undefined.