Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
e0baff8625 | |||
173bbc2ca5 | |||
cdc8f5e463 | |||
aba28d1612 |
@ -42,8 +42,13 @@ impl AppBuilder {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn routes(mut self, routes: &[Router]) -> Self {
|
||||
self.router = routes.iter().cloned().fold(self.router, Router::merge);
|
||||
pub fn route(mut self, route: Router) -> Self {
|
||||
self.router = self.router.merge(route);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn routes(mut self, routes: impl IntoIterator<Item = Router>) -> Self {
|
||||
self.router = routes.into_iter().fold(self.router, Router::merge);
|
||||
self
|
||||
}
|
||||
|
||||
@ -147,7 +152,7 @@ mod tests {
|
||||
let handler = tokio::spawn(async {
|
||||
AppBuilder::new()
|
||||
.socket((Ipv4Addr::LOCALHOST, 8080))
|
||||
.routes(&[Router::new()])
|
||||
.routes([Router::new()])
|
||||
.fallback(|| async { "Fallback" })
|
||||
.cors(CorsLayer::new())
|
||||
.normalize_path(true)
|
||||
|
@ -21,13 +21,21 @@
|
||||
#[cfg(feature = "axum")]
|
||||
macro_rules! router {
|
||||
($body:expr) => {
|
||||
pub(crate) fn router() -> axum::Router<()> {
|
||||
pub(crate) fn router() -> axum::Router {
|
||||
$body
|
||||
}
|
||||
};
|
||||
($body:expr; $state:ty) => {
|
||||
pub(crate) fn router() -> axum::Router<$state> {
|
||||
$body
|
||||
}
|
||||
};
|
||||
($route:expr, $router:expr) => {
|
||||
router!(axum::Router::new().nest($route, $router));
|
||||
};
|
||||
($route:expr, $router:expr, $state:ty) => {
|
||||
router!(axum::Router::new().nest($route, $router); $state);
|
||||
};
|
||||
($($method:ident $route:expr => $func:expr),* $(,)?) => {
|
||||
router!($crate::routes!($($method $route => $func),*));
|
||||
};
|
||||
@ -66,6 +74,7 @@ macro_rules! join_routes {
|
||||
|
||||
#[cfg(all(test, feature = "axum"))]
|
||||
mod tests {
|
||||
use axum::extract::State;
|
||||
use axum::Router;
|
||||
|
||||
async fn index() {}
|
||||
@ -94,6 +103,18 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nested_router_with_state() {
|
||||
router!(
|
||||
"/simplify",
|
||||
routes!(
|
||||
get "/:exp" => || async {},
|
||||
get "/table/:exp" => |_state: State<String>| async {}
|
||||
),
|
||||
String
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_routes() {
|
||||
let _router: Router<()> = routes!(
|
||||
@ -104,6 +125,6 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_join_routes() {
|
||||
let _router: Router<()> = join_routes![Router::new(), Router::new()];
|
||||
let _router: Router = join_routes![Router::new(), Router::new()];
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ pub mod axum;
|
||||
pub mod io;
|
||||
pub mod nom;
|
||||
pub mod serde;
|
||||
mod traits;
|
||||
pub mod vector;
|
||||
|
||||
#[cfg(feature = "derive")]
|
||||
|
@ -1,11 +1,8 @@
|
||||
#[cfg(feature = "nom")]
|
||||
use nom::{error::Error, IResult};
|
||||
|
||||
#[cfg(feature = "nom")]
|
||||
pub trait IntoResult<T> {
|
||||
type Error;
|
||||
fn into_result(self) -> Result<T, Self::Error>;
|
||||
}
|
||||
use {
|
||||
crate::traits::IntoResult,
|
||||
nom::{error::Error, IResult},
|
||||
};
|
||||
|
||||
#[cfg(feature = "nom")]
|
||||
impl<T, R> IntoResult<T> for IResult<R, T> {
|
||||
@ -17,9 +14,10 @@ impl<T, R> IntoResult<T> for IResult<R, T> {
|
||||
|
||||
#[cfg(all(test, feature = "nom"))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use nom::character::complete::char as c;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn parse_char(input: &str) -> IResult<&str, char> {
|
||||
c('A')(input)
|
||||
}
|
||||
|
5
src/traits.rs
Normal file
5
src/traits.rs
Normal file
@ -0,0 +1,5 @@
|
||||
/// Converts a type T into a Result<T, E>
|
||||
pub trait IntoResult<T> {
|
||||
type Error;
|
||||
fn into_result(self) -> Result<T, Self::Error>;
|
||||
}
|
Reference in New Issue
Block a user