Skip to main content

Crate c0

Crate c0 

Source
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::new reads the first document in a buffer; use Container for a buffer holding several documents or an EM tail.
FileLog
An append-only log file with ETB commits. Each commit is flushed and, when sync is set, fsync’d. Dropping the value closes the file.
FileView
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).
StreamReader
Scans a buffer for commits and exposes the committed region, block by block. Zero-copy: accessors return slices into the original buffer.
StreamWriter
Appends committed blocks to any Write sink. Each record, map, header, group or batch is one block, written with its ETB in a single append and flushed. For files prefer open_log, which repairs a torn tail first and can fsync each commit.
Validator
The well-formedness and canonicality checker. Use validate and canonical unless you want the reasons a unit is not canonical.

Enums§

Error
Errors raised while scanning, validating or decoding C0DATA.
FormatMode
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 StreamReader is 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::level and Record::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.

Type Aliases§

Pair
A key and its logical value, from a keyed record.
Result
Crate result type.
Table
A group read as a table is itself.