netlink_packet_route/rtnl/link/nlas/
prop_list.rs

1// SPDX-License-Identifier: MIT
2
3use crate::{
4    constants::*,
5    nlas::{DefaultNla, Nla, NlaBuffer},
6    parsers::parse_string,
7    traits::Parseable,
8    DecodeError,
9};
10
11use anyhow::Context;
12
13#[derive(Debug, PartialEq, Eq, Clone)]
14pub enum Prop {
15    AltIfName(String),
16    Other(DefaultNla),
17}
18
19impl Nla for Prop {
20    #[rustfmt::skip]
21    fn value_len(&self) -> usize {
22        use self::Prop::*;
23        match self {
24            AltIfName(ref string) => string.as_bytes().len() + 1,
25            Other(nla) => nla.value_len()
26        }
27    }
28
29    #[rustfmt::skip]
30    fn emit_value(&self, buffer: &mut [u8]) {
31        use self::Prop::*;
32        match self {
33            AltIfName(ref string) => {
34                buffer[..string.len()].copy_from_slice(string.as_bytes());
35                buffer[string.len()] = 0;
36            },
37            Other(nla) => nla.emit_value(buffer)
38        }
39    }
40
41    fn kind(&self) -> u16 {
42        use self::Prop::*;
43        match self {
44            AltIfName(_) => IFLA_ALT_IFNAME,
45            Other(nla) => nla.kind(),
46        }
47    }
48}
49
50impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Prop {
51    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
52        let payload = buf.value();
53        Ok(match buf.kind() {
54            IFLA_ALT_IFNAME => {
55                Prop::AltIfName(parse_string(payload).context("invalid IFLA_ALT_IFNAME value")?)
56            }
57            kind => {
58                Prop::Other(DefaultNla::parse(buf).context(format!("Unknown NLA type {}", kind))?)
59            }
60        })
61    }
62}