Skip to content

stdlib.serialize

Serialization Library

Generated from v0.60.1. 7 source files, 99 documented symbols.

csv.xi

fn csv_parse(text: Str) -> Result[Vec[Vec[Str]], Str]

Parse RFC 4180 CSV with the default comma delimiter.

fn csv_parse_with(text: Str, delimiter: UInt8) -> Result[Vec[Vec[Str]], Str]

Parse RFC 4180 CSV with an explicit delimiter byte.

  • Precondition: delimiter != _CSV_QUOTE
  • Precondition: delimiter != _CSV_CR && delimiter != _CSV_LF
fn csv_write_row(fields: &Vec[Str]) -> Str

Serialize one record (no trailing terminator), quoting as needed.

fn csv_write(rows: &Vec[Vec[Str]]) -> Str

Serialize records with CRLF terminators per RFC 4180.




endian.xi

fn write_u16_le(out: &mut Vec[UInt8], v: UInt16)

Append v as two little-endian bytes (LSB first). Complexity: O(1).

fn write_u32_le(out: &mut Vec[UInt8], v: UInt32)

Append v as four little-endian bytes (LSB first). Complexity: O(1).

fn write_u64_le(out: &mut Vec[UInt8], v: UInt64)

Append v as eight little-endian bytes (LSB first). Complexity: O(1).

fn write_u16_be(out: &mut Vec[UInt8], v: UInt16)

Append v as two big-endian bytes (MSB first). Complexity: O(1).

fn write_u32_be(out: &mut Vec[UInt8], v: UInt32)

Append v as four big-endian bytes (MSB first). Complexity: O(1).

fn write_u64_be(out: &mut Vec[UInt8], v: UInt64)

Append v as eight big-endian bytes (MSB first). Complexity: O(1).

fn read_u16_le(data: &Vec[UInt8], pos: Int) -> UInt16

Read a little-endian UInt16 at pos; returns 0 when fewer than two bytes remain. Callers must verify pos + 2 <= data.len(). Complexity: O(1).

fn read_u32_le(data: &Vec[UInt8], pos: Int) -> UInt32

Read a little-endian UInt32 at pos; returns 0 when fewer than four bytes remain. Callers must verify pos + 4 <= data.len(). Complexity: O(1).

fn read_u64_le(data: &Vec[UInt8], pos: Int) -> UInt64

Read a little-endian UInt64 at pos; returns 0 when fewer than eight bytes remain. Callers must verify pos + 8 <= data.len(). Complexity: O(1).

fn read_u16_be(data: &Vec[UInt8], pos: Int) -> UInt16

Read a big-endian UInt16 at pos; returns 0 when fewer than two bytes remain. Callers must verify pos + 2 <= data.len(). Complexity: O(1).

fn read_u32_be(data: &Vec[UInt8], pos: Int) -> UInt32

Read a big-endian UInt32 at pos; returns 0 when fewer than four bytes remain. Callers must verify pos + 4 <= data.len(). Complexity: O(1).

fn read_u64_be(data: &Vec[UInt8], pos: Int) -> UInt64

Read a big-endian UInt64 at pos; returns 0 when fewer than eight bytes remain. Callers must verify pos + 8 <= data.len(). Complexity: O(1).

fn write_i64_le(out: &mut Vec[UInt8], v: Int)

Append v as eight little-endian two's-complement bytes. Complexity: O(1).

fn read_i64_le(data: &Vec[UInt8], pos: Int) -> Int

Read a little-endian signed 64-bit value at pos; returns 0 when fewer than eight bytes remain. Callers must verify pos + 8 <= data.len(). Complexity: O(1).

fn write_f64_le(out: &mut Vec[UInt8], v: Float64)

Append the IEEE-754 bit pattern of v little-endian (host byte order, which is little-endian on all supported targets). Complexity: O(1).

  • Precondition: true
fn read_f64_le(data: &Vec[UInt8], pos: Int) -> Float64

Read a little-endian IEEE-754 double at pos; returns 0.0 when fewer than eight bytes remain. Callers must verify pos + 8 <= data.len(). Complexity: O(1).




json.xi

enum JsonValue

NOTE: The parent module xiom.serialize is always present in the compile unit when any xiom.serialize.* submodule is used, and it defines its own JsonValue type (Object: Map[Str, JsonValue]). The compiler unifies the two same-named types, so this module MUST use the identical enum shape (Map-backed objects) or LLVM lowering fails.

  • Null
  • Bool(value: Bool)
  • Number(value: Float64)
  • String(value: Str)
  • Array(items: Vec[JsonValue])
  • Object(entries: Map[Str, JsonValue])
