astrs-wire 0.1.0

AstRS control-plane protocol types, framed codec and version negotiation.
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! The `flags` header byte: CRC presence and payload compression.
//!
//! Blueprint §7.1 assigns bit 0 to "crc32c trailer present", bit 1 to lz4 and
//! bit 2 to zstd. Bits 3..7 are reserved and **must be zero**: a decoder that
//! ignored them could half-understand a frame from a newer peer, which is
//! exactly the silent-drift failure mode append-only evolution exists to
//! prevent.
//!
//! The compression bits are defined and negotiated here; the actual
//! compression is applied by `astrs-transport` at route setup (§6.4), which
//! owns the oxiarc codecs and the 16 KiB threshold policy. This crate reads
//! and writes the bits, validates that at most one is set, and never
//! compresses anything itself.

use core::fmt;
use core::ops::{BitAnd, BitOr, BitOrAssign, Sub, SubAssign};

use oxicode::{Decode, Encode};
use serde::{Deserialize, Serialize};

use crate::error::WireError;

/// Which compression codec a frame's payload was encoded with.
///
/// Also used at route setup to record the negotiated codec for a route
/// (blueprint §6.4).
///
/// # Examples
///
/// ```
/// use astrs_wire::{Compression, FrameFlags};
///
/// let flags = FrameFlags::CRC.with_compression(Compression::Zstd);
/// assert!(flags.has_crc());
/// assert_eq!(flags.compression(), Compression::Zstd);
/// ```
#[derive(
    Debug,
    Clone,
    Copy,
    Default,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Serialize,
    Deserialize,
    Encode,
    Decode,
)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Compression {
    /// The payload is stored verbatim.
    #[default]
    #[oxicode(variant = 0)]
    None,
    /// The payload is lz4-compressed (`oxiarc-lz4`).
    #[oxicode(variant = 1)]
    Lz4,
    /// The payload is zstd-compressed (`oxiarc-zstd`).
    #[oxicode(variant = 2)]
    Zstd,
}

impl Compression {
    /// Every codec this build knows, in variant order.
    pub const ALL: &'static [Self] = &[Self::None, Self::Lz4, Self::Zstd];

    /// A stable, lower-case name for manifests, logs and metrics labels.
    ///
    /// # Examples
    ///
    /// ```
    /// use astrs_wire::Compression;
    ///
    /// assert_eq!(Compression::Lz4.as_str(), "lz4");
    /// ```
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Lz4 => "lz4",
            Self::Zstd => "zstd",
        }
    }

    /// Whether this codec actually transforms the payload.
    ///
    /// # Examples
    ///
    /// ```
    /// use astrs_wire::Compression;
    ///
    /// assert!(!Compression::None.is_enabled());
    /// assert!(Compression::Zstd.is_enabled());
    /// ```
    #[must_use]
    pub const fn is_enabled(self) -> bool {
        !matches!(self, Self::None)
    }
}

impl fmt::Display for Compression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// The `flags` byte of a frame header.
///
/// A hand-rolled bit set — the workspace dependency policy (§18.1) has no
/// `bitflags` crate and this is eight bits with three meanings.
///
/// # Examples
///
/// ```
/// use astrs_wire::{Compression, FrameFlags};
///
/// let flags = FrameFlags::CRC | FrameFlags::LZ4;
/// assert_eq!(flags.bits(), 0b0000_0011);
/// assert!(flags.contains(FrameFlags::CRC));
/// assert_eq!(flags.compression(), Compression::Lz4);
///
/// let plain = flags - FrameFlags::LZ4;
/// assert_eq!(plain.compression(), Compression::None);
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FrameFlags(u8);

impl FrameFlags {
    /// No flags set: no checksum, no compression.
    pub const EMPTY: Self = Self(0);
    /// Bit 0 — a 4-byte little-endian CRC-32C trailer follows the payload.
    pub const CRC: Self = Self(0b0000_0001);
    /// Bit 1 — the payload is lz4-compressed.
    pub const LZ4: Self = Self(0b0000_0010);
    /// Bit 2 — the payload is zstd-compressed.
    pub const ZSTD: Self = Self(0b0000_0100);

    /// The bits this build defines. Everything else is reserved.
    pub const KNOWN_MASK: u8 = 0b0000_0111;
    /// The two compression bits, as a mask.
    pub const COMPRESSION_MASK: u8 = 0b0000_0110;

