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
// Copyright 2017 Amagicom AB.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::{ffi, pooladdr::PoolAddr};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Route {
    NoRoute,
    RouteTo(PoolAddr),
    ReplyTo(PoolAddr),
    DupTo(PoolAddr),
}

impl Default for Route {
    fn default() -> Self {
        Route::NoRoute
    }
}

impl Route {
    pub fn route_to<T: Into<PoolAddr>>(pool_addr: T) -> Self {
        Route::RouteTo(pool_addr.into())
    }

    pub fn reply_to<T: Into<PoolAddr>>(pool_addr: T) -> Self {
        Route::ReplyTo(pool_addr.into())
    }

    pub fn dup_to<T: Into<PoolAddr>>(pool_addr: T) -> Self {
        Route::DupTo(pool_addr.into())
    }

    pub fn get_pool_addr(&self) -> Option<&PoolAddr> {
        match *self {
            Route::NoRoute => None,
            Route::RouteTo(ref pool_addr) => Some(pool_addr),
            Route::ReplyTo(ref pool_addr) => Some(pool_addr),
            Route::DupTo(ref pool_addr) => Some(pool_addr),
        }
    }
}

impl<'a> From<&'a Route> for u8 {
    fn from(route: &'a Route) -> u8 {
        match *route {
            Route::NoRoute => ffi::pfvar::PF_NOPFROUTE as u8,
            Route::RouteTo(_) => ffi::pfvar::PF_ROUTETO as u8,
            Route::ReplyTo(_) => ffi::pfvar::PF_REPLYTO as u8,
            Route::DupTo(_) => ffi::pfvar::PF_DUPTO as u8,
        }
    }
}