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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use std::net::SocketAddr;

use crate::{
    advertise::{Advertiser, ThingType},
    hlist::*,
    servient::Servient,
};
use axum::{handler::Handler, routing::MethodRouter, Router};
use tower_http::cors::*;

use datta::{Operator, UriTemplate};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use wot_td::{
    builder::{FormBuilder, ThingBuilder},
    extend::ExtendableThing,
};

#[doc(hidden)]
/// ThingBuilder ExtendableThing used to build a Servient
///
/// It is not needed to know about it nor use it directly.
/// Instantiate a correct builder by calling [`Servient::builder`].
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ServientExtension {
    /// Listening address
    #[serde(skip)]
    addr: Option<SocketAddr>,
    /// Thing type
    #[serde(skip)]
    thing_type: ThingType,
    #[serde(skip)]
    permissive_cors: bool,
}

impl Default for ServientExtension {
    fn default() -> Self {
        ServientExtension {
            addr: None,
            thing_type: ThingType::default(),
            permissive_cors: true,
        }
    }
}

#[doc(hidden)]
/// Form Extension
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct Form {
    #[serde(skip)]
    method_router: MethodRouter,
}

impl From<MethodRouter> for Form {
    fn from(method_router: MethodRouter) -> Self {
        Self { method_router }
    }
}

impl ExtendableThing for ServientExtension {
    type InteractionAffordance = ();
    type PropertyAffordance = ();
    type ActionAffordance = ();
    type EventAffordance = ();
    type Form = Form;
    type ExpectedResponse = ();
    type DataSchema = ();
    type ObjectSchema = ();
    type ArraySchema = ();
}

/// Extension trait for the [`Servient`] configuration.
pub trait ServientSettings {
    /// Bind the http server to addr
    fn http_bind(self, addr: SocketAddr) -> Self;
    /// Set the thing type to be advertised.
    fn thing_type(self, ty: ThingType) -> Self;
    /// Disable the default CORS settings.
    fn http_disable_permissive_cors(self) -> Self;
}

impl<O: ExtendableThing> ServientSettings for ThingBuilder<O, wot_td::builder::Extended>
where
    O: Holder<ServientExtension>,
{
    fn http_bind(mut self, addr: SocketAddr) -> Self {
        self.other.field_mut().addr = Some(addr);
        self
    }

    fn thing_type(mut self, ty: ThingType) -> Self {
        self.other.field_mut().thing_type = ty;
        self
    }

    fn http_disable_permissive_cors(mut self) -> Self {
        self.other.field_mut().permissive_cors = false;
        self
    }
}

/// Trait extension to build a [`Servient`] from an extended [`ThingBuilder`]
///
/// TODO: Add an example
pub trait BuildServient {
    /// Extension type for the [`Servient`] and underlying [`Thing`].
    ///
    /// [`Thing`]: wot_td::thing::Thing
    type Other: ExtendableThing;
    /// Build the configured [`Servient`].
    fn build_servient(self) -> Result<Servient<Self::Other>, Box<dyn std::error::Error>>;
}

fn uritemplate_to_axum(uri: &str) -> String {
    use datta::TemplateComponent::*;
    let t = UriTemplate::new(uri);
    let mut path = String::new();

    for component in t.components() {
        match component {
            Literal(ref l) => path.push_str(l),
            VarList(ref op, ref varspec) => match op {
                Operator::Null => {
                    assert_eq!(
                        varspec.len(),
                        1,
                        "more than one variable in the expression is not supported."
                    );
                    path.push(':');
                    path.push_str(&varspec[0].name);
                }
                Operator::Slash => {
                    for v in varspec {
                        path.push_str("/:");
                        path.push_str(&v.name);
                    }
                }
                Operator::Question | Operator::Hash => break,
                Operator::Ampersand | Operator::Dot | Operator::Semi | Operator::Plus => {
                    panic!("Unsupported operator")
                }
            },
        }
    }

    path
}

impl<O: ExtendableThing> BuildServient for ThingBuilder<O, wot_td::builder::Extended>
where
    O: Holder<ServientExtension>,
    O::Form: Holder<Form>,
    O: Serialize,
{
    type Other = O;

    /// Build the configured Servient
    fn build_servient(self) -> Result<Servient<Self::Other>, Box<dyn std::error::Error>> {
        let thing = self.build()?;

        let mut router = Router::new();

        let thing_forms = thing.forms.iter().flat_map(|o| o.iter());
        let properties_forms = thing
            .properties
            .iter()
            .flat_map(|m| m.values().flat_map(|a| a.interaction.forms.iter()));
        let actions_forms = thing
            .actions
            .iter()
            .flat_map(|m| m.values().flat_map(|a| a.interaction.forms.iter()));
        let events_forms = thing
            .events
            .iter()
            .flat_map(|m| m.values().flat_map(|a| a.interaction.forms.iter()));

        for form in thing_forms
            .chain(properties_forms)
            .chain(actions_forms)
            .chain(events_forms)
        {
            let route = form.other.field_ref();

            let href = uritemplate_to_axum(&form.href);

            router = router.route(&href, route.method_router.clone());
        }

        // TODO: Figure out how to share the thing description and if we want to.
        let json = serde_json::to_value(&thing)?;

        router = router.route(
            "/.well-known/wot",
            axum::routing::get(move || async { axum::Json(json) }),
        );

        if thing.other.field_ref().permissive_cors {
            let cors = CorsLayer::new()
                .allow_methods(tower_http::cors::Any)
                .allow_origin(tower_http::cors::Any);
            router = router.layer(cors);
        }

        let sd = Advertiser::new()?;

        let name = {
            let name = thing
                .title
                .split_whitespace()
                .next()
                .unwrap_or("")
                .to_lowercase();
            let uuid = Uuid::new_v4();

            format!("{}{}", name, uuid.as_simple())
        };

        let http_addr = thing
            .other
            .field_ref()
            .addr
            .unwrap_or_else(|| "0.0.0.0:8080".parse().unwrap());

        let thing_type = thing.other.field_ref().thing_type;

        Ok(Servient {
            name,
            thing,
            router,
            sd,
            http_addr,
            thing_type,
        })
    }
}

