Skip to content
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

Add error types #15

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
6 changes: 4 additions & 2 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ impl<F> ParseableParametrized<[u8], u16> for GenlMessage<F>
where
F: ParseableParametrized<[u8], GenlHeader> + Debug,
{
type Error = DecodeError;
fn parse_with_param(
buf: &[u8],
message_type: u16,
) -> Result<Self, DecodeError> {
let buf = GenlBuffer::new_checked(buf)?;
Self::parse_with_param(&buf, message_type)
}
}
Expand All @@ -28,7 +28,9 @@ impl<'a, F, T> ParseableParametrized<GenlBuffer<&'a T>, u16> for GenlMessage<F>
where
F: ParseableParametrized<[u8], GenlHeader> + Debug,
T: AsRef<[u8]> + ?Sized,
F::Error: Into<DecodeError>,
{
type Error = DecodeError;
fn parse_with_param(
buf: &GenlBuffer<&'a T>,
message_type: u16,
Expand All @@ -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,
))
}
Expand Down
3 changes: 2 additions & 1 deletion src/ctrl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -133,7 +134,7 @@ impl ParseableParametrized<[u8], GenlHeader> for GenlCtrl {

fn parse_ctrlnlas(buf: &[u8]) -> Result<Vec<GenlCtrlAttrs>, 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::<Result<Vec<_>, _>>()
.context("failed to parse control message attributes")?;

Expand Down
1 change: 1 addition & 0 deletions src/ctrl/nlas/mcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl Nla for McastGrpAttrs {
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
for McastGrpAttrs
{
type Error = DecodeError;
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
let payload = buf.value();
Ok(match buf.kind() {
Expand Down
9 changes: 5 additions & 4 deletions src/ctrl/nlas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl Nla for GenlCtrlAttrs {
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
for GenlCtrlAttrs
{
type Error = DecodeError;
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
let payload = buf.value();
Ok(match buf.kind() {
Expand All @@ -123,10 +124,10 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
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::<Result<Vec<_>, _>>()
})
Expand All @@ -138,10 +139,10 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
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)
})
})
Expand Down
4 changes: 3 additions & 1 deletion src/ctrl/nlas/oppolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ impl Nla for OppolicyAttr {
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for OppolicyAttr {
type Error = DecodeError;
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
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::<Result<Vec<_>, _>>()
.context("failed to parse OppolicyAttr")?;

Expand Down Expand Up @@ -85,6 +86,7 @@ impl Nla for OppolicyIndexAttr {
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
for OppolicyIndexAttr
{
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/ctrl/nlas/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl Nla for OpAttrs {
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for OpAttrs {
type Error = DecodeError;
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
let payload = buf.value();
Ok(match buf.kind() {
Expand Down
5 changes: 4 additions & 1 deletion src/ctrl/nlas/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl Nla for PolicyAttr {
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for PolicyAttr {
type Error = DecodeError;
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
let payload = buf.value();

Expand Down Expand Up @@ -81,10 +82,11 @@ impl Nla for AttributePolicyAttr {
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
for AttributePolicyAttr
{
type Error = DecodeError;
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
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::<Result<Vec<_>, _>>()
.context("failed to parse AttributePolicyAttr")?;

Expand Down Expand Up @@ -168,6 +170,7 @@ impl Nla for NlPolicyTypeAttrs {
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
for NlPolicyTypeAttrs
{
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/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Emitable for GenlHeader {
}

impl<T: AsRef<[u8]>> Parseable<GenlBuffer<T>> for GenlHeader {
type Error = DecodeError;
fn parse(buf: &GenlBuffer<T>) -> Result<Self, DecodeError> {
Ok(Self {
cmd: buf.cmd(),
Expand Down
1 change: 1 addition & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ where
impl<F> NetlinkDeserializable for GenlMessage<F>
where
F: ParseableParametrized<[u8], GenlHeader> + Debug,
F::Error: Into<DecodeError>,
{
type Error = DecodeError;
fn deserialize(
Expand Down