netlink_packet_route/link/proto_info/
bridge.rs

1// SPDX-License-Identifier: MIT
2
3use anyhow::Context;
4use netlink_packet_utils::nla::{DefaultNla, Nla, NlaBuffer, NlasIterator};
5use netlink_packet_utils::traits::Parseable;
6use netlink_packet_utils::DecodeError;
7
8#[derive(Clone, Eq, PartialEq, Debug)]
9#[non_exhaustive]
10pub enum LinkProtoInfoBridge {
11    Other(DefaultNla),
12}
13
14pub(crate) struct VecLinkProtoInfoBridge(pub(crate) Vec<LinkProtoInfoBridge>);
15
16impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for VecLinkProtoInfoBridge {
17    type Error = DecodeError;
18    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
19        let mut nlas = vec![];
20        for nla in NlasIterator::new(buf.into_inner()) {
21            let nla = nla.context(format!("invalid bridge IFLA_PROTINFO {:?}", buf.value()))?;
22            nlas.push(LinkProtoInfoBridge::parse(&nla)?);
23        }
24        Ok(Self(nlas))
25    }
26}
27
28impl Nla for LinkProtoInfoBridge {
29    fn value_len(&self) -> usize {
30        match *self {
31            Self::Other(ref nla) => nla.value_len(),
32        }
33    }
34
35    fn emit_value(&self, buffer: &mut [u8]) {
36        match *self {
37            Self::Other(ref nla) => nla.emit_value(buffer),
38        }
39    }
40
41    fn kind(&self) -> u16 {
42        match *self {
43            Self::Other(ref nla) => nla.kind(),
44        }
45    }
46}
47
48impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for LinkProtoInfoBridge {
49    type Error = DecodeError;
50    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
51        Ok(Self::Other(
52            DefaultNla::parse(buf)
53                .context(format!("invalid bridge IFLA_PROTINFO {:?}", buf.value()))?,
54        ))
55    }
56}