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
#![feature(rustdoc_internals)]

//! A trait-based modular HTTP router with an extensible macro-based frontend.
//!
//! **The `drought` library is a work-in-progress! Currently the following is implemented:**
//! - [x] [`Droughter`] itself
//! - [x] Core [`Drought`]s, providing nesting and lambda facilities
//! - [ ] Static content embedding
//! - [ ] Filesystem passthrough
//! - [ ] URI rewriting
//! - [ ] Macro Frontend
//! - [ ] Macro Extensions
//!
//! All of these features *can* be implemented by library consumers, however, they
//! will be more properly integrated in future releases.
//!
//! Do note that the major version number is currently `0`. Although the core API
//! is mostly finished, there may be some flips from things to and from references,
//! just for performance's sake.

#[cfg(feature = "macro")]
pub mod macros {
    pub use drought_macros::*;


    /// A macro to hide the ugly nesting needed for asynchronous callbacks to work.
    ///
    /// It just wraps the lambda in an async future and pins it.
    #[macro_export]
    macro_rules! lambda {
        (|$($arg:pat_param),*| { $($bl:tt)* }) => {
            |$($arg),*| Box::pin(async move { $($bl)* })
        };
    }

    /// A macro to make writing lambda endpoints easier.
    ///
    /// It wraps the lambda body in a `Future` and a `DroughtResult::Handle`, with the return as the response body.
    ///
    /// # Examples
    ///
    /// ```
    /// use drought::{simple, DroughterBuilder, DroughtResult};
    ///
    /// let simple_dir = DroughterBuilder::new()
    ///     .build_with_lambda(simple!(|_, path, _, _| {
    ///         "Welcome to a simple lambda handler!".into()
    ///     }));
    /// ```
    #[macro_export]
    macro_rules! simple {
        (|$a:pat_param, $b:pat_param, $c:pat_param, $d:pat_param| { $($bl:tt)* }) => {
            |$a, $b, $c, $d| Box::pin(async move {
                $crate::droughter::DroughtResult::Handle(
                    http::Response::builder()
                        .status(200)
                        .body({ $($bl)* })
                        .unwrap()
                )
            })
        };
    }

    /// A macro to make writing lambda endpoints easier.
    ///
    /// It wraps the lambda body in a `Future` and a `DroughtResult::Handle`, with the return as the response.
    #[macro_export]
    macro_rules! handle {
        (|$a:pat_param, $b:pat_param, $c:pat_param, $d:pat_param| { $($bl:tt)* }) => {
            |$a, $b, $c, $d| Box::pin(async move {
                $crate::droughter::DroughtResult::Handle({ $($bl)* })
            })
        };
    }
}

pub mod droughter;
pub use droughter::{Drought, Droughter, DroughterBuilder, DroughterResult, DroughtResult};


mod util;