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