/// Extension trait to build http routes while assembling [`Form`] using the
/// extended [`FormBuilder`].
///
/// [`Form`]: wot_td::thing::Form
/// [`FormBuilder`]: wot_td::builder::FormBuilder
pub trait HttpRouter {
    /// Specialisation of [wot_td::builder::FormBuilder]
    type Target;
    /// Route GET requests to the given handler.
    fn http_get<H, T>(self, handler: H) -> Self::Target
    where
        H: Handler<T, (), axum::body::Body>,
        T: 'static;
    /// Route PUT requests to the given handler.
    fn http_put<H, T>(self, handler: H) -> Self::Target
    where
        H: Handler<T, (), axum::body::Body>,
        T: 'static;
    /// Route POST requests to the given handler.
    fn http_post<H, T>(self, handler: H) -> Self::Target
    where
        H: Handler<T, (), axum::body::Body>,
        T: 'static;
    /// Route PATCH requests to the given handler.
    fn http_patch<H, T>(self, handler: H) -> Self::Target
    where
        H: Handler<T, (), axum::body::Body>,
        T: 'static;
    /// Route DELETE requests to the given handler.
    fn http_delete<H, T>(self, handler: H) -> Self::Target
    where
        H: Handler<T, (), axum::body::Body>,
        T: 'static;
}

impl<Other, Href, OtherForm> HttpRouter for FormBuilder<Other, Href, OtherForm>
where
    Other: ExtendableThing + Holder<ServientExtension>,
    OtherForm: Holder<Form>,
{
    type Target = FormBuilder<Other, Href, OtherForm>;

    /// Route GET requests to the given handler.
    fn http_get<H, T>(mut self, handler: H) -> Self::Target
    where
        H: Handler<T, (), axum::body::Body>,
        T: 'static,
    {
        let method_router = std::mem::take(&mut self.other.field_mut().method_router);
        self.other.field_mut().method_router = method_router.get(handler);
        self
    }
    /// Route PUT requests to the given handler.
    fn http_put<H, T>(mut self, handler: H) -> Self::Target
    where
        H: Handler<T, (), axum::body::Body>,
        T: 'static,
    {
        let method_router = std::mem::take(&mut self.other.field_mut().method_router);
        self.other.field_mut().method_router = method_router.put(handler);
        self
    }
    /// Route POST requests to the given handler.
    fn http_post<H, T>(mut self, handler: H) -> Self::Target
    where
        H: Handler<T, (), axum::body::Body>,
        T: 'static,
    {
        let method_router = std::mem::take(&mut self.other.field_mut().method_router);
        self.other.field_mut().method_router = method_router.post(handler);
        self
    }
    /// Route PATCH requests to the given handler.
    fn http_patch<H, T>(mut self, handler: H) -> Self::Target
    where
        H: Handler<T, (), axum::body::Body>,
        T: 'static,
    {
        let method_router = std::mem::take(&mut self.other.field_mut().method_router);
        self.other.field_mut().method_router = method_router.patch(handler);
        self
    }
    /// Route DELETE requests to the given handler.
    fn http_delete<H, T>(mut self, handler: H) -> Self::Target
    where
        H: Handler<T, (), axum::body::Body>,
        T: 'static,
    {
        let method_router = std::mem::take(&mut self.other.field_mut().method_router);
        self.other.field_mut().method_router = method_router.delete(handler);
        self
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn uritemplate(uri: &str, axum: &str) {
        let a = uritemplate_to_axum(uri);

        assert_eq!(&a, axum);
    }

    #[test]
    fn plain_uri() {
        uritemplate("/properties/on", "/properties/on");
    }

    #[test]
    fn hierarchical_uri() {
        uritemplate("/properties{/prop,sub}", "/properties/:prop/:sub");
    }

    #[test]
    fn templated_uri() {
        uritemplate("/actions/fade/{action_id}", "/actions/fade/:action_id");
    }

    #[test]
    fn query_uri() {
        uritemplate("/weather/{?lat,long}", "/weather/");
    }
}