pub struct SequenceWriter<W: Write> { /* private fields */ }Expand description
Streaming writer for CBOR sequences in binary, hex, or diagnostic notation.
Construct with SequenceWriter::new, call
write_item, write_items,
or write_pairs to emit content, and then
into_inner to get the wrapped writer back.
Format semantics mirror the reader side:
Format::BinaryandFormat::Hex: items are concatenated with no separator. Each item’s bytes form a self-delimiting CBOR value.Format::Diagnostic: items are separated by,(comma and a space). The first item is written without a leading separator; no trailing comma is emitted.
The writer is format-agnostic to the caller: the same code path works for all three formats, and switching format is a single constructor argument.
§Examples
Binary sequence:
use cbor_core::{Format, SequenceWriter, Value};
let mut buf = Vec::new();
let mut sw = SequenceWriter::new(&mut buf, Format::Binary);
sw.write_item(&Value::from(1)).unwrap();
sw.write_item(&Value::from(2)).unwrap();
sw.write_item(&Value::from(3)).unwrap();
assert_eq!(buf, [0x01, 0x02, 0x03]);Diagnostic sequence, with separators inserted automatically:
use cbor_core::{Format, SequenceWriter, Value};
let mut buf = Vec::new();
let mut sw = SequenceWriter::new(&mut buf, Format::Diagnostic);
sw.write_items([Value::from(1), Value::from("hi"), Value::from(true)].iter()).unwrap();
assert_eq!(String::from_utf8(buf).unwrap(), r#"1, "hi", true"#);Round-trip through SequenceDecoder:
use cbor_core::{Array, DecodeOptions, Format, SequenceWriter, Value};
let items = [Value::from(1), Value::from("hi"), Value::from(true)];
let mut buf = Vec::new();
let mut sw = SequenceWriter::new(&mut buf, Format::Diagnostic);
sw.write_items(items.iter()).unwrap();
let decoded = Array::try_from_sequence(
DecodeOptions::new().format(Format::Diagnostic).sequence_decoder(&buf),
).unwrap();
assert_eq!(decoded.get_ref().as_slice(), &items);Implementations§
Source§impl<W: Write> SequenceWriter<W>
impl<W: Write> SequenceWriter<W>
Sourcepub const fn new(writer: W, format: Format) -> Self
pub const fn new(writer: W, format: Format) -> Self
Create a new sequence writer wrapping writer and emitting
items in the selected format.
use cbor_core::{Format, SequenceWriter};
let sw = SequenceWriter::new(Vec::new(), Format::Hex);
// `sw` now accepts items via `write_item` / `write_items` / `write_pairs`.Sourcepub fn write_item(&mut self, value: &Value) -> Result<()>
pub fn write_item(&mut self, value: &Value) -> Result<()>
Write one item of the sequence.
In Format::Diagnostic a , separator is inserted before
every item except the first. In Format::Binary and
Format::Hex items are written back-to-back with no separator.
use cbor_core::{Format, SequenceWriter, Value};
let mut buf = Vec::new();
let mut sw = SequenceWriter::new(&mut buf, Format::Hex);
sw.write_item(&Value::from(1)).unwrap();
sw.write_item(&Value::from(2)).unwrap();
assert_eq!(buf, b"0102");Sourcepub fn write_items<'a, I>(&mut self, items: I) -> Result<()>where
I: IntoIterator<Item = &'a Value>,
pub fn write_items<'a, I>(&mut self, items: I) -> Result<()>where
I: IntoIterator<Item = &'a Value>,
Write every item produced by items. Equivalent to calling
write_item in a loop, but keeps the call
site concise.
use cbor_core::{Format, SequenceWriter, Value};
let items = [Value::from(1), Value::from(2), Value::from(3)];
let mut buf = Vec::new();
SequenceWriter::new(&mut buf, Format::Binary)
.write_items(items.iter())
.unwrap();
assert_eq!(buf, [0x01, 0x02, 0x03]);Sourcepub fn write_pairs<'a, I>(&mut self, pairs: I) -> Result<()>
pub fn write_pairs<'a, I>(&mut self, pairs: I) -> Result<()>
Write each key/value pair as two consecutive items of the
sequence. Useful for emitting the contents of a map as a flat
CBOR sequence, mirroring Map::from_sequence
on the read side.
The iterator yields borrowed references, which is the natural
shape of &BTreeMap<Value, Value>::iter(), so a map held in a
Value can be streamed directly:
use cbor_core::{Format, SequenceWriter, Value, map};
let value = map! { "a" => 1, "b" => 2 };
let mut sw = SequenceWriter::new(Vec::new(), Format::Diagnostic);
sw.write_pairs(value.as_map().unwrap()).unwrap();
assert_eq!(sw.into_inner(), br#""a", 1, "b", 2"#);Sourcepub const fn get_mut(&mut self) -> &mut W
pub const fn get_mut(&mut self) -> &mut W
Mutably borrow the wrapped writer. Direct writes bypass the sequence writer’s separator bookkeeping, so use this only for format-specific ornamentation (for example, injecting a comment into diagnostic output) between full items.
Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Consume the sequence writer and return the wrapped writer.
use cbor_core::{Format, SequenceWriter, Value};
let mut sw = SequenceWriter::new(Vec::new(), Format::Binary);
sw.write_item(&Value::from(1)).unwrap();
sw.write_item(&Value::from(2)).unwrap();
let buf = sw.into_inner();
assert_eq!(buf, [0x01, 0x02]);