Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
173bbc2ca5 | |||
cdc8f5e463 | |||
aba28d1612 | |||
752d1a9d10 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -284,7 +284,7 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lib"
|
name = "lib"
|
||||||
version = "1.1.0"
|
version = "1.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"derive",
|
"derive",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "lib"
|
name = "lib"
|
||||||
version = "1.1.0"
|
version = "1.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Martin Berg Alstad"]
|
authors = ["Martin Berg Alstad"]
|
||||||
|
|
||||||
|
@ -42,8 +42,13 @@ impl AppBuilder {
|
|||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn routes(mut self, routes: &[Router]) -> Self {
|
pub fn route(mut self, route: Router) -> Self {
|
||||||
self.router = routes.iter().cloned().fold(self.router, Router::merge);
|
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
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,8 +82,8 @@ impl AppBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn serve(self) -> io::Result<()> {
|
pub async fn serve(self) -> io::Result<()> {
|
||||||
let listener = self.listener().await?;
|
|
||||||
let _ = fmt_trace();
|
let _ = fmt_trace();
|
||||||
|
let listener = self.listener().await?;
|
||||||
|
|
||||||
if self.normalize_path.unwrap_or(true) {
|
if self.normalize_path.unwrap_or(true) {
|
||||||
let app = NormalizePathLayer::trim_trailing_slash().layer(self.create_app());
|
let app = NormalizePathLayer::trim_trailing_slash().layer(self.create_app());
|
||||||
@ -147,7 +152,7 @@ mod tests {
|
|||||||
let handler = tokio::spawn(async {
|
let handler = tokio::spawn(async {
|
||||||
AppBuilder::new()
|
AppBuilder::new()
|
||||||
.socket((Ipv4Addr::LOCALHOST, 8080))
|
.socket((Ipv4Addr::LOCALHOST, 8080))
|
||||||
.routes(&[Router::new()])
|
.routes([Router::new()])
|
||||||
.fallback(|| async { "Fallback" })
|
.fallback(|| async { "Fallback" })
|
||||||
.cors(CorsLayer::new())
|
.cors(CorsLayer::new())
|
||||||
.normalize_path(true)
|
.normalize_path(true)
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
#[cfg(feature = "axum")]
|
#[cfg(feature = "axum")]
|
||||||
macro_rules! router {
|
macro_rules! router {
|
||||||
($body:expr) => {
|
($body:expr) => {
|
||||||
pub(crate) fn router() -> axum::Router<()> {
|
pub(crate) fn router() -> axum::Router {
|
||||||
$body
|
$body
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -104,6 +104,6 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_join_routes() {
|
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 io;
|
||||||
pub mod nom;
|
pub mod nom;
|
||||||
pub mod serde;
|
pub mod serde;
|
||||||
|
mod traits;
|
||||||
pub mod vector;
|
pub mod vector;
|
||||||
|
|
||||||
#[cfg(feature = "derive")]
|
#[cfg(feature = "derive")]
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
#[cfg(feature = "nom")]
|
#[cfg(feature = "nom")]
|
||||||
use nom::{error::Error, IResult};
|
use {
|
||||||
|
crate::traits::IntoResult,
|
||||||
#[cfg(feature = "nom")]
|
nom::{error::Error, IResult},
|
||||||
pub trait IntoResult<T> {
|
};
|
||||||
type Error;
|
|
||||||
fn into_result(self) -> Result<T, Self::Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "nom")]
|
#[cfg(feature = "nom")]
|
||||||
impl<T, R> IntoResult<T> for IResult<R, T> {
|
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"))]
|
#[cfg(all(test, feature = "nom"))]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
|
||||||
use nom::character::complete::char as c;
|
use nom::character::complete::char as c;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
fn parse_char(input: &str) -> IResult<&str, char> {
|
fn parse_char(input: &str) -> IResult<&str, char> {
|
||||||
c('A')(input)
|
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