teo_runtime/handler/
builder.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::sync::{Arc, Mutex};
use std::sync::atomic::AtomicBool;
use educe::Educe;
use teo_parser::ast::handler::HandlerInputFormat;
use teo_parser::r#type::Type;
use crate::app::data::AppData;
use crate::handler::{Handler, handler};
use hyper::Method;
use crate::middleware::next::Next;

#[derive(Debug, Clone)]
pub struct Builder {
    inner: Arc<Inner>
}

#[derive(Educe)]
#[educe(Debug)]
struct Inner {
    path: Vec<String>,
    namespace_path: Vec<String>,
    input_type: Type,
    output_type: Type,
    nonapi: bool,
    format: Arc<Mutex<HandlerInputFormat>>,
    method: Arc<Mutex<Method>>,
    url: Arc<Mutex<Option<String>>>,
    interface: Arc<Mutex<Option<String>>>,
    ignore_prefix: AtomicBool,
    #[educe(Debug(ignore))]
    call: &'static dyn Next,
    app_data: AppData,
}

impl Builder {

    pub fn new(path: Vec<String>, namespace_path: Vec<String>, input_type: Type, output_type: Type, nonapi: bool, format: HandlerInputFormat, call: &'static dyn Next, app_data: AppData) -> Self {
        Self {
            inner: Arc::new(Inner {
                path,
                namespace_path,
                input_type,
                output_type,
                nonapi,
                format: Arc::new(Mutex::new(format)),
                method: Arc::new(Mutex::new(Method::POST)),
                url: Arc::new(Mutex::new(None)),
                interface: Arc::new(Mutex::new(None)),
                ignore_prefix: AtomicBool::new(false),
                call,
                app_data
            })
        }
    }

    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.lock().unwrap()
    }

    pub fn set_format(&self, format: HandlerInputFormat) {
        *self.inner.format.lock().unwrap() = format
    }

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

    pub fn set_method(&self, method: Method) {
        *self.inner.method.lock().unwrap() = method;
    }

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

    pub fn set_url(&self, url: Option<String>) {
        *self.inner.url.lock().unwrap() = url;
    }

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

    pub fn set_interface(&self, interface: Option<String>) {
        *self.inner.interface.lock().unwrap() = interface;
    }

    pub fn ignore_prefix(&self) -> bool {
        self.inner.ignore_prefix.load(std::sync::atomic::Ordering::Relaxed)
    }

    pub fn set_ignore_prefix(&self, ignore_prefix: bool) {
        self.inner.ignore_prefix.store(ignore_prefix, std::sync::atomic::Ordering::Relaxed);
    }

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

    pub fn app_data(&self) -> &AppData {
        &self.inner.app_data
    }

    pub(crate) fn build(self) -> Handler {
        Handler {
            inner: Arc::new(handler::Inner {
                path: self.inner.path.clone(),
                namespace_path: self.inner.namespace_path.clone(),
                input_type: self.inner.input_type.clone(),
                output_type: self.inner.output_type.clone(),
                nonapi: self.inner.nonapi,
                format: self.inner.format.lock().unwrap().clone(),
                method: self.inner.method.lock().unwrap().clone(),
                url: self.inner.url.lock().unwrap().clone(),
                interface: self.inner.interface.lock().unwrap().clone(),
                ignore_prefix: self.inner.ignore_prefix.load(std::sync::atomic::Ordering::Relaxed),
                call: self.inner.call,
            })
        }
    }
}