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
//! Framing: putting a version tag in front of a payload, and taking it back off.
//!
//! The frame is deliberately tiny. A value on the wire is a two-byte,
//! big-endian [`u16`] version followed by your encoded payload:
//!
//! ```text
//! byte 0 byte 1 byte 2 ..
//! +--------+--------+---------------------------+
//! | ver hi | ver lo | payload (your format) |
//! +--------+--------+---------------------------+
//! \_______________/ \_________________________/
//! u16 big-endian opaque to verstamp
//! ```
//!
//! That is the whole format. verstamp never looks inside the payload; encoding
//! and decoding it stays entirely yours. Starting versions at 1 is a
//! recommendation: a reader that never registers version 0 then rejects an
//! all-zero buffer as [`Error::UnknownVersion`]. verstamp does not enforce it.
//!
//! [`frame`] and [`write()`] add the tag. [`read`] takes it off and hands you a
//! [`Frame`] to route on.
use crateChain;
use crate;
/// The width of the version tag, in bytes.
///
/// ```
/// assert_eq!(verstamp::wire::TAG_LEN, 2);
/// ```
pub const TAG_LEN: usize = 2;
/// Prepend a version tag to already-encoded payload bytes.
///
/// This is the infallible half of writing: you encode with your format of
/// choice, then stamp the result. Use [`write()`] when you would rather hand the
/// encoder a buffer to append into.
///
/// ```
/// let bytes = verstamp::frame(3, &[0xAA, 0xBB]);
/// assert_eq!(bytes, [0x00, 0x03, 0xAA, 0xBB]);
/// ```
/// Write a version tag, then let `encode` append the payload.
///
/// `encode` receives a buffer that already holds the two tag bytes; it appends
/// the payload and returns `Ok(())`, or an error that becomes
/// [`Error::Encode`]. This suits encoders that write into a `Vec<u8>` or an
/// `impl std::io::Write`.
///
/// ```
/// let bytes = verstamp::write(1, |buf| {
/// buf.extend_from_slice(&42u32.to_be_bytes());
/// Ok::<_, std::convert::Infallible>(())
/// })?;
/// assert_eq!(bytes, [0x00, 0x01, 0x00, 0x00, 0x00, 0x2A]);
/// # Ok::<(), verstamp::Error>(())
/// ```
///
/// # Errors
///
/// Returns [`Error::Encode`] if `encode` returns an error, preserving it as the
/// [`source`](std::error::Error::source).
/// Read the version tag off a byte stream.
///
/// The returned [`Frame`] exposes the [`version`](Frame::version) and the
/// [`payload`](Frame::payload), and starts a migration chain through
/// [`Frame::at`]. Empty and truncated inputs fail here, before any decoder
/// runs.
///
/// ```
/// let bytes = verstamp::frame(7, b"payload");
/// let frame = verstamp::read(&bytes)?;
/// assert_eq!(frame.version(), 7);
/// assert_eq!(frame.payload(), b"payload");
/// # Ok::<(), verstamp::Error>(())
/// ```
///
/// # Errors
///
/// Returns [`Error::Empty`] if `bytes` is empty, or [`Error::Truncated`] if it
/// is shorter than the two-byte tag.
/// A stream whose version tag has been read, ready to route on.
///
/// Build a reader by naming the oldest version you can decode with
/// [`at`](Frame::at), then adding one [`then`](Chain::then) per migration step,
/// in strictly increasing version order. The tag decides which decoder runs;
/// migrations carry the result forward to the current type.