chitey_server/
resource.rs

1pub type Responder = Result<Response<Body>, ChiteyError>;
2use http::Response;
3use hyper::Body;
4use urlpattern::{UrlPattern, UrlPatternInit};
5
6use crate::{guard::Guard, web_server::ChiteyError};
7
8#[derive(Debug)]
9pub struct Resource {
10    pub(crate) rdef: UrlPattern,
11    pub(crate) url_ptn: String,
12    pub(crate) name: Option<String>,
13    pub(crate) guard: Guard,
14}
15
16impl Resource {
17    /// Constructs new resource that matches a `path` pattern.
18    #[inline]
19    pub fn new(path: &str) -> Self {
20        let ptn = <UrlPattern>::parse(UrlPatternInit {
21            pathname: Some(path.to_owned()),
22            ..Default::default()
23        })
24        .unwrap();
25
26        Resource {
27            rdef: ptn,
28            url_ptn: path.to_string(),
29            name: None,
30            guard: Guard::Get,
31        }
32    }
33    #[inline]
34    pub fn name(mut self, nm: &str) -> Self {
35        self.name = Some(nm.to_string());
36        self
37    }
38    #[inline]
39    pub fn guard(mut self, g: Guard) -> Self {
40        self.guard = g;
41        self
42    }
43    #[inline]
44    pub fn get_rdef(self) -> UrlPattern {
45        self.rdef
46    }
47}
48
49impl Clone for Resource {
50    #[inline]
51    fn clone(&self) -> Self {
52        Self { rdef: <UrlPattern>::parse(UrlPatternInit {
53            pathname: Some(self.url_ptn.clone().to_owned()),
54            ..Default::default()
55        })
56        .unwrap(), url_ptn: self.url_ptn.clone(), name: self.name.clone(), guard: self.guard.clone() }
57    }
58}