    /// The raw byte.
    ///
    /// # Examples
    ///
    /// ```
    /// use astrs_wire::FrameFlags;
    ///
    /// assert_eq!(FrameFlags::ZSTD.bits(), 4);
    /// ```
    #[must_use]
    pub const fn bits(self) -> u8 {
        self.0
    }

    /// Parses a `flags` byte, rejecting reserved and contradictory bits.
    ///
    /// # Errors
    ///
    /// - [`WireError::ReservedFlags`] if any bit outside
    ///   [`FrameFlags::KNOWN_MASK`] is set.
    /// - [`WireError::ConflictingCompression`] if both compression bits are
    ///   set.
    ///
    /// # Examples
    ///
    /// ```
    /// use astrs_wire::{FrameFlags, WireError};
    ///
    /// assert_eq!(FrameFlags::from_bits(0b0000_0101)?, FrameFlags::CRC | FrameFlags::ZSTD);
    /// assert!(matches!(
    ///     FrameFlags::from_bits(0b1000_0000),
    ///     Err(WireError::ReservedFlags { .. })
    /// ));
    /// assert!(matches!(
    ///     FrameFlags::from_bits(0b0000_0110),
    ///     Err(WireError::ConflictingCompression)
    /// ));
    /// # Ok::<(), astrs_wire::WireError>(())
    /// ```
    pub const fn from_bits(bits: u8) -> Result<Self, WireError> {
        let reserved = bits & !Self::KNOWN_MASK;
        if reserved != 0 {
            return Err(WireError::ReservedFlags { bits, reserved });
        }
        if bits & Self::COMPRESSION_MASK == Self::COMPRESSION_MASK {
            return Err(WireError::ConflictingCompression);
        }
        Ok(Self(bits))
    }

    /// Parses a `flags` byte, silently discarding reserved bits.
    ///
    /// Provided for diagnostics and tooling (a frame dumper that wants to
    /// describe a frame it would otherwise refuse). The framing code paths
    /// always use [`FrameFlags::from_bits`].
    ///
    /// # Examples
    ///
    /// ```
    /// use astrs_wire::FrameFlags;
    ///
    /// assert_eq!(FrameFlags::from_bits_truncate(0b1111_0001), FrameFlags::CRC);
    /// ```
    #[must_use]
    pub const fn from_bits_truncate(bits: u8) -> Self {
        Self(bits & Self::KNOWN_MASK)
    }

    /// Whether every bit of `other` is set in `self`.
    ///
    /// # Examples
    ///
    /// ```
    /// use astrs_wire::FrameFlags;
    ///
    /// let flags = FrameFlags::CRC | FrameFlags::LZ4;
    /// assert!(flags.contains(FrameFlags::CRC));
    /// assert!(!flags.contains(FrameFlags::ZSTD));
    /// assert!(flags.contains(FrameFlags::EMPTY));
    /// ```
    #[must_use]
    pub const fn contains(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }

    /// Whether no bits are set.
    #[must_use]
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }

    /// Whether the frame carries a CRC-32C trailer.
    #[must_use]
    pub const fn has_crc(self) -> bool {
        self.contains(Self::CRC)
    }

    /// Returns these flags with the CRC bit set or cleared.
    ///
    /// # Examples
    ///
    /// ```
    /// use astrs_wire::FrameFlags;
    ///
    /// assert!(FrameFlags::EMPTY.with_crc(true).has_crc());
    /// assert!(!FrameFlags::CRC.with_crc(false).has_crc());
    /// ```
    #[must_use]
    pub const fn with_crc(self, enabled: bool) -> Self {
        if enabled {
            Self(self.0 | Self::CRC.0)
        } else {
            Self(self.0 & !Self::CRC.0)
        }
    }

    /// The compression codec these flags select.
    ///
    /// A byte with both compression bits set can never reach this method
    /// through [`FrameFlags::from_bits`]; if one is constructed directly the
    /// lz4 bit wins, which is the conservative reading (lz4 is the lower bit
    /// and the cheaper codec to attempt).
    ///
    /// # Examples
    ///
    /// ```
    /// use astrs_wire::{Compression, FrameFlags};
    ///
    /// assert_eq!(FrameFlags::EMPTY.compression(), Compression::None);
    /// assert_eq!(FrameFlags::LZ4.compression(), Compression::Lz4);
    /// assert_eq!(FrameFlags::ZSTD.compression(), Compression::Zstd);
    /// ```
    #[must_use]
    pub const fn compression(self) -> Compression {
        if self.0 & Self::LZ4.0 != 0 {
            Compression::Lz4
        } else if self.0 & Self::ZSTD.0 != 0 {
            Compression::Zstd
        } else {
            Compression::None
        }
    }

    /// Returns these flags with the compression bits replaced.
    ///
    /// # Examples
    ///
    /// ```
    /// use astrs_wire::{Compression, FrameFlags};
    ///
    /// let flags = FrameFlags::CRC.with_compression(Compression::Lz4);
    /// assert_eq!(flags.bits(), 0b0000_0011);
    /// let flags = flags.with_compression(Compression::None);
    /// assert_eq!(flags.bits(), 0b0000_0001);
    /// ```
    #[must_use]
    pub const fn with_compression(self, compression: Compression) -> Self {
        let cleared = self.0 & !Self::COMPRESSION_MASK;
        match compression {
            Compression::None => Self(cleared),
            Compression::Lz4 => Self(cleared | Self::LZ4.0),
            Compression::Zstd => Self(cleared | Self::ZSTD.0),
        }
    }
}

