From fc7def6958b9072c7f682fc36374536921471970 Mon Sep 17 00:00:00 2001 From: Brian Bosak Date: Tue, 23 Apr 2024 22:33:04 -0500 Subject: [PATCH] Add error types --- src/buffer.rs | 6 ++++-- src/ctrl/mod.rs | 3 ++- src/ctrl/nlas/mcast.rs | 1 + src/ctrl/nlas/mod.rs | 9 +++++---- src/ctrl/nlas/oppolicy.rs | 4 +++- src/ctrl/nlas/ops.rs | 1 + src/ctrl/nlas/policy.rs | 5 ++++- src/header.rs | 1 + src/message.rs | 1 + 9 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 87f61ec..ec184ad 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -15,11 +15,11 @@ impl ParseableParametrized<[u8], u16> for GenlMessage where F: ParseableParametrized<[u8], GenlHeader> + Debug, { + type Error = DecodeError; fn parse_with_param( buf: &[u8], message_type: u16, ) -> Result { - let buf = GenlBuffer::new_checked(buf)?; Self::parse_with_param(&buf, message_type) } } @@ -28,7 +28,9 @@ impl<'a, F, T> ParseableParametrized, u16> for GenlMessage where F: ParseableParametrized<[u8], GenlHeader> + Debug, T: AsRef<[u8]> + ?Sized, + F::Error: Into, { + type Error = DecodeError; fn parse_with_param( buf: &GenlBuffer<&'a T>, message_type: u16, @@ -37,7 +39,7 @@ where let payload_buf = buf.payload(); Ok(GenlMessage::new( header, - F::parse_with_param(payload_buf, header)?, + F::parse_with_param(payload_buf, header).map_err(|err|err.into())?, message_type, )) } diff --git a/src/ctrl/mod.rs b/src/ctrl/mod.rs index 98525ac..0f950ec 100644 --- a/src/ctrl/mod.rs +++ b/src/ctrl/mod.rs @@ -120,6 +120,7 @@ impl Emitable for GenlCtrl { } impl ParseableParametrized<[u8], GenlHeader> for GenlCtrl { + type Error = DecodeError; fn parse_with_param( buf: &[u8], header: GenlHeader, @@ -133,7 +134,7 @@ impl ParseableParametrized<[u8], GenlHeader> for GenlCtrl { fn parse_ctrlnlas(buf: &[u8]) -> Result, DecodeError> { let nlas = NlasIterator::new(buf) - .map(|nla| nla.and_then(|nla| GenlCtrlAttrs::parse(&nla))) + .map(|nla| nla.map_err(|err|DecodeError::Nla(err)).and_then(|nla| GenlCtrlAttrs::parse(&nla))) .collect::, _>>() .context("failed to parse control message attributes")?; diff --git a/src/ctrl/nlas/mcast.rs b/src/ctrl/nlas/mcast.rs index 4080d6d..67c2486 100644 --- a/src/ctrl/nlas/mcast.rs +++ b/src/ctrl/nlas/mcast.rs @@ -95,6 +95,7 @@ impl Nla for McastGrpAttrs { impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for McastGrpAttrs { + type Error = DecodeError; fn parse(buf: &NlaBuffer<&'a T>) -> Result { let payload = buf.value(); Ok(match buf.kind() { diff --git a/src/ctrl/nlas/mod.rs b/src/ctrl/nlas/mod.rs index d4317af..00d6704 100644 --- a/src/ctrl/nlas/mod.rs +++ b/src/ctrl/nlas/mod.rs @@ -97,6 +97,7 @@ impl Nla for GenlCtrlAttrs { impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for GenlCtrlAttrs { + type Error = DecodeError; fn parse(buf: &NlaBuffer<&'a T>) -> Result { let payload = buf.value(); Ok(match buf.kind() { @@ -123,10 +124,10 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> CTRL_ATTR_OPS => { let ops = NlasIterator::new(payload) .map(|nlas| { - nlas.and_then(|nlas| { + nlas.map_err(|err|DecodeError::from(err)).and_then(|nlas| { NlasIterator::new(nlas.value()) .map(|nla| { - nla.and_then(|nla| OpAttrs::parse(&nla)) + nla.map_err(|err|DecodeError::from(err)).and_then(|nla| OpAttrs::parse(&nla)) }) .collect::, _>>() }) @@ -138,10 +139,10 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> CTRL_ATTR_MCAST_GROUPS => { let groups = NlasIterator::new(payload) .map(|nlas| { - nlas.and_then(|nlas| { + nlas.map_err(|err|DecodeError::from(err)).and_then(|nlas| { NlasIterator::new(nlas.value()) .map(|nla| { - nla.and_then(|nla| { + nla.map_err(|err|DecodeError::from(err)).and_then(|nla| { McastGrpAttrs::parse(&nla) }) }) diff --git a/src/ctrl/nlas/oppolicy.rs b/src/ctrl/nlas/oppolicy.rs index ca41c78..b1edc7f 100644 --- a/src/ctrl/nlas/oppolicy.rs +++ b/src/ctrl/nlas/oppolicy.rs @@ -36,10 +36,11 @@ impl Nla for OppolicyAttr { } impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for OppolicyAttr { + type Error = DecodeError; fn parse(buf: &NlaBuffer<&'a T>) -> Result { let payload = buf.value(); let policy_idx = NlasIterator::new(payload) - .map(|nla| nla.and_then(|nla| OppolicyIndexAttr::parse(&nla))) + .map(|nla| nla.map_err(|err|DecodeError::from(err)).and_then(|nla| OppolicyIndexAttr::parse(&nla))) .collect::, _>>() .context("failed to parse OppolicyAttr")?; @@ -85,6 +86,7 @@ impl Nla for OppolicyIndexAttr { impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for OppolicyIndexAttr { + type Error = DecodeError; fn parse(buf: &NlaBuffer<&'a T>) -> Result { let payload = buf.value(); Ok(match buf.kind() { diff --git a/src/ctrl/nlas/ops.rs b/src/ctrl/nlas/ops.rs index 454096c..c5b0629 100644 --- a/src/ctrl/nlas/ops.rs +++ b/src/ctrl/nlas/ops.rs @@ -89,6 +89,7 @@ impl Nla for OpAttrs { } impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for OpAttrs { + type Error = DecodeError; fn parse(buf: &NlaBuffer<&'a T>) -> Result { let payload = buf.value(); Ok(match buf.kind() { diff --git a/src/ctrl/nlas/policy.rs b/src/ctrl/nlas/policy.rs index 50de9fb..fa1ba7a 100644 --- a/src/ctrl/nlas/policy.rs +++ b/src/ctrl/nlas/policy.rs @@ -41,6 +41,7 @@ impl Nla for PolicyAttr { } impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for PolicyAttr { + type Error = DecodeError; fn parse(buf: &NlaBuffer<&'a T>) -> Result { let payload = buf.value(); @@ -81,10 +82,11 @@ impl Nla for AttributePolicyAttr { impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for AttributePolicyAttr { + type Error = DecodeError; fn parse(buf: &NlaBuffer<&'a T>) -> Result { let payload = buf.value(); let policies = NlasIterator::new(payload) - .map(|nla| nla.and_then(|nla| NlPolicyTypeAttrs::parse(&nla))) + .map(|nla| nla.map_err(|err|DecodeError::from(err)).and_then(|nla| NlPolicyTypeAttrs::parse(&nla))) .collect::, _>>() .context("failed to parse AttributePolicyAttr")?; @@ -168,6 +170,7 @@ impl Nla for NlPolicyTypeAttrs { impl<'a, T: AsRef<[u8]> + ?Sized> Parseable> for NlPolicyTypeAttrs { + type Error = DecodeError; fn parse(buf: &NlaBuffer<&'a T>) -> Result { let payload = buf.value(); Ok(match buf.kind() { diff --git a/src/header.rs b/src/header.rs index 4107def..4737135 100644 --- a/src/header.rs +++ b/src/header.rs @@ -24,6 +24,7 @@ impl Emitable for GenlHeader { } impl> Parseable> for GenlHeader { + type Error = DecodeError; fn parse(buf: &GenlBuffer) -> Result { Ok(Self { cmd: buf.cmd(), diff --git a/src/message.rs b/src/message.rs index 2a1f0e1..e8f10ac 100644 --- a/src/message.rs +++ b/src/message.rs @@ -165,6 +165,7 @@ where impl NetlinkDeserializable for GenlMessage where F: ParseableParametrized<[u8], GenlHeader> + Debug, + F::Error: Into, { type Error = DecodeError; fn deserialize(