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("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

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. gs().gs_name("sub") is a group at depth 2. 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 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

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.

Source

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.

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 reference (a name).

Source

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.

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

GS×depth, 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 (GS×depth 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_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.

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.