rama_haproxy/protocol/v2/
error.rs1use std::fmt;
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub enum ParseError {
8 Incomplete(usize),
10 Prefix,
12 Version(u8),
14 Command(u8),
16 AddressFamily(u8),
18 Protocol(u8),
20 Partial(usize, usize),
22 InvalidAddresses(usize, usize),
24 InvalidTLV(u8, u16),
26 Leftovers(usize),
28}
29
30impl fmt::Display for ParseError {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 Self::Incomplete(len) => write!(
34 f,
35 "Expected header to the protocol prefix plus 4 bytes after the prefix (length {len}).",
36 ),
37 Self::Prefix => write!(
38 f,
39 "Expected header to start with a prefix of '\\r\\n\\r\\n\\0\\r\\nQUIT\\n'."
40 ),
41 Self::Version(version) => write!(f, "Expected version {version:X} to be equal to 2."),
42 Self::Command(command) => write!(
43 f,
44 "Invalid command {command:X}. Command must be one of: Local, Proxy.",
45 ),
46 Self::AddressFamily(af) => write!(
47 f,
48 "Invalid Address Family {af:X}. Address Family must be one of: Unspecified, IPv4, IPv6, Unix.",
49 ),
50 Self::Protocol(protocol) => write!(
51 f,
52 "Invalid protocol {protocol:X}. Protocol must be one of: Unspecified, Stream, or Datagram.",
53 ),
54 Self::Partial(len, total) => write!(
55 f,
56 "Header does not contain the advertised length of the address information and TLVs (has {len} out of {total} bytes).",
57 ),
58 Self::InvalidAddresses(len, total) => write!(
59 f,
60 "Header length of {len} bytes cannot store the {total} bytes required for the address family.",
61 ),
62 Self::InvalidTLV(tlv, len) => write!(
63 f,
64 "Header is not long enough to contain TLV {tlv} with length {len}.",
65 ),
66 Self::Leftovers(len) => write!(
67 f,
68 "Header contains leftover {len} bytes not accounted for by the address family or TLVs.",
69 ),
70 }
71 }
72}
73
74impl std::error::Error for ParseError {}