Expand description
C0DATA — structured data using ASCII C0 control codes.
Values are UTF-8 text (any bytes, as text); structure is sixteen
single-byte control codes. This crate is a zero-copy reader/writer for
the 1.0 design: Container, Document and Body index a buffer
and hand back slices into it, Builder writes canonical compact form,
validate and canonical check well-formedness and canonicality,
and the stream, pretty, diff, csv and json modules cover the
rest of the spec.
let buf = c0::Builder::build(|b| {
b.group("users", Some(&["name", "amount"]));
b.record(&["Alice", "100"]);
b.record(&["Bob", "200"]);
});
let body = c0::Body::new(&buf).unwrap();
let users = body.group(0);
assert_eq!(users.headers(), [b"name".as_slice(), b"amount"]);
assert_eq!(users.record(1).value(0).unwrap().as_ref(), b"Bob");
assert!(c0::canonical(&buf));Modules§
- csv
- CSV ⇄ C0DATA conversion.
- diff
- C0-DIFF: atomic multi-file edits with anchored patterns (DIFF.md).
- json
- JSON ⇄ C0DATA conversion.
- scan
- Scanning primitives. Each takes a buffer, a position and a stop, and returns the position after the construct it skips; the fallible ones fail on malformed input. They are the bricks the readers are built from and are exposed for tools that index C0DATA themselves.
Structs§
- Body
- A document body: the bytes between a heading and EOT, or the inside of a nested level. Indexes files, groups, header rows and records once; everything it returns borrows the original buffer.
- Builder
- Writes C0DATA in canonical compact form.
- Container
- A file or message: one or more documents and an optional raw tail after EM.
- Document
- One C0DATA document: an optional SYN heading, a body, an optional EOT.
Document::newreads the first document in a buffer; useContainerfor a buffer holding several documents or an EM tail. - FileLog
- An append-only log file with ETB commits. Each commit is flushed and,
when
syncis set, fsync’d. Dropping the value closes the file. - File
View - A file within a body: its name and groups.
- Group
- A group: its depth (GS count; 0 for the unnamed leading group), name, optional header row and records.
- Heading
- The heading of a document: keyed units after SYN, in written order. Lookups take the last value for a duplicate key.
- Record
- A record: positional or keyed. Copyable, zero-copy accessors.
- Reference
- A reference: a group name, or a path of two or three segments (group, record id, field name).
- Stream
Reader - Scans a buffer for commits and exposes the committed region, block by block. Zero-copy: accessors return slices into the original buffer.
- Stream
Writer - Appends committed blocks to any
Writesink. Eachrecord,map,header,grouporbatchis one block, written with its ETB in a single append and flushed. For files preferopen_log, which repairs a torn tail first and can fsync each commit. - Validator
- The well-formedness and canonicality checker. Use
validateandcanonicalunless you want the reasons a unit is not canonical.
Enums§
- Error
- Errors raised while scanning, validating or decoding C0DATA.
- Format
Mode - Pretty layout mode.
Constants§
- BEL
- CAN
- CR
- DLE
- EM
- ENQ
- EOT
- ETB
- ETX
- FS
- GS
- HT
- LF
- RS
- SOH
- SP
- STX
- SUB
- SYN
- US
- VERSION
- Crate version (mirrors the Crystal reference’s
C0::VERSION).
Functions§
- align
- Reformat a pretty-form string with column alignment. Keyed records
(
␁units) are aligned on their␟like two-column tables. - canonical
- Well-formed and canonical: no layout, one spelling per value, keys unique within a keyed record, no framing, no comments.
- decode_
text - Decode the inside of a text: DLE escapes removed. Zero-copy when there are none. The inside of a scanned text never ends in a lone DLE; one on an arbitrary slice is dropped.
- format
- Format compact bytes as pretty form, two-space indent, compact mode.
- format_
mode - Format with a layout mode.
- format_
with - Format with a custom indent string, compact mode.
- glyph
- The Control Picture for a control byte.
- is_
assigned - Whether a byte is one of the sixteen assigned structural or framing codes.
- is_
control - Whether a byte is a control code (below 0x20).
- is_
layout - Whether a byte is layout outside text (SP, HT, LF, CR).
- is_
level - Whether the raw unit is a nested level.
- is_name
- Whether bytes are a legal name (label, key, header name): no byte below 0x20 and no edge whitespace. A name may be empty.
- is_
plain - Whether a value can be written plain: no byte below 0x20 and no edge whitespace.
- is_
reference - Whether the raw unit is a reference.
- is_text
- Whether the raw unit (layout included) is a text value.
- is_
text_ escaped - Whether a byte is one of the seven codes that take a DLE inside text.
- last_
commit_ end - Position just past the last commit, found by scanning backward
(treating an ETB behind an odd run of DLEs as escaped). A shortcut for
well-formed input; the forward scan of
StreamReaderis authoritative. - open_
log - Open an append-only log file, repairing any torn tail first. Each commit is fsync’d.
- open_
log_ with - Like
open_log, with explicit control over per-commit fsync. - parse
- Pretty form → compact bytes. Glyphs become codes; outside text, layout touching a code is dropped and interior spaces are kept; inside text everything is kept.
- read_
log - Read a log file into a byte buffer; wrap it with
StreamReader::new. - repair_
log - Truncate an uncommitted tail so the file ends at a commit.
- trim
- Trim layout from both ends of a raw slice.
- validate
- Fail on the first structural error.
- value
- The logical value of a raw unit slice: for text, the inside with its
escapes decoded (zero-copy when there are none); for a plain value, the
slice with edge layout removed. A unit holding only a note or a dead
level is the empty value. Fails on malformed input, on a level and on a
reference (see
Record::levelandRecord::reference). - write_
name - Write a name; fails if it is not one.
- write_
value - Write a value in its one canonical spelling: plain when it can be, text otherwise, with only the seven codes escaped.