Documentation
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! A simple router for CoAP requests, inspired by web frameworks like Axum.

pub mod extract;
pub mod handler;
pub mod macros;
pub mod method_routing;
pub mod request;
pub mod response;
pub mod route;
pub mod util;

pub use coap_lite::RequestType as Method;
use handler::{BoxedHandler, Handler, HandlerWrapper};
use method_routing::MethodRouter;
pub use method_routing::{delete, fallback, get, post, put};
pub use request::Request;
use response::IntoResponse;
use route::{Route, RouteError};

/// A simple router that matches incoming CoAP requests to registered handlers based on method and path.
#[derive(Default)]
pub struct Router<S = ()> {
    /// Registered routes, stored in the order they were added. The first matching route will be used.
    routes: Vec<(Route, MethodRouter<S>)>,
    /// Optional fallback handler that is called when no routes match.
    fallback: Option<BoxedHandler<S>>,
    /// Shared application state available to handlers through the `State` extractor.
    state: S,
}

impl<S: Clone + Default + Send + Sync + 'static> Router<S> {
    /// Creates a new empty router with default state.
    pub fn new() -> Self {
        Self::from_state(Default::default())
    }
}

impl<S: Clone + Send + Sync + 'static> Router<S> {
    /// Creates a new empty router with the given state.
    pub fn from_state(state: S) -> Self {
        Self {
            routes: Vec::new(),
            fallback: None,
            state,
        }
    }

    /// Attaches shared application state to the router.
    pub fn with_state(self, state: S) -> Self {
        Self { state, ..self }
    }

    /// Registers a new route with the given path, and method router.
    ///
    /// # Arguments
    ///
    /// - `path`: The path pattern to match (e.g. `"/sensors/{id}"`).
    ///   Path parameters can be defined using `{param}` syntax.
    /// - `method_router`: The method router containing handlers for different CoAP methods.
    ///
    /// # Note
    ///
    /// Routes are matched in the order they are added.
    /// If multiple routes could match a request, the first one will be used.
    pub fn route(mut self, path: impl ToString, method_router: MethodRouter<S>) -> Self {
        let route = Route::new(path);
        self.routes.push((route, method_router));
        self
    }

    /// Sets a fallback handler that will be called when no registered routes match an incoming request.
    pub fn fallback<H, T>(mut self, handler: H) -> Self
    where
        T: Send + Sync + 'static,
        H: Handler<T, S>,
    {
        let handler = Box::new(HandlerWrapper::new(handler));
        self.fallback = Some(handler);
        self
    }

    /// Handles an incoming request by matching it against the registered routes and calling the appropriate handler.
    ///
    /// If no routes match, the fallback handler will be called if it is set, otherwise a Not Found response will be returned.
    pub(crate) async fn handle(&self, mut req: Request) -> Request {
        // routes are explored in order of registration
        for (route, handler) in &self.routes {
            if let Ok(path) = route.match_path(&req.path()) {
                req.path = path;
                match handler.try_handle(req, self.state.clone()).await {
                    Ok(req) => return req,
                    Err(req) => {
                        return self.handle_fallback(req).await;
                    }
                }
            }
        }
        // No route matched, use fallback or return not found
        self.handle_fallback(req).await
    }

    /// Handler for when no routes match an incoming request.
    ///
    /// If a fallback handler is set, it will be called with the request.
    /// Otherwise, a Not Found response will be generated and returned.
    async fn handle_fallback(&self, mut req: Request) -> Request {
        match self.fallback {
            Some(ref fallback) => fallback.call(req, self.state.clone()).await,
            None => {
                RouteError::NotFound.into_response().fill_response(&mut req);
                req
            }
        }
    }
}

#[cfg(test)]
/// Test utilities for the router module.
pub(crate) mod test_utils {
    use crate::{
        router::{
            extract::{Json, Path, Query, State},
            method_routing::{delete, fallback, get, post, put},
            response::{IntoResponse, Response, StatusCode},
            Router,
        },
        Server,
    };
    use serde::Deserialize;
    use std::{
        collections::HashMap,
        fmt::{Display, Formatter, Result},
        sync::{Arc, Mutex},
    };

    /// Simple app state for defining key-value properties that can be shared across handlers.
    #[derive(Debug, Clone, Default)]
    pub(crate) struct AppState(Arc<Mutex<HashMap<String, String>>>);

    impl AppState {
        /// Creates a new empty app state.
        pub(crate) fn new() -> Self {
            AppState(Arc::new(Mutex::new(HashMap::new())))
        }

        /// Inserts a key-value property into the app state.
        pub(crate) fn insert(&self, key: String, value: String) {
            let mut map = self.0.lock().unwrap();
            map.insert(key, value);
        }

        pub(crate) fn clear_state(&self) {
            let mut map = self.0.lock().unwrap();
            map.clear();
        }

        pub(crate) fn remove(&self, key: &str) -> Option<String> {
            let mut map = self.0.lock().unwrap();
            map.remove(key)
        }

