srt_runtime/error.rs
1//! Error type for SRT packet parsing and serialization.
2//!
3//! Spec grounding: `draft-sharabayko-srt-01` §3 (Packet Structure).
4
5/// Result alias for SRT packet parsing/serialization.
6pub type Result<T> = core::result::Result<T, Error>;
7
8/// An SRT packet parse / serialize error.
9#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12 /// Input shorter than required.
13 #[error("buffer too short: need {need}, have {have} ({what})")]
14 BufferTooShort {
15 /// Bytes required.
16 need: usize,
17 /// Bytes available.
18 have: usize,
19 /// What was being parsed.
20 what: &'static str,
21 },
22 /// The output buffer passed to `serialize_into` was too small.
23 #[error("output buffer too small: need {need}, have {have}")]
24 OutputBufferTooSmall {
25 /// Bytes required.
26 need: usize,
27 /// Bytes available.
28 have: usize,
29 },
30 /// A field value did not fit in its wire bit-width.
31 #[error("field {what} value {value} does not fit in {bits} bits")]
32 FieldTooWide {
33 /// The over-wide field name.
34 what: &'static str,
35 /// The offending value.
36 value: u64,
37 /// The field width on the wire.
38 bits: u32,
39 },
40 /// The Control Information Field length did not match any known ACK
41 /// variant (Full / Small / Light — `draft-sharabayko-srt-01` §3.2.4).
42 #[error("ACK CIF length {len} does not match Full (28), Small (16), or Light (4)")]
43 InvalidAckLength {
44 /// The CIF length actually found, in bytes.
45 len: usize,
46 },
47 /// A NAK loss-list (`draft-sharabayko-srt-01` Appendix A) was not a whole
48 /// number of 4-byte entries, or a range entry's second word had its top
49 /// bit set (which would make it a nested range).
50 #[error("invalid NAK loss list: {reason}")]
51 InvalidLossList {
52 /// Why the loss list is invalid.
53 reason: &'static str,
54 },
55 /// A Handshake Extension block's declared length (in 4-byte units)
56 /// overran the remaining CIF bytes (`draft-sharabayko-srt-01` §3.2.1).
57 #[error("handshake extension length {declared} * 4 bytes overruns {remaining} remaining")]
58 ExtensionOverrun {
59 /// The declared `Extension Length` (in 4-byte blocks).
60 declared: u16,
61 /// The bytes actually remaining in the CIF.
62 remaining: usize,
63 },
64 /// The Stream ID extension contents were not valid UTF-8 after undoing the
65 /// 32-bit little-endian word storage (`draft-sharabayko-srt-01` §3.2.1.3).
66 #[error("invalid Stream ID extension UTF-8")]
67 InvalidStreamIdUtf8,
68 /// A Key Material message (`draft-sharabayko-srt-01` §3.2.2) fixed-value
69 /// field did not carry its mandated value.
70 #[error("invalid key material {field}: {reason}")]
71 InvalidKeyMaterial {
72 /// The offending field.
73 field: &'static str,
74 /// Why it is invalid.
75 reason: &'static str,
76 },
77 /// A type-specific `parse` was called on bytes whose `F` bit (or Control
78 /// Type) indicated the other packet kind.
79 #[error("wrong packet kind: expected {expected}")]
80 WrongPacketKind {
81 /// What the caller expected to parse.
82 expected: &'static str,
83 },
84 /// A field documented as reserved / must-be-zero (`draft-sharabayko-srt-01`
85 /// §3.2, e.g. `Subtype` on a defined control type, or the header
86 /// `Type-specific Information` word where the packet type does not use
87 /// it) carried a non-zero value.
88 #[error("reserved field {what} must be zero, found {value:#x}")]
89 ReservedFieldNotZero {
90 /// The offending field.
91 what: &'static str,
92 /// The non-zero value found.
93 value: u64,
94 },
95 /// A general structural constraint (not a bit-width overflow) was
96 /// violated — e.g. a length that must be a whole number of 4-byte words.
97 #[error("invalid {what}: {reason}")]
98 InvalidField {
99 /// The offending field/value.
100 what: &'static str,
101 /// Why it is invalid.
102 reason: &'static str,
103 },
104 /// A Control Information Field documented as absent (Keep-Alive,
105 /// Congestion Warning, Shutdown, ACKACK, Peer Error — §3.2) carried extra
106 /// bytes.
107 #[error("{what} has {extra} unexpected trailing byte(s)")]
108 UnexpectedTrailingBytes {
109 /// What was being parsed.
110 what: &'static str,
111 /// How many bytes were left over.
112 extra: usize,
113 },
114}