cyfs_lib/router_handler/
category.rs

1use super::request::*;
2use cyfs_base::{BuckyError, BuckyErrorCode, BuckyResult};
3
4use std::fmt;
5use std::str::FromStr;
6
7#[derive(Clone, Copy, Hash, Eq, PartialEq)]
8pub enum RouterHandlerCategory {
9    PutObject,
10    GetObject,
11
12    PostObject,
13
14    SelectObject,
15    DeleteObject,
16
17    GetData,
18    PutData,
19    DeleteData,
20
21    SignObject,
22    VerifyObject,
23    EncryptData,
24    DecryptData,
25
26    Acl, 
27    Interest, 
28}
29
30impl RouterHandlerCategory {
31    pub fn as_str(&self) -> &str {
32        match &*self {
33            Self::PutObject => "put_object",
34            Self::GetObject => "get_object",
35
36            Self::PostObject => "post_object",
37
38            Self::SelectObject => "select_object",
39            Self::DeleteObject => "delete_object",
40
41            Self::GetData => "get_data",
42            Self::PutData => "put_data",
43            Self::DeleteData => "delete_data",
44
45            Self::SignObject => "sign_object",
46            Self::VerifyObject => "verify_object",
47            Self::EncryptData => "encrypt_data",
48            Self::DecryptData => "decrypt_data",
49
50            Self::Acl => "acl", 
51            Self::Interest => "interest", 
52        }
53    }
54}
55
56impl fmt::Debug for RouterHandlerCategory {
57    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58        write!(f, "{}", self.as_str())
59    }
60}
61
62impl fmt::Display for RouterHandlerCategory {
63    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64        write!(f, "{}", self.as_str())
65    }
66}
67
68impl FromStr for RouterHandlerCategory {
69    type Err = BuckyError;
70    fn from_str(s: &str) -> BuckyResult<Self> {
71        let ret = match s {
72            "put_object" => Self::PutObject,
73            "get_object" => Self::GetObject,
74
75            "post_object" => Self::PostObject,
76
77            "select_object" => Self::SelectObject,
78            "delete_object" => Self::DeleteObject,
79
80            "get_data" => Self::GetData,
81            "put_data" => Self::PutData,
82            "delete_data" => Self::DeleteData,
83
84            "sign_object" => Self::SignObject,
85            "verify_object" => Self::VerifyObject,
86            "encrypt_data" => Self::EncryptData,
87            "decrypt_data" => Self::DecryptData,
88
89            "acl" => Self::Acl,
90
91            "interest" => Self::Interest, 
92
93            v @ _ => {
94                let msg = format!("unknown router handler category: {}", v);
95                error!("{}", msg);
96
97                return Err(BuckyError::new(BuckyErrorCode::UnSupport, msg));
98            }
99        };
100
101        Ok(ret)
102    }
103}
104
105pub trait RouterHandlerCategoryInfo {
106    fn category() -> RouterHandlerCategory;
107}
108
109impl RouterHandlerCategoryInfo for RouterHandlerPutObjectRequest {
110    fn category() -> RouterHandlerCategory {
111        RouterHandlerCategory::PutObject
112    }
113}
114
115impl RouterHandlerCategoryInfo for RouterHandlerGetObjectRequest {
116    fn category() -> RouterHandlerCategory {
117        RouterHandlerCategory::GetObject
118    }
119}
120
121impl RouterHandlerCategoryInfo for RouterHandlerPostObjectRequest {
122    fn category() -> RouterHandlerCategory {
123        RouterHandlerCategory::PostObject
124    }
125}
126
127impl RouterHandlerCategoryInfo for RouterHandlerSelectObjectRequest {
128    fn category() -> RouterHandlerCategory {
129        RouterHandlerCategory::SelectObject
130    }
131}
132
133impl RouterHandlerCategoryInfo for RouterHandlerDeleteObjectRequest {
134    fn category() -> RouterHandlerCategory {
135        RouterHandlerCategory::DeleteObject
136    }
137}
138
139impl RouterHandlerCategoryInfo for RouterHandlerPutDataRequest {
140    fn category() -> RouterHandlerCategory {
141        RouterHandlerCategory::PutData
142    }
143}
144
145impl RouterHandlerCategoryInfo for RouterHandlerGetDataRequest {
146    fn category() -> RouterHandlerCategory {
147        RouterHandlerCategory::GetData
148    }
149}
150
151impl RouterHandlerCategoryInfo for RouterHandlerDeleteDataRequest {
152    fn category() -> RouterHandlerCategory {
153        RouterHandlerCategory::DeleteData
154    }
155}
156
157
158impl RouterHandlerCategoryInfo for RouterHandlerSignObjectRequest {
159    fn category() -> RouterHandlerCategory {
160        RouterHandlerCategory::SignObject
161    }
162}
163
164impl RouterHandlerCategoryInfo for RouterHandlerVerifyObjectRequest {
165    fn category() -> RouterHandlerCategory {
166        RouterHandlerCategory::VerifyObject
167    }
168}
169
170impl RouterHandlerCategoryInfo for RouterHandlerEncryptDataRequest {
171    fn category() -> RouterHandlerCategory {
172        RouterHandlerCategory::EncryptData
173    }
174}
175
176impl RouterHandlerCategoryInfo for RouterHandlerDecryptDataRequest {
177    fn category() -> RouterHandlerCategory {
178        RouterHandlerCategory::DecryptData
179    }
180}
181
182impl RouterHandlerCategoryInfo for RouterHandlerAclRequest {
183    fn category() -> RouterHandlerCategory {
184        RouterHandlerCategory::Acl
185    }
186}
187
188impl RouterHandlerCategoryInfo for RouterHandlerInterestRequest {
189    fn category() -> RouterHandlerCategory {
190        RouterHandlerCategory::Interest
191    }
192}
193
194pub fn extract_router_handler_category<P>() -> RouterHandlerCategory
195where
196    P: RouterHandlerCategoryInfo,
197{
198    P::category()
199}