impl BitOr for FrameFlags {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self {
        Self(self.0 | rhs.0)
    }
}

impl BitOrAssign for FrameFlags {
    fn bitor_assign(&mut self, rhs: Self) {
        self.0 |= rhs.0;
    }
}

impl BitAnd for FrameFlags {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self {
        Self(self.0 & rhs.0)
    }
}

impl Sub for FrameFlags {
    type Output = Self;

    /// Set difference: removes every bit of `rhs` from `self`.
    fn sub(self, rhs: Self) -> Self {
        Self(self.0 & !rhs.0)
    }
}

impl SubAssign for FrameFlags {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 &= !rhs.0;
    }
}

impl fmt::Display for FrameFlags {
    /// Renders as a `|`-separated list of set bits, or `-` when empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use astrs_wire::FrameFlags;
    ///
    /// assert_eq!((FrameFlags::CRC | FrameFlags::LZ4).to_string(), "crc|lz4");
    /// assert_eq!(FrameFlags::EMPTY.to_string(), "-");
    /// ```
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_empty() {
            return f.write_str("-");
        }
        let mut first = true;
        for (flag, name) in [(Self::CRC, "crc"), (Self::LZ4, "lz4"), (Self::ZSTD, "zstd")] {
            if self.contains(flag) {
                if !first {
                    f.write_str("|")?;
                }
                f.write_str(name)?;
                first = false;
            }
        }
        // Any reserved bit that got in through `FrameFlags(..)` internals is
        // still reported, so a diagnostic dump never lies by omission.
        let reserved = self.0 & !Self::KNOWN_MASK;
        if reserved != 0 {
            if !first {
                f.write_str("|")?;
            }
            write!(f, "reserved(0x{reserved:02x})")?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

    use super::*;

    #[test]
    fn bit_positions_match_the_specification() {
        assert_eq!(FrameFlags::CRC.bits(), 0b0000_0001);
        assert_eq!(FrameFlags::LZ4.bits(), 0b0000_0010);
        assert_eq!(FrameFlags::ZSTD.bits(), 0b0000_0100);
        assert_eq!(FrameFlags::EMPTY.bits(), 0);
        assert_eq!(FrameFlags::default(), FrameFlags::EMPTY);
    }

    #[test]
    fn from_bits_accepts_every_valid_combination() {
        let valid = [
            0b0000_0000,
            0b0000_0001,
            0b0000_0010,
            0b0000_0011,
            0b0000_0100,
            0b0000_0101,
        ];
        for bits in valid {
            let flags = FrameFlags::from_bits(bits).unwrap();
            assert_eq!(flags.bits(), bits);
        }
    }

    #[test]
    fn from_bits_rejects_every_reserved_bit() {
        for bit in 3..8u8 {
            let bits = 1u8 << bit;
            match FrameFlags::from_bits(bits) {
                Err(WireError::ReservedFlags { bits: b, reserved }) => {
                    assert_eq!(b, bits);
                    assert_eq!(reserved, bits);
                }
                other => panic!("bit {bit} should be reserved, got {other:?}"),
            }
        }
    }

    #[test]
    fn from_bits_rejects_both_compression_bits() {
        assert!(matches!(
            FrameFlags::from_bits(FrameFlags::COMPRESSION_MASK),
            Err(WireError::ConflictingCompression)
        ));
        assert!(matches!(
            FrameFlags::from_bits(FrameFlags::COMPRESSION_MASK | 1),
            Err(WireError::ConflictingCompression)
        ));
    }

    #[test]
    fn exhaustive_byte_sweep_partitions_valid_and_invalid() {
        let mut accepted = 0;
        for bits in 0u8..=255 {
            match FrameFlags::from_bits(bits) {
                Ok(flags) => {
                    accepted += 1;
                    assert_eq!(flags.bits(), bits);
                    assert!(bits & !FrameFlags::KNOWN_MASK == 0);
                }
                Err(WireError::ReservedFlags { .. }) => {
                    assert_ne!(bits & !FrameFlags::KNOWN_MASK, 0);
                }
                Err(WireError::ConflictingCompression) => {
                    assert_eq!(
                        bits & FrameFlags::COMPRESSION_MASK,
                        FrameFlags::COMPRESSION_MASK
                    );
                    assert_eq!(bits & !FrameFlags::KNOWN_MASK, 0);
                }
                other => panic!("unexpected result for {bits:#010b}: {other:?}"),
            }
        }
        // 0b000, 0b001, 0b010, 0b011, 0b100, 0b101 — six legal bytes.
        assert_eq!(accepted, 6);
    }

    #[test]
    fn truncate_keeps_only_known_bits() {
        assert_eq!(
            FrameFlags::from_bits_truncate(0xFF).bits(),
            FrameFlags::KNOWN_MASK
        );
        assert_eq!(FrameFlags::from_bits_truncate(0x08), FrameFlags::EMPTY);
    }

    #[test]
    fn set_operations_behave() {
        let both = FrameFlags::CRC | FrameFlags::ZSTD;
        assert!(both.contains(FrameFlags::CRC));
        assert!(both.contains(FrameFlags::ZSTD));
        assert!(!both.contains(FrameFlags::LZ4));
        assert_eq!(both & FrameFlags::CRC, FrameFlags::CRC);
        assert_eq!(both - FrameFlags::ZSTD, FrameFlags::CRC);

        let mut acc = FrameFlags::EMPTY;
        acc |= FrameFlags::LZ4;
        assert_eq!(acc, FrameFlags::LZ4);
        acc -= FrameFlags::LZ4;
        assert!(acc.is_empty());
    }

    #[test]
    fn compression_accessors_round_trip() {
        for &compression in Compression::ALL {
            let flags = FrameFlags::CRC.with_compression(compression);
            assert_eq!(flags.compression(), compression);
            assert!(flags.has_crc());
            // Setting compression must not disturb the CRC bit.
            assert_eq!(flags.with_crc(false).compression(), compression);
        }
    }

    #[test]
    fn compression_bits_are_mutually_exclusive_by_construction() {
        let flags = FrameFlags::LZ4.with_compression(Compression::Zstd);
        assert_eq!(flags.bits(), FrameFlags::ZSTD.bits());
        assert_eq!(flags.compression(), Compression::Zstd);
    }

    #[test]
    fn display_lists_set_bits() {
        assert_eq!(FrameFlags::EMPTY.to_string(), "-");
        assert_eq!(FrameFlags::CRC.to_string(), "crc");
        assert_eq!((FrameFlags::CRC | FrameFlags::ZSTD).to_string(), "crc|zstd");
        assert_eq!(
            FrameFlags::from_bits_truncate(0xFF).to_string(),
            "crc|lz4|zstd"
        );
    }

    #[test]
    fn compression_names_and_flags() {
        assert_eq!(Compression::None.to_string(), "none");
        assert_eq!(Compression::Lz4.to_string(), "lz4");
        assert_eq!(Compression::Zstd.to_string(), "zstd");
        assert!(!Compression::None.is_enabled());
        assert!(Compression::Lz4.is_enabled());
        assert!(Compression::Zstd.is_enabled());
        assert_eq!(Compression::default(), Compression::None);
    }

    #[test]
    fn compression_round_trips_through_the_codec() {
        use crate::codec::round_trip;

        for &compression in Compression::ALL {
            assert_eq!(round_trip(&compression).unwrap(), compression);
        }
    }
}