salvo_core/
lib.rs

1//! The core crate of Salvo web framework.
2//!
3//! `salvo_core` uses a set of [feature flags] to reduce the amount of compiled and
4//! optional dependencies.
5//!
6//! # Feature flags
7//!
8//! | Feature | Description | Default? |
9//! | --- | --- | :---: |
10//! | `cookie` | Support for Cookie | ✔️ |
11//! | `server` | Built-in Server implementation | ✔️ |
12//! | `http1` | Support for HTTP 1.1 protocol | ✔️ |
13//! | `http2` | Support for HTTP 2 protocol | ✔️ |
14//! | `http2-cleartext` | Support for HTTP 2 over cleartext TCP | ❌ |
15//! | `quinn` | Use [quinn](https://crates.io/crates/quinn)  to support HTTP 3 protocol | ❌ |
16//! | `test` | Utilities for testing application | ✔️ |
17//! | `acme` | Automatically obtain certificates through ACME | ❌ |
18//! | `rustls` | TLS built on [`rustls`](https://crates.io/crates/rustls) | ❌ |
19//! | `openssl` | TLS built on [`openssl-tls`](https://crates.io/crates/openssl) | ❌ |
20//! | `native-tls` | TLS built on [`native-tls`](https://crates.io/crates/native-tls) | ❌ |
21//! | `unix` | Listener based on Unix socket | ❌ |
22//! | `anyhow` | Integrate with the [`anyhow`](https://crates.io/crates/anyhow) crate | ❌ |
23//! | `eyre` | Integrate with the [`eyre`](https://crates.io/crates/eyre) crate | ❌ |
24#![doc(html_favicon_url = "https://salvo.rs/favicon-32x32.png")]
25#![doc(html_logo_url = "https://salvo.rs/images/logo.svg")]
26#![cfg_attr(docsrs, feature(doc_cfg))]
27
28/// Re-export `async_trait`.
29pub use async_trait::async_trait;
30pub use hyper;
31pub use salvo_macros::handler;
32
33pub use salvo_macros as macros;
34// https://github.com/bkchr/proc-macro-crate/issues/10
35extern crate self as salvo_core;
36// https://github.com/bkchr/proc-macro-crate/issues/10
37extern crate self as salvo;
38
39#[macro_use]
40mod cfg;
41
42pub mod catcher;
43pub mod conn;
44mod depot;
45mod error;
46pub mod extract;
47pub mod fs;
48pub mod fuse;
49pub mod handler;
50pub mod http;
51pub mod proto;
52pub mod routing;
53pub mod rt;
54#[doc(hidden)]
55pub mod serde;
56cfg_feature! {
57    #![feature ="server"]
58    pub mod server;
59    pub use self::server::Server;
60}
61mod service;
62pub mod writing;
63cfg_feature! {
64    #![feature ="test"]
65    pub mod test;
66}
67cfg_feature! {
68    #![feature ="quinn"]
69    pub use proto::webtransport;
70}
71
72pub use self::conn::Listener;
73pub use self::depot::Depot;
74pub use self::error::{BoxedError, Error};
75pub use self::extract::Extractible;
76pub use self::handler::Handler;
77pub use self::http::{Request, Response};
78pub use self::routing::{FlowCtrl, Router};
79pub use self::service::Service;
80pub use self::writing::{Scribe, Writer};
81/// Result type which has `salvo::Error` as its error type.
82pub type Result<T> = std::result::Result<T, Error>;
83
84/// A list of things that automatically imports into application use salvo_core.
85pub mod prelude {
86    pub use async_trait::async_trait;
87    pub use salvo_macros::{Extractible, handler};
88
89    pub use crate::depot::Depot;
90    pub use crate::http::{Request, Response, StatusCode, StatusError};
91    cfg_feature! {
92        #![feature = "acme"]
93        pub use crate::conn::AcmeListener;
94    }
95    cfg_feature! {
96        #![feature ="rustls"]
97        pub use crate::conn::RustlsListener;
98    }
99    cfg_feature! {
100        #![feature ="native-tls"]
101        pub use crate::conn::NativeTlsListener;
102    }
103    cfg_feature! {
104        #![feature ="openssl"]
105        pub use crate::conn::OpensslListener;
106    }
107    cfg_feature! {
108        #![feature ="quinn"]
109        pub use crate::conn::QuinnListener;
110    }
111    cfg_feature! {
112        #![unix]
113        pub use crate::conn::UnixListener;
114    }
115    pub use crate::conn::{JoinedListener, Listener, TcpListener};
116    pub use crate::handler::{self, Handler};
117    pub use crate::routing::{FlowCtrl, Router};
118    cfg_feature! {
119        #![feature = "server"]
120        pub use crate::server::Server;
121    }
122    pub use crate::service::Service;
123    pub use crate::writing::{Json, Redirect, Scribe, Text, Writer};
124}
125
126#[doc(hidden)]
127pub mod __private {
128    pub use tracing;
129}
130
131#[doc(hidden)]
132pub trait IntoVecString {
133    fn into_vec_string(self) -> Vec<String>;
134}
135
136impl IntoVecString for &'static str {
137    fn into_vec_string(self) -> Vec<String> {
138        vec![self.to_owned()]
139    }
140}
141impl IntoVecString for String {
142    fn into_vec_string(self) -> Vec<String> {
143        vec![self]
144    }
145}
146
147impl<const N: usize> IntoVecString for [&'static str; N] {
148    fn into_vec_string(self) -> Vec<String> {
149        self.into_iter().map(|s| s.into()).collect()
150    }
151}
152
153impl<T> IntoVecString for Vec<T>
154where
155    T: Into<String>,
156{
157    fn into_vec_string(self) -> Vec<String> {
158        self.into_iter().map(|s| s.into()).collect()
159    }
160}
161
162impl<T> IntoVecString for &Vec<T>
163where
164    T: Into<String> + Clone,
165{
166    fn into_vec_string(self) -> Vec<String> {
167        self.iter().map(|s| s.clone().into()).collect()
168    }
169}
170
171#[doc(hidden)]
172#[macro_export]
173macro_rules! for_each_tuple {
174    ($callback:ident) => {
175        $callback! {
176            1 {
177                (0) -> A,
178            }
179            2 {
180                (0) -> A,
181                (1) -> B,
182            }
183            3 {
184                (0) -> A,
185                (1) -> B,
186                (2) -> C,
187            }
188            4 {
189                (0) -> A,
190                (1) -> B,
191                (2) -> C,
192                (3) -> D,
193            }
194            5 {
195                (0) -> A,
196                (1) -> B,
197                (2) -> C,
198                (3) -> D,
199                (4) -> E,
200            }
201            6 {
202                (0) -> A,
203                (1) -> B,
204                (2) -> C,
205                (3) -> D,
206                (4) -> E,
207                (5) -> F,
208            }
209            7 {
210                (0) -> A,
211                (1) -> B,
212                (2) -> C,
213                (3) -> D,
214                (4) -> E,
215                (5) -> F,
216                (6) -> G,
217            }
218            8 {
219                (0) -> A,
220                (1) -> B,
221                (2) -> C,
222                (3) -> D,
223                (4) -> E,
224                (5) -> F,
225                (6) -> G,
226                (7) -> H,
227            }
228        }
229    };
230}