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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use cyfs_base::*;

use serde_json::{Map, Value};
use std::fmt;
use std::str::FromStr;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RouterHandlerAction {
    Default,
    Response,

    Reject,
    Drop,

    Pass,
}

impl RouterHandlerAction {
    pub fn to_error_code(&self) -> BuckyErrorCode {
        match *self {
            RouterHandlerAction::Reject => BuckyErrorCode::Reject,
            RouterHandlerAction::Drop => BuckyErrorCode::Ignored,

            _ => BuckyErrorCode::Ok,
        }
    }

    pub fn is_action_error(e: &BuckyError) -> bool {
        Self::is_action_error_code(&e.code())
    }

    pub fn is_action_error_code(code: &BuckyErrorCode) -> bool {
        match code {
            BuckyErrorCode::Reject | BuckyErrorCode::Ignored => true,
            _ => false,
        }
    }
}

impl fmt::Display for RouterHandlerAction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match &*self {
            Self::Default => "Default",
            Self::Response => "Response",

            Self::Reject => "Reject",
            Self::Drop => "Drop",

            Self::Pass => "Pass",
        };

        fmt::Display::fmt(s, f)
    }
}

impl FromStr for RouterHandlerAction {
    type Err = BuckyError;
    fn from_str(s: &str) -> BuckyResult<Self> {
        let ret = match s {
            "Default" => Self::Default,
            "Response" => Self::Response,

            "Reject" => Self::Reject,
            "Drop" => Self::Drop,

            "Pass" => Self::Pass,
            v @ _ => {
                let msg = format!("unknown router handler action: {}", v);
                error!("{}", msg);

                return Err(BuckyError::new(BuckyErrorCode::UnSupport, msg));
            }
        };

        Ok(ret)
    }
}

impl JsonCodec<RouterHandlerAction> for RouterHandlerAction {
    fn encode_json(&self) -> Map<String, Value> {
        let mut obj = Map::new();
        obj.insert("action".to_owned(), Value::String(self.to_string()));

        obj
    }

    fn decode_json(obj: &Map<String, Value>) -> BuckyResult<Self> {
        let action = obj.get("action").ok_or_else(|| {
            error!("action field missed! {:?}", obj);
            BuckyError::from(BuckyErrorCode::InvalidFormat)
        })?;

        let action = action.as_str().unwrap_or("");
        let ret = Self::from_str(action)?;

        Ok(ret)
    }
}