teo_runtime/handler/
handler.rs

1use std::sync::Arc;
2use educe::Educe;
3use serde::{Serialize, Serializer};
4use teo_parser::ast::handler::HandlerInputFormat;
5use teo_parser::r#type::Type;
6use hyper::Method;
7use crate::middleware::next::Next;
8use crate::traits::named::Named;
9
10#[derive(Educe)]
11#[educe(Debug)]
12#[derive(Clone)]
13pub struct Handler {
14    pub(super) inner: Arc<Inner>
15}
16
17pub fn method_serialize<S>(x: &Method, s: S) -> Result<S::Ok, S::Error>
18where
19    S: Serializer,
20{
21    s.serialize_str(x.as_str())
22}
23
24#[derive(Educe, Serialize)]
25#[educe(Debug)]
26pub(super) struct Inner {
27    pub(super) path: Vec<String>,
28    pub(super) namespace_path: Vec<String>,
29    pub(super) input_type: Type,
30    pub(super) output_type: Type,
31    pub(super) nonapi: bool,
32    pub(super) format: HandlerInputFormat,
33    #[serde(serialize_with = "method_serialize")]
34    pub(super) method: Method,
35    pub(super) url: Option<String>,
36    pub(super) interface: Option<String>,
37    pub(super) ignore_prefix: bool,
38    #[serde(skip)] #[educe(Debug(ignore))]
39    pub(super) call: Next,
40}
41
42impl Handler {
43
44    pub fn path(&self) -> &Vec<String> {
45        &self.inner.path
46    }
47
48    pub fn namespace_path(&self) -> &Vec<String> {
49        &self.inner.namespace_path
50    }
51
52    pub fn parent_path(&self) -> &Vec<String> {
53        &self.inner.namespace_path
54    }
55
56    pub fn input_type(&self) -> &Type {
57        &self.inner.input_type
58    }
59
60    pub fn output_type(&self) -> &Type {
61        &self.inner.output_type
62    }
63
64    pub fn nonapi(&self) -> bool {
65        self.inner.nonapi
66    }
67
68    pub fn format(&self) -> HandlerInputFormat {
69        self.inner.format
70    }
71
72    pub fn method(&self) -> &Method {
73        &self.inner.method
74    }
75
76    pub fn url(&self) -> Option<&String> {
77        self.inner.url.as_ref()
78    }
79
80    pub fn interface(&self) -> &Option<String> {
81        &self.inner.interface
82    }
83
84    pub fn ignore_prefix(&self) -> bool {
85        self.inner.ignore_prefix
86    }
87
88    pub fn call(&self) -> Next {
89        self.inner.call.clone()
90    }
91
92    pub fn has_custom_url_args(&self) -> bool {
93        if self.inner.url.is_some() {
94            self.inner.url.as_ref().unwrap().contains("*") || self.inner.url.as_ref().unwrap().contains(":")
95        } else {
96            false
97        }
98    }
99
100    pub fn has_body_input(&self) -> bool {
101        !(self.inner.method == Method::GET || self.inner.method == Method::DELETE)
102    }
103
104    pub fn custom_url_args_path(&self) -> Option<Vec<String>> {
105        if let Some(interface) = &self.inner.interface {
106            let mut result = self.inner.path.clone();
107            result.push(interface.clone());
108            Some(result)
109        } else {
110            None
111        }
112    }
113}
114
115impl Named for Handler {
116
117    fn name(&self) -> &str {
118        self.inner.path.last().map(|s| s.as_str()).unwrap()
119    }
120}
121
122impl Serialize for Handler {
123    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
124        self.inner.serialize(serializer)
125    }
126}