xitca-web 0.2.2

an async web framework
Documentation
use std::ops::Deref;

use crate::{body::BodyStream, context::WebContext, error::Error, handler::FromRequest, http::Uri};

#[derive(Debug)]
pub struct UriRef<'a>(pub &'a Uri);

impl Deref for UriRef<'_> {
    type Target = Uri;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}

impl<'a, 'r, C, B> FromRequest<'a, WebContext<'r, C, B>> for UriRef<'a>
where
    B: BodyStream,
{
    type Type<'b> = UriRef<'b>;
    type Error = Error<C>;

    #[inline]
    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
        Ok(UriRef(ctx.req().uri()))
    }
}

#[derive(Debug)]
pub struct UriOwn(pub Uri);

impl Deref for UriOwn {
    type Target = Uri;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a, 'r, C, B> FromRequest<'a, WebContext<'r, C, B>> for UriOwn
where
    B: BodyStream,
{
    type Type<'b> = UriOwn;
    type Error = Error<C>;

    #[inline]
    async fn from_request(ctx: &'a WebContext<'r, C, B>) -> Result<Self, Self::Error> {
        Ok(UriOwn(ctx.req().uri().clone()))
    }
}