pub struct Builder { /* private fields */ }Expand description
Writes C0DATA in canonical compact form.
let buf = c0::Builder::build(|b| {
b.syn().heading("shape", "tabular");
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(),
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). sub is C0-DIFF only:
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
});The composed methods (file, group, header, record, field,
map, key, level, list, reference, 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, reference segments, 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. gs().gs_name("sub") is a group at depth 2. 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
A bare SUB: a substitution whose new side is empty. C0-DIFF only; in a data body SUB is an error. Does not change the unit count.
Sourcepub fn sub_value(&mut self, value: impl AsRef<[u8]>) -> &mut Self
pub fn sub_value(&mut self, value: impl AsRef<[u8]>) -> &mut Self
SUB and the new side of a substitution. C0-DIFF only:
rs_value("old").sub_value("new") is a replace-all,
us_value("10").sub_value("20") a pattern unit with one substitution.
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_path(&mut self, segments: &[&str]) -> &mut Self
pub fn enq_path(&mut self, segments: &[&str]) -> &mut Self
ENQ and a reference by path: one segment is a group reference, two or three are a path written as text.
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
GS×depth, 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 (GS×depth 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_path(&mut self, segments: &[&str]) -> &mut Self
pub fn reference_path(&mut self, segments: &[&str]) -> &mut Self
A reference by path as the next unit: one segment is a group reference, two or three are a path written as text.