Removed tokio feature and added tokio to axum feature

This commit is contained in:
Martin Berg Alstad 2024-06-30 23:39:00 +02:00
parent 0898a50166
commit 963c0610e5
4 changed files with 9 additions and 14 deletions

2
Cargo.lock generated
View File

@ -294,7 +294,7 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "lib" name = "lib"
version = "1.3.0" version = "1.3.1"
dependencies = [ dependencies = [
"axum", "axum",
"derive", "derive",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "lib" name = "lib"
version = "1.3.0" version = "1.3.1"
edition = "2021" edition = "2021"
authors = ["Martin Berg Alstad"] authors = ["Martin Berg Alstad"]
@ -27,8 +27,7 @@ serde = { version = "1.0.203", optional = true, features = ["derive"] }
derive = { path = "derive", optional = true } derive = { path = "derive", optional = true }
[features] [features]
axum = ["dep:axum", "dep:tower", "dep:tower-http", "dep:thiserror", "dep:tracing", "dep:tracing-subscriber"] axum = ["dep:axum", "dep:tower", "dep:tower-http", "dep:thiserror", "dep:tracing", "dep:tracing-subscriber", "dep:tokio"]
tokio = ["dep:tokio"]
io = ["dep:tokio", "dep:tokio-util"] io = ["dep:tokio", "dep:tokio-util"]
iter = [] iter = []
nom = ["dep:nom"] nom = ["dep:nom"]

View File

@ -1,6 +1,7 @@
use { use {
axum::{extract::Request, handler::Handler, Router, ServiceExt}, axum::{extract::Request, handler::Handler, Router, ServiceExt},
std::net::Ipv4Addr, std::{io, net::Ipv4Addr, net::SocketAddr},
tokio::net::TcpListener,
tower::layer::Layer, tower::layer::Layer,
tower_http::{ tower_http::{
cors::CorsLayer, cors::CorsLayer,
@ -10,8 +11,6 @@ use {
}, },
tracing::{info, Level}, tracing::{info, Level},
}; };
#[cfg(feature = "tokio")]
use {std::io, std::net::SocketAddr, tokio::net::TcpListener};
// TODO trim trailing slash into macro > let _app = NormalizePathLayer::trim_trailing_slash().layer(create_app!(routes)); // TODO trim trailing slash into macro > let _app = NormalizePathLayer::trim_trailing_slash().layer(create_app!(routes));
#[macro_export] #[macro_export]
@ -77,7 +76,6 @@ impl AppBuilder {
self self
} }
#[cfg(feature = "tokio")]
pub async fn serve(self) -> io::Result<()> { pub async fn serve(self) -> io::Result<()> {
let _ = fmt_trace(); // Allowed to fail let _ = fmt_trace(); // Allowed to fail
let listener = self.listener().await?; let listener = self.listener().await?;
@ -92,7 +90,6 @@ impl AppBuilder {
Ok(()) Ok(())
} }
#[cfg(feature = "tokio")]
async fn listener(&self) -> io::Result<TcpListener> { async fn listener(&self) -> io::Result<TcpListener> {
let addr = SocketAddr::from(self.socket.unwrap_or((Ipv4Addr::UNSPECIFIED, 8000))); let addr = SocketAddr::from(self.socket.unwrap_or((Ipv4Addr::UNSPECIFIED, 8000)));
info!("Initializing server on: {addr}"); info!("Initializing server on: {addr}");
@ -128,7 +125,6 @@ mod tests {
use super::*; use super::*;
#[cfg(feature = "tokio")]
mod tokio_tests { mod tokio_tests {
use std::time::Duration; use std::time::Duration;

View File

@ -1,4 +1,4 @@
#[cfg(feature = "tokio")] #[cfg(feature = "io")]
use {crate::io::file, axum::body::Body, axum::response::Html, std::io}; use {crate::io::file, axum::body::Body, axum::response::Html, std::io};
/// Load an HTML file from the given file path, relative to the current directory. /// Load an HTML file from the given file path, relative to the current directory.
@ -10,7 +10,7 @@ use {crate::io::file, axum::body::Body, axum::response::Html, std::io};
/// ``` /// ```
/// let html = async { lib::axum::load::load_html("openapi.html").await.unwrap() }; /// let html = async { lib::axum::load::load_html("openapi.html").await.unwrap() };
/// ``` /// ```
#[cfg(feature = "tokio")] #[cfg(feature = "io")]
pub async fn load_html<Path>(file_path: Path) -> Result<Html<Body>, io::Error> pub async fn load_html<Path>(file_path: Path) -> Result<Html<Body>, io::Error>
where where
Path: AsRef<std::path::Path>, Path: AsRef<std::path::Path>,
@ -18,7 +18,7 @@ where
load_file(file_path).await.map(Html) load_file(file_path).await.map(Html)
} }
#[cfg(feature = "tokio")] #[cfg(feature = "io")]
pub async fn load_file<Path>(file_path: Path) -> Result<Body, io::Error> pub async fn load_file<Path>(file_path: Path) -> Result<Body, io::Error>
where where
Path: AsRef<std::path::Path>, Path: AsRef<std::path::Path>,
@ -70,7 +70,7 @@ mod tests {
load_html!("load.rs", "{{replace_me}}" => "hello", "{{replace_me_too}}" => "world"); load_html!("load.rs", "{{replace_me}}" => "hello", "{{replace_me_too}}" => "world");
} }
#[cfg(feature = "tokio")] #[cfg(feature = "io")]
mod tokio { mod tokio {
use super::super::*; use super::super::*;