use core::fmt;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum KcpSegmentBuildError {
PayloadLengthTooLarge {
length: usize,
},
LengthOverflow {
header_length: usize,
payload_length: usize,
},
BufferTooShort {
required: usize,
available: usize,
},
}
impl fmt::Display for KcpSegmentBuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::PayloadLengthTooLarge { length } => {
write!(f, "KCP payload length {length} exceeds u32::MAX")
}
Self::LengthOverflow {
header_length,
payload_length,
} => write!(
f,
"KCP segment length overflows: header {header_length} plus payload {payload_length}"
),
Self::BufferTooShort {
required,
available,
} => write!(
f,
"KCP buffer is too short: need {required} bytes, have {available}"
),
}
}
}
impl core::error::Error for KcpSegmentBuildError {}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum KcpSegmentParseError {
HeaderTruncated {
required: usize,
available: usize,
},
PayloadLengthNotRepresentable {
value: u32,
},
LengthOverflow {
header_length: usize,
payload_length: usize,
},
PayloadTruncated {
required: usize,
available: usize,
},
}
impl fmt::Display for KcpSegmentParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::HeaderTruncated {
required,
available,
} => write!(
f,
"KCP segment header is truncated: need {required} bytes, have {available}"
),
Self::PayloadLengthNotRepresentable { value } => write!(
f,
"KCP segment payload length {value} cannot be represented on this target"
),
Self::LengthOverflow {
header_length,
payload_length,
} => write!(
f,
"KCP segment length overflows: header {header_length} plus payload {payload_length}"
),
Self::PayloadTruncated {
required,
available,
} => write!(
f,
"KCP segment payload is truncated: need {required} bytes, have {available}"
),
}
}
}
impl core::error::Error for KcpSegmentParseError {}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum KcpSegmentsParseError {
EmptySequence,
Segment {
offset: usize,
error: KcpSegmentParseError,
},
}
impl fmt::Display for KcpSegmentsParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptySequence => f.write_str("KCP segment sequence is empty"),
Self::Segment { offset, error } => {
write!(
f,
"KCP segment at offset {offset} cannot be parsed: {error}"
)
}
}
}
}
impl core::error::Error for KcpSegmentsParseError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Segment { error, .. } => Some(error),
Self::EmptySequence => None,
}
}
}