1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SPDX-License-Identifier: MIT

use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
    nla::{DefaultNla, Nla, NlaBuffer},
    parsers::parse_u32,
    DecodeError, Parseable,
};

const IFLA_PROTO_DOWN_REASON_MASK: u16 = 1;
const IFLA_PROTO_DOWN_REASON_VALUE: u16 = 2;

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum LinkProtocolDownReason {
    Value(u32),
    Mask(u32),
    Other(DefaultNla),
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
    for LinkProtocolDownReason
{
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        let payload = buf.value();
        Ok(match buf.kind() {
            IFLA_PROTO_DOWN_REASON_MASK => {
                Self::Mask(parse_u32(payload).context(format!(
                    "invalid IFLA_PROTO_DOWN_REASON_MASK {payload:?}"
                ))?)
            }
            IFLA_PROTO_DOWN_REASON_VALUE => {
                Self::Value(parse_u32(payload).context(format!(
                    "invalid IFLA_PROTO_DOWN_REASON_MASK {payload:?}"
                ))?)
            }
            kind => Self::Other(DefaultNla::parse(buf).context(format!(
                "unknown NLA type {kind} for IFLA_PROTO_DOWN_REASON: \
                {payload:?}"
            ))?),
        })
    }
}

impl Nla for LinkProtocolDownReason {
    fn kind(&self) -> u16 {
        match self {
            Self::Value(_) => IFLA_PROTO_DOWN_REASON_VALUE,
            Self::Mask(_) => IFLA_PROTO_DOWN_REASON_MASK,
            Self::Other(v) => v.kind(),
        }
    }

    fn value_len(&self) -> usize {
        match self {
            Self::Value(_) | Self::Mask(_) => 4,
            Self::Other(v) => v.value_len(),
        }
    }

    fn emit_value(&self, buffer: &mut [u8]) {
        match self {
            Self::Value(v) | Self::Mask(v) => {
                NativeEndian::write_u32(buffer, *v)
            }
            Self::Other(v) => v.emit_value(buffer),
        }
    }
}