teo_runtime/handler/
handler.rs

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::sync::Arc;
use educe::Educe;
use serde::{Serialize, Serializer};
use teo_parser::ast::handler::HandlerInputFormat;
use teo_parser::r#type::Type;
use hyper::Method;
use crate::middleware::next::Next;
use crate::traits::named::Named;

#[derive(Educe)]
#[educe(Debug)]
#[derive(Clone)]
pub struct Handler {
    pub(super) inner: Arc<Inner>
}

pub fn method_serialize<S>(x: &Method, s: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    s.serialize_str(x.as_str())
}

#[derive(Educe, Serialize)]
#[educe(Debug)]
pub(super) struct Inner {
    pub(super) path: Vec<String>,
    pub(super) namespace_path: Vec<String>,
    pub(super) input_type: Type,
    pub(super) output_type: Type,
    pub(super) nonapi: bool,
    pub(super) format: HandlerInputFormat,
    #[serde(serialize_with = "method_serialize")]
    pub(super) method: Method,
    pub(super) url: Option<String>,
    pub(super) interface: Option<String>,
    pub(super) ignore_prefix: bool,
    #[serde(skip)] #[educe(Debug(ignore))]
    pub(super) call: &'static dyn Next,
}

impl Handler {

    pub fn path(&self) -> &Vec<String> {
        &self.inner.path
    }

    pub fn namespace_path(&self) -> &Vec<String> {
        &self.inner.namespace_path
    }

    pub fn input_type(&self) -> &Type {
        &self.inner.input_type
    }

    pub fn output_type(&self) -> &Type {
        &self.inner.output_type
    }

    pub fn nonapi(&self) -> bool {
        self.inner.nonapi
    }

    pub fn format(&self) -> HandlerInputFormat {
        self.inner.format
    }

    pub fn method(&self) -> &Method {
        &self.inner.method
    }

    pub fn url(&self) -> Option<&String> {
        self.inner.url.as_ref()
    }

    pub fn interface(&self) -> &Option<String> {
        &self.inner.interface
    }

    pub fn ignore_prefix(&self) -> bool {
        self.inner.ignore_prefix
    }

    pub fn call(&self) -> &'static dyn Next {
        self.inner.call
    }

    pub fn has_custom_url_args(&self) -> bool {
        if self.inner.url.is_some() {
            self.inner.url.as_ref().unwrap().contains("*") || self.inner.url.as_ref().unwrap().contains(":")
        } else {
            false
        }
    }

    pub fn has_body_input(&self) -> bool {
        !(self.inner.method == Method::GET || self.inner.method == Method::DELETE)
    }

    pub fn custom_url_args_path(&self) -> Option<Vec<String>> {
        if let Some(interface) = &self.inner.interface {
            let mut result = self.inner.path.clone();
            result.push(interface.clone());
            Some(result)
        } else {
            None
        }
    }
}

impl Named for Handler {

    fn name(&self) -> &str {
        self.inner.path.last().map(|s| s.as_str()).unwrap()
    }
}

impl Serialize for Handler {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> where S: serde::Serializer {
        self.inner.serialize(serializer)
    }
}