tiny_web/sys/
route.rs

1use crate::fnv1a_64;
2use tiny_web_macro::fnv1a_64;
3
4/// Route of request
5///
6///  # Values
7///
8/// * `module: String` - Start module.
9/// * `class: String` - Start class.
10/// * `action: String` - Start action (controller).
11/// * `module_id: i64` - Module id.
12/// * `class_id: i64` - Class id.
13/// * `action_id: i64` - Action id.
14/// * `param: Option<String>` - Controller param.
15/// * `lang_id: Option<i64>` - Set lang id.
16#[derive(Debug, Clone)]
17pub struct Route {
18    /// Start module
19    pub module: String,
20    /// Start class
21    pub class: String,
22    /// Start action (controller)
23    pub action: String,
24    /// Module id
25    pub module_id: i64,
26    /// Class id
27    pub class_id: i64,
28    /// Action id
29    pub action_id: i64,
30    /// Controller param
31    pub param: Option<String>,
32    /// Set lang id
33    pub lang_id: Option<i64>,
34}
35
36impl Route {
37    pub fn default_index() -> Route {
38        Route {
39            module: "index".to_owned(),
40            class: "index".to_owned(),
41            action: "index".to_owned(),
42            module_id: fnv1a_64!("index"),
43            class_id: fnv1a_64!("index"),
44            action_id: fnv1a_64!("index"),
45            param: None,
46            lang_id: None,
47        }
48    }
49    pub fn default_not_found() -> Route {
50        Route {
51            module: "index".to_owned(),
52            class: "index".to_owned(),
53            action: "not_found".to_owned(),
54            module_id: fnv1a_64!("index"),
55            class_id: fnv1a_64!("index"),
56            action_id: fnv1a_64!("not_found"),
57            param: None,
58            lang_id: None,
59        }
60    }
61    pub fn default_err() -> Route {
62        Route {
63            module: "index".to_owned(),
64            class: "index".to_owned(),
65            action: "err".to_owned(),
66            module_id: fnv1a_64!("index"),
67            class_id: fnv1a_64!("index"),
68            action_id: fnv1a_64!("err"),
69            param: None,
70            lang_id: None,
71        }
72    }
73    pub fn default_install() -> Route {
74        Route {
75            module: "index".to_owned(),
76            class: "install".to_owned(),
77            action: "index".to_owned(),
78            module_id: fnv1a_64!("index"),
79            class_id: fnv1a_64!("install"),
80            action_id: fnv1a_64!("index"),
81            param: None,
82            lang_id: None,
83        }
84    }
85
86    pub fn parse(val: &str) -> Option<Route> {
87        let vec: Vec<&str> = val.split('/').collect();
88        if vec.len() < 4 || vec.len() > 5 {
89            return None;
90        }
91        if !unsafe { *vec.get_unchecked(0) }.is_empty() {
92            return None;
93        }
94        let module = unsafe { *vec.get_unchecked(1) };
95        if module.is_empty() {
96            return None;
97        }
98        let class = unsafe { *vec.get_unchecked(2) };
99        if class.is_empty() {
100            return None;
101        }
102        let action = unsafe { *vec.get_unchecked(3) };
103        if action.is_empty() {
104            return None;
105        }
106        let param = if vec.len() == 5 {
107            let param = unsafe { *vec.get_unchecked(4) };
108            if param.is_empty() {
109                return None;
110            }
111            Some(param.to_owned())
112        } else {
113            None
114        };
115        let module = module.to_owned();
116        let module_id = fnv1a_64(module.as_bytes());
117        let class: String = class.to_owned();
118        let class_id = fnv1a_64(class.as_bytes());
119        let action = action.to_owned();
120        let action_id = fnv1a_64(action.as_bytes());
121        Some(Route {
122            module,
123            class,
124            action,
125            module_id,
126            class_id,
127            action_id,
128            param,
129            lang_id: None,
130        })
131    }
132}