fn json_parse(s: Str) -> Result[JsonValue, Str]

Parse s into a JsonValue tree. Returns Err with a descriptive message on malformed input or trailing garbage. Complexity: O(n), n = input length.

NOTE: the name json_parse collides with the parent module's own json_parse and direct calls can misresolve under the current compiler (BUG 25 #1). The parsing logic lives in the uniquely-named helpers above.

fn json_stringify(v: JsonValue) -> Str

Serialize v as compact JSON (no insignificant whitespace). Complexity: O(n), n = number of nodes.

fn json_pretty(v: JsonValue) -> Str

Serialize v as indented JSON (2 spaces per level). Complexity: O(n), n = number of nodes.

fn json_get(v: JsonValue, key: Str) -> Option[JsonValue]

The value under key, if v is an object and the key is present. Complexity: O(k), k = number of keys.

fn json_get_path(v: JsonValue, path: &Vec[Str]) -> Option[JsonValue]

The value at a key path (e.g. ["user", "address", "city"]), navigating objects by string key and arrays by numeric string index. Complexity: O(d * k), d = path depth, k = keys per object.

fn json_set(v: JsonValue, key: Str, value: JsonValue) -> JsonValue

A copy of v (an object) with key set to value. An existing key is replaced; a missing key is appended. Complexity: O(k), k = number of keys.

fn json_array_push(v: JsonValue, item: JsonValue) -> JsonValue

A copy of the array v with item appended. Non-array values produce an array containing just item. Complexity: O(k), k = number of items.

fn json_object_new() -> JsonValue

A new empty JSON object. Complexity: O(1).

fn json_array_new() -> JsonValue

A new empty JSON array. Complexity: O(1).

fn json_number(f: Float64) -> JsonValue

Wrap a float as a JSON number. Complexity: O(1).

fn json_string(s: Str) -> JsonValue

Wrap a string as a JSON string. Complexity: O(1).

fn json_bool(b: Bool) -> JsonValue

Wrap a bool as a JSON bool. Complexity: O(1).

fn json_null() -> JsonValue

The JSON null value. Complexity: O(1).

fn json_type(v: JsonValue) -> Str

The type name of v: object, array, string, number, bool, or null. Complexity: O(1).

fn json_escape(s: Str) -> Str

Escape s for embedding inside a JSON string literal (without the surrounding quotes). Handles \" \ \/ \b \f \n \r \t and control chars. Complexity: O(n), n = string length.

NOTE: the name json_escape collides with the parent module's own json_escape; the logic lives in the uniquely-named _json_escape_impl.




serialize.xi

type SerializeError

=== Error type ===

Field Type
kind Int
message Str
path Str
line Int
col Int

Derives: Eq, Clone, Display

fn format_error() -> Str

Error kinds: 0=Unknown, 1=InvalidFormat, 2=MissingField, 3=TypeMismatch, 4=ContractViolation, 5=UnsupportedType

fn detect_format(data: &Vec[UInt8]) -> Str

=== Format detection ===

  • Postcondition: result.len() > 0

fn is_valid_json(data: Str) -> Bool

True when the text parses as JSON.

  • Precondition: data.len() >= 0

fn is_valid_bytes(data: &Vec[UInt8]) -> Bool

True when the bytes contain valid UTF-8 JSON.

fn json_string(s: Str) -> Str

=== JSON helpers ===

fn json_number(n: Float64) -> Str

JSON number literal for the value.

fn json_bool(b: Bool) -> Str

JSON literal "true"/"false".

fn json_null() -> Str

JSON literal "null".

fn json_array(items: Vec[Str]) -> Str

JSON array from pre-rendered element strings.

fn json_object(pairs: Vec[(Str, Str)]) -> Str

JSON object from pre-rendered key/value pairs.

fn to_json[T](value: T) -> Result[Str, SerializeError]

Serialize a value to JSON; Err on failure.

fn from_json[T](s: Str) -> Result[T, SerializeError]

Deserialize a value from JSON; Err on failure.

fn json_parse(data: Str) -> Result[JsonValue, SerializeError]

Parse JSON text into a JsonValue; Err with position info.

  • Precondition: data.len() >= 0
  • Postcondition: true
  • Postcondition: true

fn parse_json(s: Str) -> Result[JsonValue, SerializeError]

Alias of json_parse (parse JSON text).

enum JsonValue

Parsed JSON value (null/bool/number/string/array/object).

  • Null
  • Bool(value: Bool)
  • Number(value: Float64)
  • String(value: Str)
  • Array(items: Vec[JsonValue])
  • Object(entries: Map[Str, JsonValue])

fn to_str(self: Self) -> Str

=== JsonValue methods ===

