Skip to main content

Crate terset

Crate terset 

Source
Expand description

Official Rust implementation of Terset (Format 1.6 default, reading 1.0 through 1.6).

Terset is a compact text format for JSON records in LLM prompts. It has zero runtime dependencies in its default configuration and an optional serde feature for [serde_json::Value] and serde::Serialize/serde::Deserialize interoperability.

§Quick start

use terset::{decode, encode, rules_for, JsonValue};

let input = JsonValue::parse_json(
    r#"{"users":[{"id":"u1","name":"Ada","role":"admin"},{"id":"u2","name":"Ben","role":"ops"}]}"#,
)?;
let text = encode(&input)?;
let rules = rules_for(&text)?;
let roundtrip = decode(&text)?;

assert_eq!(roundtrip, input);
assert!(rules.contains("Tables use tab-separated headers and rows."));

§Write profile

use terset::{read_rows, write_prompt, WriteColumn, WriteColumnType};

let cols = [
    WriteColumn::new("label", WriteColumnType::String),
    WriteColumn::new("qty", WriteColumnType::Number),
];
let prompt = write_prompt(&cols, None)?;
assert!(prompt.ends_with("label\tqty"));

let rows = read_rows("pear\t2\nplum\t5\nEND_ROWS", &cols, None)?;
assert_eq!(rows.len(), 2);
assert_eq!(rows[0].get("label").and_then(|v| v.as_str()), Some("pear"));

Structs§

EncodeOptions
Options for encode_with.
Error
Error returned by Terset encoding, decoding, and write-profile functions.
JsonObject
An insertion-order-preserving JSON object.
JsonString
A JSON string that preserves valid UTF-8 with zero overhead and can also losslessly represent lone UTF-16 surrogate code units (\uD800..\uDFFF) using WTF-8 encoding for full ECMAScript JSON conformance.
RowStream
Streaming iterator returned by iter_rows.
WriteColumn
Column specification for write_prompt, read_rows, and iter_rows.
WriteOptions
Options for write-profile v1 and v2 (write_prompt, read_rows, iter_rows).

Enums§

ErrorKind
Category of error returned by Terset operations.
FormatVersion
Terset format version to write. The decoder accepts all seven versions (1.0 through 1.6).
JsonValue
A zero-dependency JSON value preserving exact object key order and IEEE-754 binary64 number semantics.
WriteColumnType
Column data type for the Terset write profile.
WriteDefault
Default value for a write-profile column when a cell is left blank.

Constants§

RULES
The full rules for the legacy default format. Use crate::rules_for for child tables and 1.6 modifiers.

Functions§

decode
Strictly decodes canonical Terset text (formats 1.0 through 1.6) into a JsonValue.
encode
Encodes a JSON object or array as Terset text using the default format (1.6).
encode_with
Encodes a JSON object or array as Terset text with explicit EncodeOptions.
iter_rows
Incrementally parses write-profile rows from a streamed iterator of text chunks.
read_rows
Reads all rows of a write-profile answer with the supplied columns and options.
rules_for
Returns the fitted prompt rules needed by canonical Terset text. Rejects the same noncanonical or invalid texts as decode.
write_prompt
Builds the model’s instructions and tab-separated header for the supplied columns.