Skip to content

Commit

Permalink
implemented std::error::Error for error enums
Browse files Browse the repository at this point in the history
this makes idiomatic error propagation easier and
allows utilizing crates like thiserror or anyhow
  • Loading branch information
babymotte authored and dholroyd committed Mar 11, 2024
1 parent b3808be commit 65202ee
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,23 @@ impl<'a> RtpPacketBuilder<'a> {
}
}

impl std::error::Error for RtpPacketBuildError {}

impl std::fmt::Display for RtpPacketBuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
RtpPacketBuildError::BufferTooSmall => "buffer too small",
RtpPacketBuildError::PayloadTypeInvalid => "payload type invalid",
RtpPacketBuildError::ExtensionTooLarge => "extensions too large",
RtpPacketBuildError::ExtensionMissingPadding => "extension missing padding",
}
)
}
}

#[cfg(test)]
mod test {
use crate::{RtpPacketBuilder, Pad};
Expand Down
22 changes: 22 additions & 0 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,28 @@ impl<'a> fmt::Debug for RtpReader<'a> {
}
}

impl std::error::Error for RtpReaderError {}

impl std::fmt::Display for RtpReaderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
RtpReaderError::BufferTooShort(b) => format!("buffer too short: {b}"),
RtpReaderError::UnsupportedVersion(v) => format!("unsupported version: {v}"),
RtpReaderError::HeadersTruncated {
header_len,
buffer_len,
} => format!(
"headers truncated: header length: {header_len}; buffer length: {buffer_len}"
),
RtpReaderError::PaddingLengthInvalid(p) => format!("padding length invalid: {p}"),
}
)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 65202ee

Please sign in to comment.