pub struct Builder { /* private fields */ }Expand description
Writes C0DATA in canonical compact form.
let buf = c0::Builder::build(|b| {
b.syn().heading("schema", "ledger");
b.group("users", Some(&["name", "amount"]));
b.record(&["Alice", "1502.30"]);
b.record(&["Bob", "340.00"]);
b.eot();
});Two layers. The primitives are 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 grammatically follows it, so a
chain reads like the bytes. Rust has no optional arguments, so the codes
that may stand bare come in two spellings: the bare code (rs(), us(),
gs(), soh(), bel(), etb()) and the code with what follows it
(rs_value, us_value, gs_name, soh_key, bel_note,
etb_payload). sub is always bare: the element after the next
separator is subordinate to the one before it, before GS a subsection
(␚␝s), before US a sub-unit (old␚␟new). A reference is enq(name)
or enq_level(|b| …), a pattern:
let buf = c0::Builder::build(|b| {
b.rs_value("Alice").us_value("1502.30"); // ␞Alice␟1502.30
b.rs().soh_key("host").us_value("localhost"); // ␞␁host␟localhost
b.bel().gs_name("old"); // ␇␝old: a dead group
b.sub().gs_name("intro"); // ␚␝intro: a subsection (depth 2)
b.rs_value("old").sub().us_value("new"); // ␞old␚␟new: new is a sub-unit of old
b.rs().enq("tags").us().enq_level(|b| { // ␞␅tags␟␅␐␝tags␞001␘
b.gs_name("tags").rs_value("001");
});
});The composed methods (file, group, header, record, field,
map, key, level, list, reference, sub_unit, note, …) are
built on the
primitives and cannot write ill-formed output. The primitives write what
you spell; well-formedness is then yours to keep. Both layers share the
record state, so they mix.
Values are written in their one spelling: plain when they can be, text
(STX … ETX, seven codes escaped) otherwise; they accept any
AsRef<[u8]> and are byte-transparent. Keyed records (map, key)
keep the order given. Nested levels (level, list, key_level)
open with DLE and close with CAN. Names (file and group labels, keys,
header names, pattern steps, commit payloads) are identifiers: a
name with a byte below 0x20 or edge whitespace panics.
Implementations§
Source§impl Builder
impl Builder
pub fn new() -> Self
Sourcepub fn build<F: FnOnce(&mut Builder)>(f: F) -> Vec<u8> ⓘ
pub fn build<F: FnOnce(&mut Builder)>(f: F) -> Vec<u8> ⓘ
Build a buffer by driving a fresh builder, returning its bytes.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consume the builder, returning the built buffer.
Sourcepub fn gs(&mut self) -> &mut Self
pub fn gs(&mut self) -> &mut Self
A bare GS: a group with an empty name (gs().gs_name("b") is two
groups, "" then b). Ends any open record.
Sourcepub fn gs_name_at(&mut self, name: &str, depth: usize) -> &mut Self
pub fn gs_name_at(&mut self, name: &str, depth: usize) -> &mut Self
depth − 1 SUB, GS and a group name: gs_name_at("s", 2) is
␚␝s, a subsection. Ends any open record.
Sourcepub fn rs_value(&mut self, value: impl AsRef<[u8]>) -> &mut Self
pub fn rs_value(&mut self, value: impl AsRef<[u8]>) -> &mut Self
RS and the record’s first value.
Sourcepub fn us(&mut self) -> &mut Self
pub fn us(&mut self) -> &mut Self
A bare US: the next unit of the record is empty, or is what follows (a level, a reference).
Sourcepub fn us_value(&mut self, value: impl AsRef<[u8]>) -> &mut Self
pub fn us_value(&mut self, value: impl AsRef<[u8]>) -> &mut Self
US and the value that follows it.
Sourcepub fn sub(&mut self) -> &mut Self
pub fn sub(&mut self) -> &mut Self
SUB: the element after the next separator is subordinate to the one
before it, at that code’s grain: before GS a subsection (␚␝s, one
SUB per level), before US a sub-unit (old␚␟new). Does not change
the unit count.
Sourcepub fn soh_key(&mut self, name: &str) -> &mut Self
pub fn soh_key(&mut self, name: &str) -> &mut Self
SOH and a key. The key’s value follows as us_value.
Sourcepub fn dle(&mut self) -> &mut Self
pub fn dle(&mut self) -> &mut Self
DLE: open a nested level. Inside, the builder is at the top of a
fresh body; the record state is restored by can.
Sourcepub fn bel_note(&mut self, text: impl AsRef<[u8]>) -> &mut Self
pub fn bel_note(&mut self, text: impl AsRef<[u8]>) -> &mut Self
BEL and a note. A note has content: an empty one panics (a bare BEL
is a dead marker, written with bel).
Sourcepub fn enq(&mut self, name: &str) -> &mut Self
pub fn enq(&mut self, name: &str) -> &mut Self
ENQ and a group name: a group reference. The name is required.
Sourcepub fn enq_level<F: FnOnce(&mut Builder)>(&mut self, f: F) -> &mut Self
pub fn enq_level<F: FnOnce(&mut Builder)>(&mut self, f: F) -> &mut Self
ENQ and a pattern: a level built in f, the target with its values
left out (enq_level(|b| { b.gs_name("tags").rs_value("001").us_value("label"); })
writes ␅␐␝tags␞001␟label␘).
Sourcepub fn syn(&mut self) -> &mut Self
pub fn syn(&mut self) -> &mut Self
SYN: the start of a document. Heading pairs follow as
soh_key(k).us_value(v) or heading. Ends any open record.
Sourcepub fn etb(&mut self) -> &mut Self
pub fn etb(&mut self) -> &mut Self
A bare ETB: commit what was appended since the last commit. Ends any open record.
Sourcepub fn etb_payload(&mut self, payload: &str) -> &mut Self
pub fn etb_payload(&mut self, payload: &str) -> &mut Self
ETB and an integrity payload (a name).
Sourcepub fn heading(&mut self, key: &str, value: impl AsRef<[u8]>) -> &mut Self
pub fn heading(&mut self, key: &str, value: impl AsRef<[u8]>) -> &mut Self
A heading key and its value (SOH key US value), after syn.
Sourcepub fn heading_level<F: FnOnce(&mut Builder)>(
&mut self,
key: &str,
f: F,
) -> &mut Self
pub fn heading_level<F: FnOnce(&mut Builder)>( &mut self, key: &str, f: F, ) -> &mut Self
A heading key whose value is a nested level (e.g. the blob table).
Sourcepub fn group(&mut self, name: &str, headers: Option<&[&str]>) -> &mut Self
pub fn group(&mut self, name: &str, headers: Option<&[&str]>) -> &mut Self
GS, a group name, and a header row when given.
Sourcepub fn group_at(
&mut self,
name: &str,
headers: Option<&[&str]>,
depth: usize,
) -> &mut Self
pub fn group_at( &mut self, name: &str, headers: Option<&[&str]>, depth: usize, ) -> &mut Self
A group at depth (depth − 1 SUB then GS), a group name, and a
header row when given.
Sourcepub fn header(&mut self, names: &[&str]) -> &mut Self
pub fn header(&mut self, names: &[&str]) -> &mut Self
A header row: SOH RS name US name …. Ends the record it writes.
Sourcepub fn section(&mut self, name: &str, depth: usize) -> &mut Self
pub fn section(&mut self, name: &str, depth: usize) -> &mut Self
Document mode: a section at depth (depth − 1 SUB, GS and a name).
Sourcepub fn begin_record(&mut self) -> &mut Self
pub fn begin_record(&mut self) -> &mut Self
Sourcepub fn block(&mut self, text: impl AsRef<[u8]>) -> &mut Self
pub fn block(&mut self, text: impl AsRef<[u8]>) -> &mut Self
Document mode: a paragraph (a record with one unit).
Sourcepub fn item(&mut self, text: impl AsRef<[u8]>) -> &mut Self
pub fn item(&mut self, text: impl AsRef<[u8]>) -> &mut Self
Document mode: a list item (a unit of the open block).
Sourcepub fn field(&mut self, value: impl AsRef<[u8]>) -> &mut Self
pub fn field(&mut self, value: impl AsRef<[u8]>) -> &mut Self
Append a unit to the open record (opens one if none is open).
Sourcepub fn map<T: AsRef<[u8]>>(&mut self, pairs: &[(&str, T)]) -> &mut Self
pub fn map<T: AsRef<[u8]>>(&mut self, pairs: &[(&str, T)]) -> &mut Self
A keyed record: a map, entries in the order given.
Sourcepub fn key(&mut self, name: &str, value: impl AsRef<[u8]>) -> &mut Self
pub fn key(&mut self, name: &str, value: impl AsRef<[u8]>) -> &mut Self
One keyed unit on the open record (opens one if none is open).
Sourcepub fn key_level<F: FnOnce(&mut Builder)>(
&mut self,
name: &str,
f: F,
) -> &mut Self
pub fn key_level<F: FnOnce(&mut Builder)>( &mut self, name: &str, f: F, ) -> &mut Self
One keyed unit whose value is a nested level.
Sourcepub fn level<F: FnOnce(&mut Builder)>(&mut self, f: F) -> &mut Self
pub fn level<F: FnOnce(&mut Builder)>(&mut self, f: F) -> &mut Self
A nested level as the next unit. Inside f the builder is at the
top of a fresh body.
Sourcepub fn list<T: AsRef<[u8]>>(&mut self, items: &[T]) -> &mut Self
pub fn list<T: AsRef<[u8]>>(&mut self, items: &[T]) -> &mut Self
A flat list as the next unit: a level holding one record.
Sourcepub fn reference(&mut self, name: &str) -> &mut Self
pub fn reference(&mut self, name: &str) -> &mut Self
A group reference (ENQ name) as the next unit.
Sourcepub fn reference_level<F: FnOnce(&mut Builder)>(&mut self, f: F) -> &mut Self
pub fn reference_level<F: FnOnce(&mut Builder)>(&mut self, f: F) -> &mut Self
A reference as the next unit, its pattern built in f (see
enq_level).
Sourcepub fn reference_to(
&mut self,
group: &str,
record: &str,
field: Option<&str>,
) -> &mut Self
pub fn reference_to( &mut self, group: &str, record: &str, field: Option<&str>, ) -> &mut Self
A reference to a record, or to a field of it, as the next unit: the
pattern ␅␐␝group␞record␟field␘.
Sourcepub fn sub_unit(&mut self, value: impl AsRef<[u8]>) -> &mut Self
pub fn sub_unit(&mut self, value: impl AsRef<[u8]>) -> &mut Self
A sub-unit of the unit just written: SUB, US and the value
(␞a␚␟b). It is not a unit of the record and does not count as
one. Opens a record if none is open: ␞␚␟b is an empty first unit
holding b.
Sourcepub fn sub_unit_at(
&mut self,
value: impl AsRef<[u8]>,
depth: usize,
) -> &mut Self
pub fn sub_unit_at( &mut self, value: impl AsRef<[u8]>, depth: usize, ) -> &mut Self
A sub-unit at depth: depth SUB, US and the value. depth 2 is a
sub-unit of the sub-unit written before (␞a␚␟b␚␚␟c).