Skip to content

std.cbor

rfc 8949; writer emits preferred (shortest) forms, reader allocates nothing

pub CborError :: errors {
truncated,
malformed,
bad_text,
too_deep,
}
  • truncated: the bytes end in the middle of an item
  • malformed: invalid header, or a stray break
  • bad_text: a text string that is not UTF-8
  • too_deep: skip hit 1000 nesting levels
pub Kind :: enum {
uint,
negint,
bytes,
text,
array,
map,
tag,
simple,
boolean,
null_value,
undefined,
float,
end,
}
  • uint: value
  • negint: -1 - value
  • bytes: data
  • text: data, UTF-8
  • array: value items follow
  • map: value pairs follow, each a key and its value
  • tag: the tag value; the item it tags follows
  • simple: unassigned simple value, number in value
  • boolean: value 1 for true, 0 for false
  • float: number
  • end: break closing an indefinite item
pub Item :: struct {
kind: Kind,
value: u64,
data: []const u8,
number: f64,
indefinite: bool,
}

from Reader.next; data slices the input

pub as_i64 :: fn(self: *Self) -> ?i64

uint/negint as i64; null if it doesn’t fit

pub Out :: struct {
w: *Writer,
failed: bool,
}

caller keeps item counts right; end closes indefinites; write errors show in finish

pub uint :: fn(self: *Self, v: u64)
pub int :: fn(self: *Self, v: i64)
pub bytes :: fn(self: *Self, b: []const u8)
pub text :: fn(self: *Self, s: []const u8)

utf-8 not checked

pub begin_array :: fn(self: *Self, n: usize)

write n items next

pub begin_map :: fn(self: *Self, n: usize)

write n key/value pairs next

pub begin_array_indefinite :: fn(self: *Self)

write the items, then end

pub begin_map_indefinite :: fn(self: *Self)

write the pairs, then end

pub end :: fn(self: *Self)

closes an indefinite array or map

pub tag :: fn(self: *Self, t: u64)

tags the next item (1 = epoch seconds, 32 = uri, …)

pub boolean :: fn(self: *Self, v: bool)
pub null_value :: fn(self: *Self)
pub undefined :: fn(self: *Self)
pub simple :: fn(self: *Self, v: u8)

0..19 or 32..255; anything else writes nothing

pub float :: fn(self: *Self, v: f64)

shortest exact width; NaN as half

pub raw :: fn(self: *Self, b: []const u8)

pre-encoded cbor, as is

pub finish :: fn(self: *Self) -> void ! IoError

.write_failed if any write failed

pub writer :: fn(w: *Writer) -> Out
pub Reader :: struct {
data: []const u8,
pos: usize,
}

one item at a time; a container’s items follow its header

pub at_end :: fn(self: *Self) -> bool
pub next :: fn(self: *Self) -> Item ! CborError
pub skip :: fn(self: *Self) -> void ! CborError

skips the next item and everything in it

pub skip_contents :: fn(self: *Self, it: Item) -> void ! CborError

skips what it (just read) holds: items, pairs, tagged item, string parts

pub reader :: fn(data: []const u8) -> Reader