fn get(self: Self, key: Str) -> Option[JsonValue]

Object member by key, or None.

fn index(self: Self, i: Int) -> Option[JsonValue]

Array element by index, or None.

fn little_endian() -> Bool

=== Binary helpers ===

fn big_endian() -> Bool

True when the target is big-endian (false on x86_64).

fn json_escape(s: Str) -> Str

JSON-escapes a string (without surrounding quotes). Handles \, ", \n, \r, \t, \b, \f. Complexity: O(n), n = string length.

fn json_unescape(s: Str) -> Result[Str, Str]

Unescapes a JSON-escaped string (without surrounding quotes). Handles \, \", \/, \b, \f, \n, \r, \t, \uNNNN. Complexity: O(n), n = string length.

fn json_minify(s: Str) -> Result[Str, Str]

Strips whitespace from JSON outside of strings. Complexity: O(n), n = input length.

fn json_pretty(s: Str) -> Result[Str, Str]

Pretty-prints JSON with 2-space indentation. Uses a simple tokenizer-based approach that tracks nesting depth. Complexity: O(n), n = input length.

fn json_get_path(json: Str, path: Str) -> Option[Str]

Navigates a JSON string using a dot-notation path (e.g. "a.b.0"). Returns the value at the path as a string, or None if not found. Complexity: O(n * p), n = JSON size, p = path depth.

fn json_type_of(s: Str) -> Str

Returns the JSON type of a string: "object", "array", "string", "number", "bool", "null", or "invalid". Complexity: O(1) -- reads only the first non-whitespace character.

fn varint_encode(value: Int) -> Vec[UInt8]

Encodes an integer using unsigned LEB128 (Little Endian Base 128). Each byte uses 7 bits for data and the MSB as continuation flag. Complexity: O(log128(n)).

fn varint_decode(data: &Vec[UInt8], pos: Int) -> Result[Int, Str]

Decodes an unsigned LEB128 integer from a byte slice starting at pos. Returns the decoded value. The caller advances pos by varint_encoded_len. Complexity: O(log128(n)).

fn varint_decode_at(data: &Vec[UInt8], pos: Int) -> Result[Int, Str]

Decodes a LEB128 integer and returns the value with its encoded byte length. The caller can advance by the returned length. Complexity: O(log128(n)).

fn varint_encoded_len(data: &Vec[UInt8], pos: Int) -> Int

Returns the number of bytes consumed by a LEB128-encoded integer. Scans continuation bits. Complexity: O(log128(n)).

fn bytes_to_hex_str(data: &Vec[UInt8]) -> Str

Converts bytes to a hex string. Delegates to xiom.encoding.hex_encode. Complexity: O(n), n = data length.

fn hex_str_to_bytes(s: Str) -> Result[Vec[UInt8], Str]

Converts a hex string to bytes. Delegates to xiom.encoding.hex_decode. Complexity: O(n), n = string length.



toml.xi

enum TomlValue

TOML v1 reader (the subset a package manifest needs).

Supported: comments; bare and quoted keys; [table] and [a.b] headers; basic strings with the five common escapes; literal strings; integers (decimal, optional sign, _ separators); floats; booleans; arrays of strings/ints/floats. Keys are stored section-qualified with '.' ("package.name"), in file order.

NOT in v1: dates/times, multi-line strings, inline tables, arrays-of-tables, dotted keys in assignment position. The WRITER (toml_write) emits the same subset: flat section-qualified keys, root keys first, [section] blocks in first-appearance order, the five common escapes. Errors carry the 1-based line number.

  • TStr(_0: Str)
  • TInt(_0: Int)
  • TFloat(_0: Float64)
  • TBool(_0: Bool)
  • TStrArray(_0: Vec[Str])
  • TIntArray(_0: Vec[Int])
  • TFloatArray(_0: Vec[Float64])
type TomlTable

Parsed TOML table (ordered key -> value map).

Field Type
keys Vec[Str]
values Vec[TomlValue]
fn toml_parse(text: Str) -> Result[TomlTable, Str]

Parse a TOML v1 subset document; keys are section-qualified ("a.b"). Errors carry the 1-based source line.

fn toml_get(t: &TomlTable, key: Str) -> Option[TomlValue]

Value for key, or None when missing.

fn toml_has(t: &TomlTable, key: Str) -> Bool

True when key exists.

fn toml_get_str(t: &TomlTable, key: Str) -> Option[Str]

String value for key, or None when missing or of another type.

fn toml_get_int(t: &TomlTable, key: Str) -> Option[Int]

Int value for key, or None when missing or of another type.

fn toml_get_float(t: &TomlTable, key: Str) -> Option[Float64]

Float value for key, or None when missing or of another type.

