aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-04-06 08:34:24 +0000
committerFuwn <[email protected]>2023-04-06 08:34:24 +0000
commit873cef6fbba01c99733c97378b5f001779604cf4 (patch)
tree4dc531b2005740ec5048350ecdae7ba15836e058 /src
parentfeat(cargo): bump 0.3.1 -> 0.3.2 (diff)
downloadwindmark-873cef6fbba01c99733c97378b5f001779604cf4.tar.xz
windmark-873cef6fbba01c99733c97378b5f001779604cf4.zip
fix(context): custom parameters format for easy lifetimes
Diffstat (limited to 'src')
-rw-r--r--src/context/hook.rs13
-rw-r--r--src/context/route.rs12
-rw-r--r--src/handler/hooks/post_route.rs4
-rw-r--r--src/handler/hooks/pre_route.rs4
-rw-r--r--src/handler/partial.rs4
-rw-r--r--src/handler/response/route.rs6
-rw-r--r--src/module/asynchronous.rs4
-rw-r--r--src/module/sync.rs4
-rw-r--r--src/response/macros.rs16
-rw-r--r--src/router.rs10
-rw-r--r--src/utilities.rs10
11 files changed, 51 insertions, 36 deletions
diff --git a/src/context/hook.rs b/src/context/hook.rs
index 6a4fe72..04ac816 100644
--- a/src/context/hook.rs
+++ b/src/context/hook.rs
@@ -16,31 +16,34 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
+use std::collections::HashMap;
+
use matchit::Params;
use openssl::x509::X509;
use url::Url;
#[allow(clippy::module_name_repetitions)]
#[derive(Clone)]
-pub struct HookContext<'a> {
+pub struct HookContext {
pub peer_address: Option<std::net::SocketAddr>,
pub url: Url,
- pub params: Option<Params<'a, 'a>>,
+ pub params: Option<HashMap<String, String>>,
pub certificate: Option<X509>,
}
-impl<'a> HookContext<'a> {
+impl HookContext {
#[must_use]
pub fn new(
peer_address: std::io::Result<std::net::SocketAddr>,
url: Url,
- params: Option<Params<'a, 'a>>,
+ params: Option<Params<'_, '_>>,
certificate: Option<X509>,
) -> Self {
Self {
peer_address: peer_address.ok(),
url,
- params,
+ params: params
+ .map(|parameters| crate::utilities::params_to_hashmap(&parameters)),
certificate,
}
}
diff --git a/src/context/route.rs b/src/context/route.rs
index 9ff0b03..a39ad0b 100644
--- a/src/context/route.rs
+++ b/src/context/route.rs
@@ -16,31 +16,33 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
+use std::collections::HashMap;
+
use matchit::Params;
use openssl::x509::X509;
use url::Url;
#[allow(clippy::module_name_repetitions)]
#[derive(Clone)]
-pub struct RouteContext<'a> {
+pub struct RouteContext {
pub peer_address: Option<std::net::SocketAddr>,
pub url: Url,
- pub params: Params<'a, 'a>,
+ pub params: HashMap<String, String>,
pub certificate: Option<X509>,
}
-impl<'a> RouteContext<'a> {
+impl RouteContext {
#[must_use]
pub fn new(
peer_address: std::io::Result<std::net::SocketAddr>,
url: Url,
- params: Params<'a, 'a>,
+ params: &Params<'_, '_>,
certificate: Option<X509>,
) -> Self {
Self {
peer_address: peer_address.ok(),
url,
- params,
+ params: crate::utilities::params_to_hashmap(params),
certificate,
}
}
diff --git a/src/handler/hooks/post_route.rs b/src/handler/hooks/post_route.rs
index eea0128..f4a919d 100644
--- a/src/handler/hooks/post_route.rs
+++ b/src/handler/hooks/post_route.rs
@@ -20,9 +20,9 @@ use crate::{context::HookContext, Response};
#[allow(clippy::module_name_repetitions)]
pub trait PostRouteHook:
- FnMut(HookContext<'_>, &mut Response) + Send + Sync
+ FnMut(HookContext, &mut Response) + Send + Sync
{
}
-impl<T> PostRouteHook for T where T: FnMut(HookContext<'_>, &mut Response) + Send + Sync
+impl<T> PostRouteHook for T where T: FnMut(HookContext, &mut Response) + Send + Sync
{}
diff --git a/src/handler/hooks/pre_route.rs b/src/handler/hooks/pre_route.rs
index 13f4482..af5ed9d 100644
--- a/src/handler/hooks/pre_route.rs
+++ b/src/handler/hooks/pre_route.rs
@@ -19,6 +19,6 @@
use crate::context::HookContext;
#[allow(clippy::module_name_repetitions)]
-pub trait PreRouteHook: FnMut(HookContext<'_>) + Send + Sync {}
+pub trait PreRouteHook: FnMut(HookContext) + Send + Sync {}
-impl<T> PreRouteHook for T where T: FnMut(HookContext<'_>) + Send + Sync {}
+impl<T> PreRouteHook for T where T: FnMut(HookContext) + Send + Sync {}
diff --git a/src/handler/partial.rs b/src/handler/partial.rs
index 3d5bfb2..1d55b93 100644
--- a/src/handler/partial.rs
+++ b/src/handler/partial.rs
@@ -18,6 +18,6 @@
use crate::context::RouteContext;
-pub trait Partial: FnMut(RouteContext<'_>) -> String + Send + Sync {}
+pub trait Partial: FnMut(RouteContext) -> String + Send + Sync {}
-impl<T> Partial for T where T: FnMut(RouteContext<'_>) -> String + Send + Sync {}
+impl<T> Partial for T where T: FnMut(RouteContext) -> String + Send + Sync {}
diff --git a/src/handler/response/route.rs b/src/handler/response/route.rs
index a65b889..c7df774 100644
--- a/src/handler/response/route.rs
+++ b/src/handler/response/route.rs
@@ -23,16 +23,16 @@ use crate::{context::RouteContext, Response};
#[allow(clippy::module_name_repetitions)]
#[async_trait]
pub trait RouteResponse: Send + Sync {
- async fn call(&mut self, context: RouteContext<'_>) -> Response;
+ async fn call(&mut self, context: RouteContext) -> Response;
}
#[async_trait]
impl<T, F> RouteResponse for T
where
- T: FnMut(RouteContext<'_>) -> F + Send + Sync,
+ T: FnMut(RouteContext) -> F + Send + Sync,
F: std::future::Future<Output = Response> + Send + 'static,
{
- async fn call(&mut self, context: RouteContext<'_>) -> Response {
+ async fn call(&mut self, context: RouteContext) -> Response {
(*self)(context).await
}
}
diff --git a/src/module/asynchronous.rs b/src/module/asynchronous.rs
index 3d0ddd5..ea69d60 100644
--- a/src/module/asynchronous.rs
+++ b/src/module/asynchronous.rs
@@ -24,8 +24,8 @@ pub trait AsyncModule: Send + Sync {
async fn on_attach(&mut self, _: &mut Router) {}
/// Called before a route is mounted.
- async fn on_pre_route(&mut self, _: HookContext<'_>) {}
+ async fn on_pre_route(&mut self, _: HookContext) {}
/// Called after a route is mounted.
- async fn on_post_route(&mut self, _: HookContext<'_>) {}
+ async fn on_post_route(&mut self, _: HookContext) {}
}
diff --git a/src/module/sync.rs b/src/module/sync.rs
index 0c2a67b..0628dfa 100644
--- a/src/module/sync.rs
+++ b/src/module/sync.rs
@@ -23,8 +23,8 @@ pub trait Module {
fn on_attach(&mut self, _: &mut Router) {}
/// Called before a route is mounted.
- fn on_pre_route(&mut self, _: HookContext<'_>) {}
+ fn on_pre_route(&mut self, _: HookContext) {}
/// Called after a route is mounted.
- fn on_post_route(&mut self, _: HookContext<'_>) {}
+ fn on_post_route(&mut self, _: HookContext) {}
}
diff --git a/src/response/macros.rs b/src/response/macros.rs
index 76bc950..ebd9799 100644
--- a/src/response/macros.rs
+++ b/src/response/macros.rs
@@ -23,10 +23,10 @@ macro_rules! sync_response {
#[macro_export]
macro_rules! $name {
($body:expr /* $(,)? */) => {
- |_: ::windmark::context::RouteContext<'_>| ::windmark::Response::$name($body)
+ |_: ::windmark::context::RouteContext| ::windmark::Response::$name($body)
};
($context:ident, $body:expr /* $(,)? */) => {
- |$context: ::windmark::context::RouteContext<'_>| ::windmark::Response::$name($body)
+ |$context: ::windmark::context::RouteContext| ::windmark::Response::$name($body)
};
}
)*
@@ -40,10 +40,10 @@ macro_rules! async_response {
#[macro_export]
macro_rules! [< $name _async >] {
($body:expr /* $(,)? */) => {
- |_: ::windmark::context::RouteContext<'_>| async { ::windmark::Response::$name($body) }
+ |_: ::windmark::context::RouteContext| async { ::windmark::Response::$name($body) }
};
($context:ident, $body:expr /* $(,)? */) => {
- |$context: ::windmark::context::RouteContext<'_>| async { ::windmark::Response::$name($body) }
+ |$context: ::windmark::context::RouteContext| async { ::windmark::Response::$name($body) }
};
}
})*
@@ -86,7 +86,7 @@ response!(binary_success_auto);
#[macro_export]
macro_rules! binary_success {
($body:expr, $mime:expr) => {
- |_: ::windmark::context::RouteContext<'_>| {
+ |_: ::windmark::context::RouteContext| {
::windmark::Response::binary_success($body, $mime)
}
};
@@ -97,7 +97,7 @@ macro_rules! binary_success {
feature to be enabled"
);
- |_: ::windmark::context::RouteContext<'_>| {
+ |_: ::windmark::context::RouteContext| {
#[cfg(feature = "auto-deduce-mime")]
return ::windmark::Response::binary_success_auto($body);
@@ -107,7 +107,7 @@ macro_rules! binary_success {
}
}};
($context:ident, $body:expr, $mime:expr) => {
- |$context: ::windmark::context::RouteContext<'_>| {
+ |$context: ::windmark::context::RouteContext| {
::windmark::Response::binary_success($body, $mime)
}
};
@@ -118,7 +118,7 @@ macro_rules! binary_success {
feature to be enabled"
);
- |$context: ::windmark::context::RouteContext<'_>| {
+ |$context: ::windmark::context::RouteContext| {
#[cfg(feature = "auto-deduce-mime")]
return ::windmark::Response::binary_success_auto($body);
diff --git a/src/router.rs b/src/router.rs
index 030ffb8..0b550c4 100644
--- a/src/router.rs
+++ b/src/router.rs
@@ -162,7 +162,7 @@ impl Router {
pub fn mount<R>(
&mut self,
route: impl Into<String> + AsRef<str>,
- mut handler: impl FnMut(RouteContext<'_>) -> R + Send + Sync + 'static,
+ mut handler: impl FnMut(RouteContext) -> R + Send + Sync + 'static,
) -> &mut Self
where
R: IntoFuture<Output = Response> + Send + 'static,
@@ -172,9 +172,9 @@ impl Router {
.routes
.insert(
route.into(),
- Arc::new(AsyncMutex::new(Box::new(
- move |context: RouteContext<'_>| handler(context).into_future(),
- ))),
+ Arc::new(AsyncMutex::new(Box::new(move |context: RouteContext| {
+ handler(context).into_future()
+ }))),
)
.unwrap();
@@ -375,7 +375,7 @@ impl Router {
let route_context = RouteContext::new(
stream.get_ref().peer_addr(),
url.clone(),
- route.params.clone(),
+ &route.params,
peer_certificate,
);
diff --git a/src/utilities.rs b/src/utilities.rs
index 68962ff..aa14292 100644
--- a/src/utilities.rs
+++ b/src/utilities.rs
@@ -31,3 +31,13 @@ pub fn queries_from_url(url: &url::Url) -> HashMap<String, String> {
queries
}
+
+#[must_use]
+pub fn params_to_hashmap(
+ params: &matchit::Params<'_, '_>,
+) -> HashMap<String, String> {
+ params
+ .iter()
+ .map(|(k, v)| (k.to_string(), v.to_string()))
+ .collect()
+}