cyfs_lib/events/
category.rs

1use cyfs_base::{BuckyError, BuckyErrorCode, BuckyResult};
2
3use std::fmt;
4use std::str::FromStr;
5
6#[derive(Clone, Debug, Hash, Eq, PartialEq)]
7pub enum RouterEventCategory {
8    TestEvent,
9    ZoneRoleChanged,
10}
11
12impl RouterEventCategory {
13    pub fn as_str(&self) -> &str {
14        match self {
15            Self::TestEvent => "test_event",
16            Self::ZoneRoleChanged => "zone_role_changed",
17        }
18    }
19}
20
21impl fmt::Display for RouterEventCategory {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        fmt::Display::fmt(self.as_str(), f)
24    }
25}
26
27impl FromStr for RouterEventCategory {
28    type Err = BuckyError;
29    fn from_str(s: &str) -> BuckyResult<Self> {
30        let ret = match s {
31            "test_event" => Self::TestEvent,
32            "zone_role_changed" => Self::ZoneRoleChanged,
33
34            v @ _ => {
35                let msg = format!("unknown router event category: {}", v);
36                error!("{}", msg);
37
38                return Err(BuckyError::new(BuckyErrorCode::UnSupport, msg));
39            }
40        };
41
42        Ok(ret)
43    }
44}
45
46pub trait RouterEventCategoryInfo {
47    fn category() -> RouterEventCategory;
48}
49
50
51pub fn extract_router_event_category<P>() -> RouterEventCategory
52where
53    P: RouterEventCategoryInfo,
54{
55    P::category()
56}