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
// Copyright (C) 2018 - Will Glozer. All rights reserved.

use std::convert::TryFrom;
use crate::err::Invalid;
use crate::ffi::route::*;
use crate::sys::Cursor;

#[derive(Debug, Eq, PartialEq)]
pub enum RTA<'a> {
    Dst(&'a [u8]),
    Src(&'a [u8]),
    IIF(u32),
    OIF(u32),
    Gateway(&'a [u8]),
    Unsupported(rtattr, Cursor<'a>),
}

impl<'a> TryFrom<(rtattr, Cursor<'a>)> for RTA<'a> {
    type Error = Invalid;

    fn try_from((attr, tail): (rtattr, Cursor<'a>)) -> Result<RTA<'a>, Self::Error> {
        Ok(match attr.rta_type {
            RTA_DST     => RTA::Dst(tail.bytes()),
            RTA_SRC     => RTA::Src(tail.bytes()),
            RTA_IIF     => RTA::IIF(tail.copy()),
            RTA_OIF     => RTA::OIF(tail.copy()),
            RTA_GATEWAY => RTA::Gateway(tail.bytes()),
            _           => RTA::Unsupported(attr, tail),
        })
    }
}