        /// Retrieves a value from the app state by key.
        pub(crate) fn get_property(&self, key: &str) -> Option<String> {
            let map = self.0.lock().unwrap();
            map.get(key).cloned()
        }

        pub(crate) fn get_state(&self) -> HashMap<String, String> {
            self.0.lock().unwrap().clone()
        }
    }
    /// Define the arguments you expect in the query string
    #[derive(Debug, Deserialize)]
    pub(crate) struct QueryArgs {
        /// The key to look up in the app state.
        pub key: String,
        /// Optional value
        pub value: Option<String>,
    }

    /// Defines the errors that can occur in the application
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub(crate) enum Error {
        /// Error indicating that a property was not found
        PropertyNotFound,
    }

    /// Implements the `Display` trait for the `Error` enum.
    ///
    /// This allows for custom error messages to be displayed when the error is printed.
    impl Display for Error {
        fn fmt(&self, f: &mut Formatter) -> Result {
            match self {
                Error::PropertyNotFound => write!(f, "Property not found"),
            }
        }
    }

    impl IntoResponse for Error {
        fn into_response(self) -> Response {
            let payload = self.to_string().into_bytes();
            Response::new()
                .set_status_code(StatusCode::NotFound)
                .set_payload(payload)
        }
    }

    /// Implements the `Error` trait for the `Error` enum.
    ///
    /// This allows the error to be used in contexts where a standard error is expected.
    impl std::error::Error for Error {}

    /// Example handler that demonstrates using the `State` extractor to generate a response.s
    pub(crate) async fn get_state(state: State<AppState>) -> impl IntoResponse {
        let state = state.get_state();
        Json(state).into_response()
    }

    pub(crate) async fn delete_state(state: State<AppState>) -> impl IntoResponse {
        state.clear_state();
        StatusCode::Valid.into_response()
    }

    /// Example handler that demonstrates using the `Query` and `State` extractors to generate a response.
    pub(crate) async fn get_property(
        query: Query<QueryArgs>,
        state: State<AppState>,
    ) -> impl IntoResponse {
        match state.get_property(&query.key) {
            Some(value) => (StatusCode::Valid, Json(value)).into_response(),
            None => Json(Error::PropertyNotFound).into_response(),
        }
    }

    pub(crate) async fn post_property_query(
        query: Query<QueryArgs>,
        state: State<AppState>,
    ) -> impl IntoResponse {
        if let Some(value) = query.value.as_ref() {
            if state.get_property(&query.key).is_some() {
                (StatusCode::BadRequest, "Property already exists").into_response()
            } else {
                state.insert(query.key.clone(), value.clone());
                StatusCode::Valid.into_response()
            }
        } else {
            (StatusCode::BadRequest, "Missing value in query").into_response()
        }
    }

    /// Example POST handler that demonstrates using the `Path`, `State`, and `Json` extractors to generate a response.
    pub(crate) async fn post_property(
        key: Path<String>,
        state: State<AppState>,
        Json(value): Json<String>,
    ) -> impl IntoResponse {
        if state.get_property(&key).is_some() {
            (StatusCode::BadRequest, "Property already exists").into_response()
        } else {
            state.insert(key.clone(), value);
            StatusCode::Valid.into_response()
        }
    }

    /// Example PUT handler that demonstrates using the `Path`, `State`, and `Json` extractors to generate a response.
    pub(crate) async fn put_property(
        key: Path<String>,
        state: State<AppState>,
        Json(value): Json<String>,
    ) -> impl IntoResponse {
        if state.get_property(&key).is_some() {
            state.insert(key.clone(), value);
            StatusCode::Valid.into_response()
        } else {
            (StatusCode::NotFound, "Property not found").into_response()
        }
    }

    /// Example DELETE handler that demonstrates using the `Path` and `State` extractors to generate a response.
    pub(crate) async fn delete_property(
        Path(key): Path<String>,
        state: State<AppState>,
    ) -> impl IntoResponse {
        match state.remove(&key) {
            Some(_) => StatusCode::Valid.into_response(),
            None => Json(Error::PropertyNotFound).into_response(),
        }
    }

    pub(crate) async fn fallback_handler() -> impl IntoResponse {
        (StatusCode::BadRequest, "Fallback response").into_response()
    }

    pub(crate) async fn route_fallback_handler() -> impl IntoResponse {
        (StatusCode::BadRequest, "Route fallback response").into_response()
    }

    pub(crate) fn build_router() -> Router<AppState> {
        Router::new()
            .with_state(AppState::new())
            .route(
                "/state",
                delete(delete_state)
                    .get(get_state)
                    .fallback(route_fallback_handler),
            )
            .route(
                "/state/property/{key}",
                put(put_property)
                    .post(post_property)
                    .delete(delete_property),
            )
            .route("/property", get(get_property).post(post_property_query))
            .route(
                "/property/{key}",
                post(post_property)
                    .put(put_property)
                    .delete(delete_property),
            )
            .fallback(fallback_handler)
            .route("/fallback", fallback(route_fallback_handler))
    }

