Skip to main content

Builder

Struct Builder 

Source
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

Source

pub fn new() -> Self

Source

pub fn build<F: FnOnce(&mut Builder)>(f: F) -> Vec<u8> ⓘ

Build a buffer by driving a fresh builder, returning its bytes.

Source

pub fn as_bytes(&self) -> &[u8] ⓘ

The bytes built so far.

Source

pub fn into_bytes(self) -> Vec<u8> ⓘ

Consume the builder, returning the built buffer.

Source

pub fn fs(&mut self, name: &str) -> &mut Self

FS and a file name. Ends any open record.

Source

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.

Source

pub fn gs_name(&mut self, name: &str) -> &mut Self

GS and a group name. Ends any open record.

Source

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.

Source

pub fn rs(&mut self) -> &mut Self

A bare RS: opens a record with no units yet.

Source

pub fn rs_value(&mut self, value: impl AsRef<[u8]>) -> &mut Self

RS and the record’s first value.

Source

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).

Source

pub fn us_value(&mut self, value: impl AsRef<[u8]>) -> &mut Self

US and the value that follows it.

Source

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.

Source

pub fn soh(&mut self) -> &mut Self

A bare SOH. Before rs it opens a header row.

Source

pub fn soh_key(&mut self, name: &str) -> &mut Self

SOH and a key. The key’s value follows as us_value.

Source

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.

Source

pub fn can(&mut self) -> &mut Self

CAN: close the innermost level. Panics when none is open.

Source

pub fn bel(&mut self) -> &mut Self

A bare BEL: marks the element that follows dead.

Source

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).

Source

pub fn enq(&mut self, name: &str) -> &mut Self

ENQ and a group name: a group reference. The name is required.

Source

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␘).

Source

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.

Source

pub fn eot(&mut self) -> &mut Self

EOT: the end of the document. Ends any open record.

Source

pub fn etb(&mut self) -> &mut Self

A bare ETB: commit what was appended since the last commit. Ends any open record.

Source

pub fn etb_payload(&mut self, payload: &str) -> &mut Self

ETB and an integrity payload (a name).

Source

pub fn em(&mut self, tail: &[u8]) -> &mut Self

EM and the raw tail. Nothing may follow.

Source

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.

Source

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).

Source

pub fn file(&mut self, name: &str) -> &mut Self

FS and a file name.

Source

pub fn group(&mut self, name: &str, headers: Option<&[&str]>) -> &mut Self

GS, a group name, and a header row when given.

Source

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.

Source

pub fn header(&mut self, names: &[&str]) -> &mut Self

A header row: SOH RS name US name …. Ends the record it writes.

Source

pub fn section(&mut self, name: &str, depth: usize) -> &mut Self

Document mode: a section at depth (depth − 1 SUB, GS and a name).

Source

pub fn record<T: AsRef<[u8]>>(&mut self, fields: &[T]) -> &mut Self

A positional record.

Source

pub fn begin_record(&mut self) -> &mut Self

Begin a record and leave it open for field, level, list, reference or key calls.

Source

pub fn block(&mut self, text: impl AsRef<[u8]>) -> &mut Self

Document mode: a paragraph (a record with one unit).

Source

pub fn item(&mut self, text: impl AsRef<[u8]>) -> &mut Self

Document mode: a list item (a unit of the open block).

Source

pub fn field(&mut self, value: impl AsRef<[u8]>) -> &mut Self

Append a unit to the open record (opens one if none is open).

Source

pub fn map<T: AsRef<[u8]>>(&mut self, pairs: &[(&str, T)]) -> &mut Self

A keyed record: a map, entries in the order given.

Source

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).

Source

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.

Source

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.

Source

pub fn list<T: AsRef<[u8]>>(&mut self, items: &[T]) -> &mut Self

A flat list as the next unit: a level holding one record.

Source

pub fn reference(&mut self, name: &str) -> &mut Self

A group reference (ENQ name) as the next unit.

Source

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).

Source

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␘.

Source

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.

Source

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).

Source

pub fn note(&mut self, text: impl AsRef<[u8]>) -> &mut Self

A note at the current position (record level, or as an empty unit). A note has content: an empty one panics (a bare BEL is a dead marker, written with bel).

Trait Implementations§

Source§

impl Default for Builder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.