macro_rules! endpoint {
($tyname:ident, $req:tt $(< $req_lt:lifetime >)?, $resp:tt $(< $resp_lt:lifetime >)? $(,)?) => { ... };
($tyname:ident, $req:tt $(< $req_lt:lifetime >)?, $resp:tt $(< $resp_lt:lifetime >)?, $path:expr $(,)?) => { ... };
}Expand description
ยงEndpoint macro
Used to define a single Endpoint marker type that implements the Endpoint trait.
Both request and response types may be borrowed or owned.
Borrowed types may contain lifetimes, but only as types with lifetimes, not
direct references. This means we can accept Borrowed<'a> as a type but not
&'a str. If both the request and response are borrowed, they must have
different named lifetimes.
use ergot::endpoint;
#[derive(Debug, Serialize, Deserialize, Schema)]
pub struct Req1 {
a: u8,
b: u64,
}
#[derive(Debug, Serialize, Deserialize, Schema)]
pub struct Resp1 {
c: [u8; 4],
d: i32,
}
// We can use wrapper types for borrowed types
#[derive(Schema, Debug, PartialEq, Serialize, Deserialize)]
pub struct Stir<'a> {
s: &'a str,
}
// Or we can use type aliases for borrowed types
pub type Stir2<'a> = &'a str;
// both owned
endpoint!(Endpoint1, Req1, Resp1, "endpoint/1");
// request borrowed
endpoint!(Endpoint2, Stir<'a>, Resp1, "endpoint/2");
// response borrowed
endpoint!(Endpoint3, Req1, Stir<'a>, "endpoint/3");
// both request and response borrowed
endpoint!(Endpoint4, Stir<'a>, Stir<'b>, "endpoint/4");
// aliases work too
endpoint!(Endpoint5, Stir2<'a>, Stir2<'b>, "endpoint/5");If the path is omitted, the type name is used instead.