    pub(crate) async fn run_router<S: ToString>(addr: S) {
        let router = build_router();
        let server = Server::new_udp(addr.to_string()).unwrap();
        server.serve(router).await.unwrap();
    }

    pub(crate) fn encode_payload(payload: &str) -> Vec<u8> {
        if serde_json::from_str::<serde_json::Value>(payload).is_ok() {
            payload.as_bytes().to_vec()
        } else {
            serde_json::to_vec(payload).expect("string payload should always serialize to JSON")
        }
    }
}

#[cfg(test)]
mod tests {
    use super::response::StatusCode;
    use super::test_utils::*;
    use crate::client::UdpCoAPClient as Client;
    use coap_lite::RequestType as Method;
    use tokio::time::{sleep, Duration};

    #[tokio::test]
    async fn test_router() {
        // Start the server in the background
        let addr = "127.0.0.1:5684";
        let server_handle = tokio::spawn(async move {
            run_router(addr).await;
        });
        sleep(Duration::from_millis(100)).await;

        // Wrong path (should trigger fallback)
        let response = Client::get(&format!("coap://{}/wrong_path", addr)).await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(status, StatusCode::BadRequest);
        assert_eq!(payload, "Fallback response");

        // Wrong method (should trigger route fallback)
        let response = Client::post(&format!("coap://{}/state", addr), b"{}".to_vec()).await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(status, StatusCode::BadRequest);
        assert_eq!(payload, "Route fallback response");

        // Another wrong method (should trigger route fallback)
        let response = Client::get(&format!("coap://{}/fallback", addr)).await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(status, StatusCode::BadRequest);
        assert_eq!(payload, "Route fallback response");

        // Unknown method on existing path with no route fallback (should trigger global fallback)
        let response =
            Client::request(&format!("coap://{}/property", addr), Method::Fetch, None).await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(status, StatusCode::BadRequest);
        assert_eq!(payload, "Fallback response");

        // Get state (should be empty)
        let response = Client::get(&format!("coap://{}/state", addr)).await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(status, StatusCode::Content);
        assert_eq!(payload, "{}");

        // Get a property with wrong query format
        let response = Client::get(&format!("coap://{}/property?wrong=test_key", addr)).await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(status, StatusCode::BadRequest);
        assert_eq!(payload, "Invalid query string");

        // Get a non-existent property
        let response = Client::get(&format!("coap://{}/property?key=test_key", addr)).await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(status, StatusCode::NotFound);
        assert_eq!(payload, "Property not found");

        // Put a non-existent property (should return not found)
        let response = Client::put(
            &format!("coap://{}/property/test_key", addr),
            encode_payload("test_value"),
        )
        .await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(status, StatusCode::NotFound);
        assert_eq!(payload, "Property not found");

        // Post a new property
        let response = Client::post(
            &format!("coap://{}/property/test_key", addr),
            encode_payload("test_value"),
        )
        .await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        assert_eq!(status, StatusCode::Valid);
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(payload, "");

        // Get the property we just set
        let response = Client::get(&format!("coap://{}/property?key=test_key", addr)).await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(status, StatusCode::Valid);
        assert_eq!(payload, r#""test_value""#);

        // Post the same property again (should return bad request)
        let response = Client::post(
            &format!("coap://{}/property/test_key", addr),
            encode_payload("test_value"),
        )
        .await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(status, StatusCode::BadRequest);
        assert_eq!(payload, "Property already exists");

        // Put an existing property (should update it)
        let response = Client::put(
            &format!("coap://{}/property/test_key", addr),
            encode_payload("updated_value"),
        )
        .await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        assert_eq!(status, StatusCode::Valid);
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(payload, "");

        // Delete the property
        let response = Client::delete(&format!("coap://{}/property/test_key", addr)).await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        assert_eq!(status, StatusCode::Valid);
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(payload, "");

        // Try to add property with post_property_query without value (should return bad request)
        let response = Client::post(
            &format!("coap://{}/property?key=query_key", addr),
            Vec::new(),
        )
        .await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(status, StatusCode::BadRequest);
        assert_eq!(payload, "Missing value in query");

        // Add a new property with post_property_query
        let response = Client::post(
            &format!("coap://{}/property?key=query_key&value=query_value", addr),
            Vec::new(),
        )
        .await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        assert_eq!(status, StatusCode::Valid);
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(payload, "");

        // Clear state
        let response = Client::delete(&format!("coap://{}/state", addr)).await;
        assert!(response.is_ok());
        let response = response.unwrap();
        let status = *response.get_status();
        assert_eq!(status, StatusCode::Valid);
        let payload = String::from_utf8(response.message.payload).unwrap();
        assert_eq!(payload, "");

        server_handle.abort();
        let _ = server_handle.await;
    }
}