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§
- Encode
Options - Options for
encode_with. - Error
- Error returned by Terset encoding, decoding, and write-profile functions.
- Json
Object - An insertion-order-preserving JSON object.
- Json
String - 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. - Write
Column - Column specification for
write_prompt,read_rows, anditer_rows. - Write
Options - Options for write-profile v1 and v2 (
write_prompt,read_rows,iter_rows).
Enums§
- Error
Kind - Category of error returned by Terset operations.
- Format
Version - Terset format version to write. The decoder accepts all seven versions (
1.0through1.6). - Json
Value - A zero-dependency JSON value preserving exact object key order and IEEE-754 binary64 number semantics.
- Write
Column Type - Column data type for the Terset write profile.
- Write
Default - 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_forfor child tables and 1.6 modifiers.
Functions§
- decode
- Strictly decodes canonical Terset text (formats
1.0through1.6) into aJsonValue. - 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.