fn toml_get_bool(t: &TomlTable, key: Str) -> Option[Bool]

Bool value for key, or None when missing or of another type.

fn toml_get_str_array(t: &TomlTable, key: Str) -> Option[Vec[Str]]

String-array value for key, or None when missing or of another type.

fn toml_get_int_array(t: &TomlTable, key: Str) -> Option[Vec[Int]]

Int-array value for key, or None when missing or of another type.

fn toml_keys(t: &TomlTable) -> Vec[Str]

All keys in file order.

fn toml_write(t: &TomlTable) -> Str

Render t as TOML text for the v1 subset: root keys first (file order), then one [section] block per dotted-key prefix, in first-appearance order. Round-trips through toml_parse except for non-finite floats (written as nan/inf, which the v1 reader does not accept back). Complexity: O(n^2) worst case in the number of keys (section grouping).




varint.xi

fn varint_encode(value: Int) -> Vec[UInt8]

Encode value as signed LEB128 bytes (negative values use the 64-bit two's-complement pattern). Complexity: O(log128(|value|)).

fn varint_decode(bytes: &Vec[UInt8]) -> Result[(Int, Int), Str]

Decode a signed LEB128 value from bytes, returning (value, consumed). Returns Err on truncation or on input longer than 10 bytes. Complexity: O(1) (at most 10 bytes).

NOTE: the name varint_decode collides with the parent module xiom.serialize's own varint_decode, so direct calls to this symbol can misresolve in the current compiler (BUG 25 #1). The real logic lives in the uniquely-named private helper _sleb128_decode; this function is a thin wrapper. Prefer varint_decode_slice / _sleb128_decode for verification.

fn varint_size(value: Int) -> Int

The number of bytes needed to encode value as signed LEB128. Complexity: O(1) (at most 10).

fn zigzag_encode(n: Int) -> Int

Map a signed value to its non-negative zigzag encoding: 0 -> 0, -1 -> 1, 1 -> 2, -2 -> 3, ... Complexity: O(1).

fn zigzag_decode(n: Int) -> Int

Invert zigzag_encode: map a zigzag value back to the signed value. Complexity: O(1).

fn uvarint_encode(value: UInt64) -> Vec[UInt8]

Encode a UInt64 as unsigned LEB128 bytes. Complexity: O(log128(v)).

fn uvarint_decode(bytes: &Vec[UInt8]) -> Result[(UInt64, Int), Str]

Decode an unsigned LEB128 value from bytes, returning (value, consumed). Returns Err on truncation, on input longer than 10 bytes, or when the value would overflow UInt64. Complexity: O(1) (at most 10 bytes).

fn varint_encode_slice(values: &Vec[Int]) -> Vec[UInt8]

Encode each value back-to-back as a signed LEB128 stream.

fn varint_decode_slice(bytes: &Vec[UInt8]) -> Result[Vec[Int], Str]

Decode a stream of back-to-back signed LEB128 values. Returns Err on the first malformed or truncated varint. Complexity: O(n), n = number of values.




yaml_lite.xi

enum YamlValue

Parsed YAML-lite value (scalar/map/sequence).

  • Scalar(value: Str)
  • Sequence(items: Vec[YamlValue])
  • Mapping(keys: Vec[Str], values: Vec[YamlValue])
fn yaml_parse(s: Str) -> Result[YamlValue, Str]

Parse s into a YamlValue tree. Returns Err on malformed documents. Complexity: O(n), n = document size.

NOTE: the current compiler miscompiles cross-module calls that return a Result whose payload is a recursive enum (see report); yaml_parse works when invoked from within its own module but not across modules.

fn yaml_parse_document(s: Str) -> Result[YamlValue, Str]

Parse a single YAML document (alias of yaml_parse for a strict reader). Complexity: O(n), n = document size.

fn yaml_stringify(v: YamlValue) -> Str

Serialize v as YAML text. Complexity: O(n), n = number of nodes.

fn yaml_get(v: YamlValue, key: Str) -> Option[YamlValue]

The value under key, if v is a mapping and the key is present. Complexity: O(k), k = number of keys.

fn yaml_emit_scalar(s: Str) -> Str

Emit s as a quoted (when ambiguous) or plain YAML scalar. Complexity: O(n), n = string length.

fn yaml_emit_sequence(items: &Vec[Str]) -> Str

Emit items as a YAML block sequence. Complexity: O(n), n = number of items.

fn yaml_emit_mapping(keys: &Vec[Str], values: &Vec[Str]) -> Str

Emit keys and values as a YAML block mapping. The two vectors must be the same length; mismatches are truncated to the shorter. Complexity: O(n), n = number of keys.