use alux_http::{FromPartsAlg, read_cookies, read_header_name};
use alux_http_parts::ReadParts;
use core::fmt::Display;
use core::future::Future;
use core::marker::PhantomData;
use salvo::Request;
use salvo::http::header;
use salvo::http::{HeaderMap, StatusError};
use serde::de::DeserializeOwned;
pub struct SalvoPathInput<Input>(PhantomData<Input>);
pub struct SalvoQueryInput<Input>(PhantomData<Input>);
pub struct SalvoBodyInput<Input>(PhantomData<Input>);
pub struct SalvoFormInput<Input>(PhantomData<Input>);
pub struct SalvoRawBodyInput<Input>(PhantomData<Input>);
pub struct SalvoHeadInput<Input>(PhantomData<Input>);
pub trait FromHeadersAlg: Sized {
fn from_headers(headers: &HeaderMap) -> Option<Self>;
}
impl FromHeadersAlg for HeaderMap {
fn from_headers(headers: &HeaderMap) -> Option<Self> {
Some(headers.clone())
}
}
pub trait FromPayloadAlg: Sized {
fn from_payload(payload: &[u8]) -> Option<Self>;
}
impl FromPayloadAlg for Vec<u8> {
fn from_payload(payload: &[u8]) -> Option<Self> {
Some(payload.to_vec())
}
}
impl FromPayloadAlg for String {
fn from_payload(payload: &[u8]) -> Option<Self> {
Self::from_utf8(payload.to_vec()).ok()
}
}
pub(crate) trait SalvoInputAlg<Output> {
fn extract(request: &mut Request) -> impl Future<Output = Result<Output, StatusError>> + Send;
}
fn unreadable(role: &str, reason: &str) -> StatusError {
StatusError::bad_request().brief(format!("the {role} could not be read: {reason}"))
}
macro_rules! salvo_parsed {
($marker:ident, $parse:ident, $role:literal) => {
impl<Input> SalvoInputAlg<Input> for $marker<Input>
where
Input: DeserializeOwned + Send,
{
async fn extract(request: &mut Request) -> Result<Input, StatusError> {
request.$parse().map_err(|error| unreadable($role, &error.to_string()))
}
}
};
}
salvo_parsed!(SalvoQueryInput, parse_queries, "query");
impl<Input> SalvoInputAlg<Input> for SalvoPathInput<Input>
where
Input: DeserializeOwned + Send,
{
async fn extract(request: &mut Request) -> Result<Input, StatusError> {
let bound = request.params().keys().cloned().collect::<Vec<_>>();
if let [only] = bound.as_slice()
&& let Some(value) = request.param::<Input>(only)
{
return Ok(value);
}
request.parse_params().map_err(|error| unreadable("path", &error.to_string()))
}
}
macro_rules! salvo_awaited {
($marker:ident, $parse:ident, $role:literal) => {
impl<Input> SalvoInputAlg<Input> for $marker<Input>
where
Input: DeserializeOwned + Send,
{
async fn extract(request: &mut Request) -> Result<Input, StatusError> {
request.$parse().await.map_err(|error| unreadable($role, &error.to_string()))
}
}
};
}
salvo_awaited!(SalvoBodyInput, parse_json, "body");
salvo_awaited!(SalvoFormInput, parse_form, "form");
impl<Input> SalvoInputAlg<Input> for SalvoRawBodyInput<Input>
where
Input: FromPayloadAlg + Send,
{
async fn extract(request: &mut Request) -> Result<Input, StatusError> {
let payload = request.payload().await.map_err(|error| unreadable("body", &error.to_string()))?;
Input::from_payload(payload).ok_or_else(|| unreadable("body", "it states something else"))
}
}
pub struct SalvoCookieInput<Input>(PhantomData<Input>);
pub struct SalvoHeaderInput<Input>(PhantomData<Input>);
fn headers_of<Input>(stated: impl Iterator<Item = (String, String)>) -> Result<Input, String>
where
Input: DeserializeOwned,
{
let named = stated.map(|(name, value)| (read_header_name(&name), value)).collect::<Vec<_>>();
let encoded = serde_urlencoded::to_string(&named).map_err(|error| error.to_string())?;
serde_urlencoded::from_str(&encoded).map_err(|error| error.to_string())
}
fn cookies_of<Input>(header: Option<&str>) -> Result<Input, String>
where
Input: DeserializeOwned,
{
let stated = read_cookies(header.unwrap_or_default());
let encoded = serde_urlencoded::to_string(&stated).map_err(|error| error.to_string())?;
serde_urlencoded::from_str(&encoded).map_err(|error| error.to_string())
}
impl<Input> SalvoInputAlg<Input> for SalvoCookieInput<Input>
where
Input: DeserializeOwned + Send,
{
async fn extract(request: &mut Request) -> Result<Input, StatusError> {
let header = request.headers().get(header::COOKIE).and_then(|value| value.to_str().ok());
cookies_of(header).map_err(|error| unreadable("cookies", &error))
}
}
impl<Input> SalvoInputAlg<Input> for SalvoHeadInput<Input>
where
Input: FromHeadersAlg + Send,
{
async fn extract(request: &mut Request) -> Result<Input, StatusError> {
Input::from_headers(request.headers()).ok_or_else(|| unreadable("headers", "they state something else"))
}
}
impl<Input> SalvoInputAlg<Input> for SalvoHeaderInput<Input>
where
Input: DeserializeOwned + Send,
{
async fn extract(request: &mut Request) -> Result<Input, StatusError> {
let stated = request
.headers()
.iter()
.filter_map(|(name, value)| Some((name.to_string(), value.to_str().ok()?.to_owned())))
.collect::<Vec<_>>();
headers_of(stated.into_iter()).map_err(|error| unreadable("headers", &error))
}
}
pub struct SalvoMultipartInput<Input>(PhantomData<Input>);
impl<Input> SalvoInputAlg<Input> for SalvoMultipartInput<Input>
where
Input: FromPartsAlg<ReadParts> + Send,
Input::Error: Display,
{
async fn extract(request: &mut Request) -> Result<Input, StatusError> {
let media_type = request
.headers()
.get(header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.ok_or_else(|| unreadable("parts", "the body states no media type"))?
.to_owned();
let body = request.payload().await.map_err(|error| unreadable("parts", &error.to_string()))?.to_vec();
let parts = ReadParts::new(&media_type, body).map_err(|error| unreadable("parts", &error.to_string()))?;
Input::from_parts(parts).await.map_err(|error| unreadable("parts", &error.to_string()))
}
}
pub(crate) trait SalvoInputsAlg<Outputs> {
fn extract(request: &mut Request) -> impl Future<Output = Result<Outputs, StatusError>> + Send;
}
impl SalvoInputsAlg<()> for () {
async fn extract(_request: &mut Request) -> Result<(), StatusError> {
Ok(())
}
}
macro_rules! salvo_inputs {
($($input:ident => $output:ident),+ $(,)?) => {
impl<$($input, $output),+> SalvoInputsAlg<($($output,)+)> for ($($input,)+)
where
$($input: SalvoInputAlg<$output>, $output: Send,)+
{
async fn extract(request: &mut Request) -> Result<($($output,)+), StatusError> {
Ok(($($input::extract(request).await?,)+))
}
}
};
}
salvo_inputs!(I1 => O1);
salvo_inputs!(I1 => O1, I2 => O2);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5, I6 => O6);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5, I6 => O6, I7 => O7);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5, I6 => O6, I7 => O7, I8 => O8);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5, I6 => O6, I7 => O7, I8 => O8, I9 => O9);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5, I6 => O6, I7 => O7, I8 => O8, I9 => O9, I10 => O10);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5, I6 => O6, I7 => O7, I8 => O8, I9 => O9, I10 => O10, I11 => O11);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5, I6 => O6, I7 => O7, I8 => O8, I9 => O9, I10 => O10, I11 => O11, I12 => O12);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5, I6 => O6, I7 => O7, I8 => O8, I9 => O9, I10 => O10, I11 => O11, I12 => O12, I13 => O13);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5, I6 => O6, I7 => O7, I8 => O8, I9 => O9, I10 => O10, I11 => O11, I12 => O12, I13 => O13, I14 => O14);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5, I6 => O6, I7 => O7, I8 => O8, I9 => O9, I10 => O10, I11 => O11, I12 => O12, I13 => O13, I14 => O14, I15 => O15);
salvo_inputs!(I1 => O1, I2 => O2, I3 => O3, I4 => O4, I5 => O5, I6 => O6, I7 => O7, I8 => O8, I9 => O9, I10 => O10, I11 => O11, I12 => O12, I13 => O13, I14 => O14, I15 => O15, I16 => O16);