web: add typed paths
parent
8fa4c910c1
commit
adb057583f
@ -0,0 +1,5 @@
|
||||
format_code_in_doc_comments = true
|
||||
format_strings = true
|
||||
imports_granularity = "Crate"
|
||||
group_imports = "StdExternalCrate"
|
||||
wrap_comments = true
|
@ -0,0 +1,559 @@
|
||||
/*
|
||||
* This file is part of mailpot
|
||||
*
|
||||
* Copyright 2020 - Manos Pitsidianakis
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
|
||||
|
||||
use super::*;
|
||||
|
||||
// from https://github.com/servo/rust-url/blob/master/url/src/parser.rs
|
||||
const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
|
||||
const PATH: &AsciiSet = &FRAGMENT.add(b'#').add(b'?').add(b'{').add(b'}');
|
||||
pub const PATH_SEGMENT: &AsciiSet = &PATH.add(b'/').add(b'%');
|
||||
|
||||
pub trait IntoCrumb: TypedPath {
|
||||
fn to_crumb(&self) -> Cow<'static, str> {
|
||||
Cow::from(self.to_uri().to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl<TP: TypedPath> IntoCrumb for TP {}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum ListPathIdentifier {
|
||||
Pk(#[serde(deserialize_with = "parse_int")] i64),
|
||||
Id(String),
|
||||
}
|
||||
|
||||
fn parse_int<'de, T, D>(de: D) -> Result<T, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
T: std::str::FromStr,
|
||||
<T as std::str::FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
use serde::Deserialize;
|
||||
String::deserialize(de)?
|
||||
.parse()
|
||||
.map_err(serde::de::Error::custom)
|
||||
}
|
||||
|
||||
impl From<i64> for ListPathIdentifier {
|
||||
fn from(val: i64) -> Self {
|
||||
Self::Pk(val)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for ListPathIdentifier {
|
||||
fn from(val: String) -> Self {
|
||||
Self::Id(val)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ListPathIdentifier {
|
||||
#[allow(clippy::unnecessary_to_owned)]
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let id: Cow<'_, str> = match self {
|
||||
Self::Pk(id) => id.to_string().into(),
|
||||
Self::Id(id) => id.into(),
|
||||
};
|
||||
write!(f, "{}", utf8_percent_encode(&id, PATH_SEGMENT,))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize, TypedPath)]
|
||||
#[typed_path("/list/:id/")]
|
||||
pub struct ListPath(pub ListPathIdentifier);
|
||||
|
||||
impl From<&DbVal<mailpot::models::MailingList>> for ListPath {
|
||||
fn from(val: &DbVal<mailpot::models::MailingList>) -> Self {
|
||||
Self(ListPathIdentifier::Id(val.id.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize, TypedPath)]
|
||||
#[typed_path("/list/:id/posts/:msgid/")]
|
||||
pub struct ListPostPath(pub ListPathIdentifier, pub String);
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize, TypedPath)]
|
||||
#[typed_path("/list/:id/edit/")]
|
||||
pub struct ListEditPath(pub ListPathIdentifier);
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize, TypedPath)]
|
||||
#[typed_path("/settings/list/:id/")]
|
||||
pub struct ListSettingsPath(pub ListPathIdentifier);
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize, TypedPath)]
|
||||
#[typed_path("/login/")]
|
||||
pub struct LoginPath;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize, TypedPath)]
|
||||
#[typed_path("/logout/")]
|
||||
pub struct LogoutPath;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize, TypedPath)]
|
||||
#[typed_path("/settings/")]
|
||||
pub struct SettingsPath;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize, TypedPath)]
|
||||
#[typed_path("/help/")]
|
||||
pub struct HelpPath;
|
||||
|
||||
macro_rules! unit_impl {
|
||||
($ident:ident, $ty:expr) => {
|
||||
pub fn $ident() -> Value {
|
||||
$ty.to_crumb().into()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
unit_impl!(login_path, LoginPath);
|
||||
unit_impl!(logout_path, LogoutPath);
|
||||
unit_impl!(settings_path, SettingsPath);
|
||||
unit_impl!(help_path, HelpPath);
|
||||
|
||||
macro_rules! list_id_impl {
|
||||
($ident:ident, $ty:tt) => {
|
||||
pub fn $ident(id: Value) -> std::result::Result<Value, Error> {
|
||||
if let Some(id) = id.as_str() {
|
||||
return Ok($ty(ListPathIdentifier::Id(id.to_string()))
|
||||
.to_crumb()
|
||||
.into());
|
||||
}
|
||||
let pk = id.try_into()?;
|
||||
Ok($ty(ListPathIdentifier::Pk(pk)).to_crumb().into())
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
list_id_impl!(list_path, ListPath);
|
||||
list_id_impl!(list_settings_path, ListSettingsPath);
|
||||
list_id_impl!(list_edit_path, ListEditPath);
|
||||
|
||||
pub fn list_post_path(id: Value, msg_id: Value) -> std::result::Result<Value, Error> {
|
||||
let Some(msg_id) = msg_id.as_str().map(|s| if s.starts_with('<') && s.ends_with('>') { s.to_string() } else {
|
||||
format!("<{s}>")
|
||||
}) else {
|
||||
return Err(Error::new(
|
||||
minijinja::ErrorKind::UnknownMethod,
|
||||
"Second argument of list_post_path must be a string."
|
||||
));
|
||||
};
|
||||
|
||||
if let Some(id) = id.as_str() {
|
||||
return Ok(ListPostPath(ListPathIdentifier::Id(id.to_string()), msg_id)
|
||||
.to_crumb()
|
||||
.into());
|
||||
}
|
||||
let pk = id.try_into()?;
|
||||
Ok(ListPostPath(ListPathIdentifier::Pk(pk), msg_id)
|
||||
.to_crumb()
|
||||
.into())
|
||||
}
|
||||
|
||||
pub mod tsr {
|
||||
use std::{borrow::Cow, convert::Infallible};
|
||||
|
||||
use axum::{
|
||||
http::Request,
|
||||
response::{IntoResponse, Redirect, Response},
|
||||
routing::{any, MethodRouter},
|
||||
Router,
|
||||
};
|
||||
use axum_extra::routing::{RouterExt as ExtraRouterExt, SecondElementIs, TypedPath};
|
||||
use http::{uri::PathAndQuery, StatusCode, Uri};
|
||||
use tower_service::Service;
|
||||
|
||||
/// Extension trait that adds additional methods to [`Router`].
|
||||
pub trait RouterExt<S, B>: ExtraRouterExt<S, B> {
|
||||
/// Add a typed `GET` route to the router.
|
||||
///
|
||||
/// The path will be inferred from the first argument to the handler
|
||||
/// function which must implement [`TypedPath`].
|
||||
///
|
||||
/// See [`TypedPath`] for more details and examples.
|
||||
fn typed_get<H, T, P>(self, handler: H) -> Self
|
||||
where
|
||||
H: axum::handler::Handler<T, S, B>,
|
||||
T: SecondElementIs<P> + 'static,
|
||||
P: TypedPath;
|
||||
|
||||
/// Add a typed `DELETE` route to the router.
|
||||
///
|
||||
/// The path will be inferred from the first argument to the handler
|
||||
/// function which must implement [`TypedPath`].
|
||||
///
|
||||
/// See [`TypedPath`] for more details and examples.
|
||||
fn typed_delete<H, T, P>(self, handler: H) -> Self
|
||||
where
|
||||
H: axum::handler::Handler<T, S, B>,
|
||||
T: SecondElementIs<P> + 'static,
|
||||
P: TypedPath;
|
||||
|
||||
/// Add a typed `HEAD` route to the router.
|
||||
///
|
||||
/// The path will be inferred from the first argument to the handler
|
||||
/// function which must implement [`TypedPath`].
|
||||
///
|
||||
/// See [`TypedPath`] for more details and examples.
|
||||
fn typed_head<H, T, P>(self, handler: H) -> Self
|
||||
where
|
||||
H: axum::handler::Handler<T, S, B>,
|
||||
T: SecondElementIs<P> + 'static,
|
||||
P: TypedPath;
|
||||
|
||||
/// Add a typed `OPTIONS` route to the router.
|
||||
///
|
||||
/// The path will be inferred from the first argument to the handler
|
||||
/// function which must implement [`TypedPath`].
|
||||
///
|
||||
/// See [`TypedPath`] for more details and examples.
|
||||
fn typed_options<H, T, P>(self, handler: H) -> Self
|
||||
where
|
||||
H: axum::handler::Handler<T, S, B>,
|
||||
T: SecondElementIs<P> + 'static,
|
||||
P: TypedPath;
|
||||
|
||||
/// Add a typed `PATCH` route to the router.
|
||||
///
|
||||
/// The path will be inferred from the first argument to the handler
|
||||
/// function which must implement [`TypedPath`].
|
||||
///
|
||||
/// See [`TypedPath`] for more details and examples.
|
||||
fn typed_patch<H, T, P>(self, handler: H) -> Self
|
||||
where
|
||||
H: axum::handler::Handler<T, S, B>,
|
||||
T: SecondElementIs<P> + 'static,
|
||||
P: TypedPath;
|
||||
|
||||
/// Add a typed `POST` route to the router.
|
||||
///
|
||||
/// The path will be inferred from the first argument to the handler
|
||||
/// function which must implement [`TypedPath`].
|
||||
///
|
||||
/// See [`TypedPath`] for more details and examples.
|
||||
fn typed_post<H, T, P>(self, handler: H) -> Self
|
||||
where
|
||||
H: axum::handler::Handler<T, S, B>,
|
||||
T: SecondElementIs<P> + 'static,
|
||||
P: TypedPath;
|
||||
|
||||
/// Add a typed `PUT` route to the router.
|
||||
///
|
||||
/// The path will be inferred from the first argument to the handler
|
||||
/// function which must implement [`TypedPath`].
|
||||
///
|
||||
/// See [`TypedPath`] for more details and examples.
|
||||
fn typed_put<H, T, P>(self, handler: H) -> Self
|
||||
where
|
||||
H: axum::handler::Handler<T, S, B>,
|
||||
T: SecondElementIs<P> + 'static,
|
||||
P: TypedPath;
|
||||
|
||||
/// Add a typed `TRACE` route to the router.
|
||||
///
|
||||
/// The path will be inferred from the first argument to the handler
|
||||
/// function which must implement [`TypedPath`].
|
||||
///
|
||||
/// See [`TypedPath`] for more details and examples.
|
||||
fn typed_trace<H, T, P>(self, handler: H) -> Self
|
||||
where
|
||||
H: axum::handler::Handler<T, S, B>,
|
||||
T: SecondElementIs<P> + 'static,
|
||||
P: TypedPath;
|
||||
|
||||
/// Add another route to the router with an additional "trailing slash
|
||||
/// redirect" route.
|
||||
///
|
||||
/// If you add a route _without_ a trailing slash, such as `/foo`, this
|
||||
/// method will also add a route for `/foo/` that redirects to
|
||||
/// `/foo`.
|
||||
///
|
||||
/// If you add a route _with_ a trailing slash, such as `/bar/`, this
|
||||
/// method will also add a route for `/bar` that redirects to
|
||||
/// `/bar/`.
|
||||
///
|
||||
/// This is similar to what axum 0.5.x did by default, except this
|
||||
/// explicitly adds another route, so trying to add a `/foo/`
|
||||
/// route after calling `.route_with_tsr("/foo", /* ... */)`
|
||||