Skip to content

Add error types #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl<'a, T: AsRef<[u8]> + AsMut<[u8]> + ?Sized> SockDiagBuffer<&'a mut T> {
impl<'a, T: AsRef<[u8]>> ParseableParametrized<SockDiagBuffer<&'a T>, u16>
for SockDiagMessage
{
type Error = DecodeError;
fn parse_with_param(
buf: &SockDiagBuffer<&'a T>,
message_type: u16,
Expand Down
3 changes: 3 additions & 0 deletions src/inet/nlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct LegacyMemInfo {
}

impl<T: AsRef<[u8]>> Parseable<LegacyMemInfoBuffer<T>> for LegacyMemInfo {
type Error = DecodeError;
fn parse(buf: &LegacyMemInfoBuffer<T>) -> Result<Self, DecodeError> {
Ok(Self {
receive_queue: buf.receive_queue(),
Expand Down Expand Up @@ -184,6 +185,7 @@ pub struct MemInfo {
}

impl<T: AsRef<[u8]>> Parseable<MemInfoBuffer<T>> for MemInfo {
type Error = DecodeError;
fn parse(buf: &MemInfoBuffer<T>) -> Result<Self, DecodeError> {
Ok(Self {
receive_queue: buf.receive_queue(),
Expand Down Expand Up @@ -314,6 +316,7 @@ impl nla::Nla for Nla {
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Nla {
type Error = DecodeError;
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
let payload = buf.value();
Ok(match buf.kind() {
Expand Down
1 change: 1 addition & 0 deletions src/inet/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ bitflags! {
impl<'a, T: AsRef<[u8]> + 'a> Parseable<InetRequestBuffer<&'a T>>
for InetRequest
{
type Error = DecodeError;
fn parse(buf: &InetRequestBuffer<&'a T>) -> Result<Self, DecodeError> {
let err = "invalid socket_id value";
let socket_id = SocketId::parse_with_param(
Expand Down
5 changes: 4 additions & 1 deletion src/inet/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub struct InetResponseHeader {
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<InetResponseBuffer<&'a T>>
for InetResponseHeader
{
type Error = DecodeError;
fn parse(buf: &InetResponseBuffer<&'a T>) -> Result<Self, DecodeError> {
let err = "invalid socket_id value";
let socket_id = SocketId::parse_with_param(
Expand Down Expand Up @@ -170,13 +171,14 @@ impl<'a, T: AsRef<[u8]> + ?Sized> InetResponseBuffer<&'a T> {
pub fn nlas(
&self,
) -> impl Iterator<Item = Result<NlaBuffer<&'a [u8]>, DecodeError>> {
NlasIterator::new(self.payload())
NlasIterator::new(self.payload()).map(|res| res.map_err(Into::into))
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<InetResponseBuffer<&'a T>>
for SmallVec<[Nla; 8]>
{
type Error = DecodeError;
fn parse(buf: &InetResponseBuffer<&'a T>) -> Result<Self, DecodeError> {
let mut nlas = smallvec![];
for nla_buf in buf.nlas() {
Expand All @@ -189,6 +191,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<InetResponseBuffer<&'a T>>
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<InetResponseBuffer<&'a T>>
for InetResponse
{
type Error = DecodeError;
fn parse(buf: &InetResponseBuffer<&'a T>) -> Result<Self, DecodeError> {
let header = InetResponseHeader::parse(buf)
.context("failed to parse inet response header")?;
Expand Down
1 change: 1 addition & 0 deletions src/inet/socket_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl SocketId {
impl<'a, T: AsRef<[u8]> + 'a> ParseableParametrized<SocketIdBuffer<&'a T>, u8>
for SocketId
{
type Error = DecodeError;
fn parse_with_param(
buf: &SocketIdBuffer<&'a T>,
af: u8,
Expand Down
3 changes: 3 additions & 0 deletions src/unix/nlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub struct Vfs {
}

impl<T: AsRef<[u8]>> Parseable<VfsBuffer<T>> for Vfs {
type Error = DecodeError;
fn parse(buf: &VfsBuffer<T>) -> Result<Self, DecodeError> {
Ok(Self {
inode: buf.inode(),
Expand Down Expand Up @@ -214,6 +215,7 @@ pub struct MemInfo {
}

impl<T: AsRef<[u8]>> Parseable<MemInfoBuffer<T>> for MemInfo {
type Error = DecodeError;
fn parse(buf: &MemInfoBuffer<T>) -> Result<Self, DecodeError> {
Ok(Self {
so_rcvbuf: buf.so_rcvbuf(),
Expand Down Expand Up @@ -298,6 +300,7 @@ impl nla::Nla for Nla {
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Nla {
type Error = DecodeError;
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
let payload = buf.value();
Ok(match buf.kind() {
Expand Down
1 change: 1 addition & 0 deletions src/unix/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ bitflags! {
impl<'a, T: AsRef<[u8]> + 'a> Parseable<UnixRequestBuffer<&'a T>>
for UnixRequest
{
type Error = DecodeError;
fn parse(buf: &UnixRequestBuffer<&'a T>) -> Result<Self, DecodeError> {
Ok(Self {
state_flags: StateFlags::from_bits_truncate(buf.state_flags()),
Expand Down
5 changes: 4 additions & 1 deletion src/unix/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct UnixResponseHeader {
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<UnixResponseBuffer<&'a T>>
for UnixResponseHeader
{
type Error = DecodeError;
fn parse(buf: &UnixResponseBuffer<&'a T>) -> Result<Self, DecodeError> {
Ok(Self {
kind: buf.kind(),
Expand Down Expand Up @@ -187,13 +188,14 @@ impl<'a, T: AsRef<[u8]> + ?Sized> UnixResponseBuffer<&'a T> {
pub fn nlas(
&self,
) -> impl Iterator<Item = Result<NlaBuffer<&'a [u8]>, DecodeError>> {
NlasIterator::new(self.payload())
NlasIterator::new(self.payload()).map(|nla| nla.map_err(Into::into))
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<UnixResponseBuffer<&'a T>>
for SmallVec<[Nla; 8]>
{
type Error = DecodeError;
fn parse(buf: &UnixResponseBuffer<&'a T>) -> Result<Self, DecodeError> {
let mut nlas = smallvec![];
for nla_buf in buf.nlas() {
Expand All @@ -206,6 +208,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<UnixResponseBuffer<&'a T>>
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<UnixResponseBuffer<&'a T>>
for UnixResponse
{
type Error = DecodeError;
fn parse(buf: &UnixResponseBuffer<&'a T>) -> Result<Self, DecodeError> {
let header = UnixResponseHeader::parse(buf)
.context("failed to parse inet response header")?;
Expand Down