1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Streaming encoder for CBOR sequences.
//!
//! [`SequenceWriter`] is the write-side counterpart of
//! [`SequenceDecoder`](crate::SequenceDecoder) and
//! [`SequenceReader`](crate::SequenceReader). It wraps any
//! [`io::Write`](std::io::Write) and emits a CBOR sequence (RFC 8742)
//! in the format selected by [`EncodeFormat`].
use ;
use crate::;
/// Streaming writer for CBOR sequences in binary, hex, or diagnostic notation.
///
/// Construct with [`SequenceWriter::new`], call
/// [`write_item`](Self::write_item), [`write_items`](Self::write_items),
/// or [`write_pairs`](Self::write_pairs) to emit content, and then
/// [`into_inner`](Self::into_inner) to get the wrapped writer back.
///
/// Format semantics:
///
/// * [`EncodeFormat::Binary`] and [`EncodeFormat::Hex`]: items are
/// concatenated with no separator. Each item's bytes form a
/// self-delimiting CBOR value.
/// * [`EncodeFormat::Diagnostic`]: items are separated by `, ` (comma
/// and a space). The first item is written without a leading
/// separator; no trailing comma is emitted.
/// * [`EncodeFormat::DiagnosticPretty`]: items are pretty-printed with
/// `{:#?}` (indented, multi-line for collections) and separated by
/// `,\n`.
///
/// [`Format`](crate::Format) values accepted on the read side pass
/// through the `impl Into<EncodeFormat>` bound unchanged.
///
/// # Examples
///
/// Binary sequence:
///
/// ```
/// use cbor_core::{SequenceWriter, Value, EncodeFormat};
///
/// let mut buf = Vec::new();
/// let mut sw = SequenceWriter::new(&mut buf, EncodeFormat::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::{SequenceWriter, Value, EncodeFormat};
///
/// let mut buf = Vec::new();
/// let mut sw = SequenceWriter::new(&mut buf, EncodeFormat::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"#);
/// ```
///
/// A [`Format`](crate::Format) value (from the read-side API) converts
/// implicitly:
///
/// ```
/// 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();
/// assert_eq!(buf, b"01");
/// ```
///
/// Round-trip through [`SequenceDecoder`](crate::SequenceDecoder):
///
/// ```
/// use cbor_core::{Array, DecodeOptions, Format, SequenceWriter, Value, EncodeFormat};
///
/// let items = [Value::from(1), Value::from("hi"), Value::from(true)];
///
/// let mut buf = Vec::new();
/// let mut sw = SequenceWriter::new(&mut buf, EncodeFormat::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);
/// ```