use core::fmt;
use super::packet::header::{QuicConnectionIdField, QuicVersion};
use super::varint::QuicVarIntBuildError;
mod long;
mod short;
mod terminal;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum QuicPacketBuildField {
TokenLength,
ProtectedLength,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum QuicPacketBuildError {
UnusedBitsOutOfRange {
maximum: u8,
actual: u8,
},
ProtectedLowBitsOutOfRange {
maximum: u8,
actual: u8,
},
UnsupportedVersion {
version: QuicVersion,
},
ConnectionIdTooLong {
field: QuicConnectionIdField,
maximum: usize,
actual: usize,
},
EmptyVersionList,
EmptyRetryToken,
ProtectedRemainderTooShort {
minimum: usize,
actual: usize,
},
VarInt {
field: QuicPacketBuildField,
error: QuicVarIntBuildError,
},
LengthOverflow {
component: &'static str,
offset: usize,
length: usize,
},
BufferTooShort {
required: usize,
available: usize,
},
}
impl fmt::Display for QuicPacketBuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnusedBitsOutOfRange { maximum, actual } => write!(
f,
"QUIC unused bits value {actual:#04x} exceeds maximum {maximum:#04x}"
),
Self::ProtectedLowBitsOutOfRange { maximum, actual } => write!(
f,
"QUIC protected low bits value {actual:#04x} exceeds maximum {maximum:#04x}"
),
Self::UnsupportedVersion { version } => write!(
f,
"QUIC packet construction does not support version {:#010x}",
version.raw()
),
Self::ConnectionIdTooLong {
field,
maximum,
actual,
} => write!(
f,
"QUIC {field:?} connection ID length {actual} exceeds maximum {maximum}"
),
Self::EmptyVersionList => write!(
f,
"QUIC Version Negotiation construction requires a nonempty version list"
),
Self::EmptyRetryToken => {
write!(f, "QUIC Retry construction requires a nonempty Retry Token")
}
Self::ProtectedRemainderTooShort { minimum, actual } => write!(
f,
"QUIC protected packet remainder is too short: need at least {minimum} bytes, have {actual}"
),
Self::VarInt { field, error } => {
write!(f, "QUIC {field:?} field cannot be encoded: {error}")
}
Self::LengthOverflow {
component,
offset,
length,
} => write!(
f,
"QUIC packet size overflows while adding {component}: offset {offset} plus length {length}"
),
Self::BufferTooShort {
required,
available,
} => write!(
f,
"QUIC packet destination is too short: need {required} bytes, have {available}"
),
}
}
}
pub use long::{QuicHandshakePacketBuilder, QuicInitialPacketBuilder, QuicZeroRttPacketBuilder};
pub use short::QuicShortPacketBuilder;
pub use terminal::{QuicRetryPacketBuilder, QuicVersionNegotiationPacketBuilder};