teo_runtime/handler/
builder.rs

1use std::sync::{Arc, Mutex};
2use std::sync::atomic::AtomicBool;
3use educe::Educe;
4use teo_parser::ast::handler::HandlerInputFormat;
5use teo_parser::r#type::Type;
6use crate::app::data::AppData;
7use crate::handler::{Handler, handler};
8use hyper::Method;
9use crate::middleware::next::Next;
10
11#[derive(Debug, Clone)]
12pub struct Builder {
13    inner: Arc<Inner>
14}
15
16#[derive(Educe)]
17#[educe(Debug)]
18struct Inner {
19    path: Vec<String>,
20    namespace_path: Vec<String>,
21    input_type: Type,
22    output_type: Type,
23    nonapi: bool,
24    format: Arc<Mutex<HandlerInputFormat>>,
25    method: Arc<Mutex<Method>>,
26    url: Arc<Mutex<Option<String>>>,
27    interface: Arc<Mutex<Option<String>>>,
28    ignore_prefix: AtomicBool,
29    #[educe(Debug(ignore))]
30    call: Next,
31    app_data: AppData,
32}
33
34impl Builder {
35
36    pub fn new(path: Vec<String>, namespace_path: Vec<String>, input_type: Type, output_type: Type, nonapi: bool, format: HandlerInputFormat, call: Next, app_data: AppData) -> Self {
37        Self {
38            inner: Arc::new(Inner {
39                path,
40                namespace_path,
41                input_type,
42                output_type,
43                nonapi,
44                format: Arc::new(Mutex::new(format)),
45                method: Arc::new(Mutex::new(Method::POST)),
46                url: Arc::new(Mutex::new(None)),
47                interface: Arc::new(Mutex::new(None)),
48                ignore_prefix: AtomicBool::new(false),
49                call,
50                app_data
51            })
52        }
53    }
54
55    pub fn path(&self) -> &Vec<String> {
56        &self.inner.path
57    }
58
59    pub fn namespace_path(&self) -> &Vec<String> {
60        &self.inner.namespace_path
61    }
62
63    pub fn input_type(&self) -> &Type {
64        &self.inner.input_type
65    }
66
67    pub fn output_type(&self) -> &Type {
68        &self.inner.output_type
69    }
70
71    pub fn nonapi(&self) -> bool {
72        self.inner.nonapi
73    }
74
75    pub fn format(&self) -> HandlerInputFormat {
76        *self.inner.format.lock().unwrap()
77    }
78
79    pub fn set_format(&self, format: HandlerInputFormat) {
80        *self.inner.format.lock().unwrap() = format
81    }
82
83    pub fn method(&self) -> Method {
84        self.inner.method.lock().unwrap().clone()
85    }
86
87    pub fn set_method(&self, method: Method) {
88        *self.inner.method.lock().unwrap() = method;
89    }
90
91    pub fn url(&self) -> Option<String> {
92        self.inner.url.lock().unwrap().clone()
93    }
94
95    pub fn set_url(&self, url: Option<String>) {
96        *self.inner.url.lock().unwrap() = url;
97    }
98
99    pub fn interface(&self) -> Option<String> {
100        self.inner.interface.lock().unwrap().clone()
101    }
102
103    pub fn set_interface(&self, interface: Option<String>) {
104        *self.inner.interface.lock().unwrap() = interface;
105    }
106
107    pub fn ignore_prefix(&self) -> bool {
108        self.inner.ignore_prefix.load(std::sync::atomic::Ordering::Relaxed)
109    }
110
111    pub fn set_ignore_prefix(&self, ignore_prefix: bool) {
112        self.inner.ignore_prefix.store(ignore_prefix, std::sync::atomic::Ordering::Relaxed);
113    }
114
115    pub fn call(&self) -> Next {
116        self.inner.call.clone()
117    }
118
119    pub fn app_data(&self) -> &AppData {
120        &self.inner.app_data
121    }
122
123    pub(crate) fn build(self) -> Handler {
124        Handler {
125            inner: Arc::new(handler::Inner {
126                path: self.inner.path.clone(),
127                namespace_path: self.inner.namespace_path.clone(),
128                input_type: self.inner.input_type.clone(),
129                output_type: self.inner.output_type.clone(),
130                nonapi: self.inner.nonapi,
131                format: self.inner.format.lock().unwrap().clone(),
132                method: self.inner.method.lock().unwrap().clone(),
133                url: self.inner.url.lock().unwrap().clone(),
134                interface: self.inner.interface.lock().unwrap().clone(),
135                ignore_prefix: self.inner.ignore_prefix.load(std::sync::atomic::Ordering::Relaxed),
136                call: self.inner.call.clone(),
137            })
138        }
139    }
140}