c0
A Rust implementation of C0DATA — structured data built on ASCII C0 control codes. Values are UTF-8 text (any bytes, as text); structure is sixteen single-byte codes: FS/GS/RS/US separators, SOH keys and header rows, STX…ETX text, DLE…CAN nesting, ENQ references, BEL comments, SYN/EOT document framing, ETB stream commits, EM trailing blobs.
This crate is a zero-copy reader/writer: readers index a buffer once and
hand back slices (&[u8]) into it, decoding text only on demand
(Cow<[u8]>). It has no dependencies.
Status
Implements the 1.0 redesign of the spec, mirroring the Crystal reference
(c0-cr):
- scanning primitives, validator (
validate,canonical) Container/Document/Body/Group/Record/Referencereaders,Builderwriter- stream mode:
StreamReader(commits, damaged blocks, torn tail),StreamWriter,FileLogwith tail repair and fsync - pretty form: compact, aligned and spaced layouts, and the round-tripping
parse - CSV ⇄ C0DATA conversion
- C0-DIFF: parse, build, and atomic multi-file apply (sections and replace-all)
- JSON: the
Valuetree andto_value/from_value(dependency-free), withto_json/from_jsontext helpers behind the optionaljsonfeature
It passes every case of the shared conformance vectors from
c0-spec, read from the git submodule at
c0-spec/. After cloning:
Not ported: a YAML converter and a serde-style derive (the Serializable
equivalent).
Features
The library is dependency-free by default. The optional json feature adds
JSON text conversion and pulls in serde_json:
= { = "0.3.1", = ["json"] } # used in code as `c0`
Usage
use ;
// Build canonical compact bytes: ␝users␁␞name␟amount␞Alice␟1502.30␞Bob␟340.00
let buf = build;
// Read them back, zero-copy
let body = new?;
let users = body.group;
assert_eq!;
assert_eq!;
// The builder's output is canonical — hashable for content addressing
assert!;
Values (record, field, list, key, map) take any AsRef<[u8]> and are
written in their one spelling: plain when they have no byte below 0x20 and no
edge whitespace, otherwise text ␂…␃ with only STX, ETX, DLE, ETB, EOT, SYN
and EM escaped. Names (labels, keys, header names) are identifiers and panic
when they are not.
Keyed records and nesting
let buf = build;
let body = new?;
let server = body.group.record;
assert_eq!;
let origins = server.get_level?.unwrap;
assert_eq!;
A positional unit holding a level is read with Record::level (any body) or
Record::list (a level holding one positional record, written with
Builder::list). A reference (␅tags, ␅␂tags␟001␃) is written with
reference/reference_path and read with Record::reference.
Primitives
Underneath, the builder has one method per code: fs, gs, rs, us,
sub, soh, dle, can, bel, enq, syn, eot, etb, em. Each writes
its code and, when given, what follows it, so a chain reads like the bytes,
and anything the grammar allows can be written. Rust has no optional
arguments, so a code that may stand bare has two spellings: the bare code
(rs(), us(), sub(), gs(), soh(), bel(), etb()) and the code
with what follows it (rs_value, us_value, sub_value, gs_name,
soh_key, bel_note, etb_payload); a reference is enq(name) or
enq_path(&[...]). sub is C0-DIFF only: in a data body SUB is an error.
let buf = build;
The composed methods above are built on these and cannot write ill-formed
output; with the primitives, well-formedness is yours. The two layers
share the record state and mix freely: rs_value("a") then field("b")
writes ␞a␟b, and gs_name("g") then key("k", "v") writes ␝g␞␁k␟v.
Documents
let buf = build;
let doc = new?;
assert_eq!;
assert!; // a missing SYN or EOT warns, never fails
Container::new reads a buffer of several documents and the raw region after
EM (blob_bytes(name) looks a blob up through the first heading's blob
table). validate rejects malformed bytes; canonical tells whether bytes
are a canonical unit.
Stream logs (ETB commits)
use ;
let mut log = open_log?; // truncates a torn tail first
log.start?; // ␖␁shape␟stream␗
log.record?;
log.batch?;
let bytes = read_log?;
let r = new;
assert!; // an uncommitted tail is skipped
let committed = r.undamaged; // blocks cut by a stray ETB are dropped
let doc = new?;
Pretty form
format/format_mode render compact bytes with Control Picture glyphs,
line breaks and indentation (FormatMode::{Compact, Aligned, Spaced});
parse reads them back. A value containing a glyph character is refused.
Development
License
MIT