Skip to content

Commit

Permalink
fix: param and query transforms (#208)
Browse files Browse the repository at this point in the history
* fix: param and query transforms

* finalize

* cleanup
  • Loading branch information
elcharitas authored Dec 7, 2024
1 parent cd9c6fe commit d178081
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions crates/shared/src/server/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ use multer::Multipart;
use serde::Deserialize;

use crate::server::NgynContext;
use std::borrow::Cow;

/// Represents a transformer trait.
pub trait Transformer<'a> {
/// Transforms the given `NgynContext` and `NgynResponse` and returns an instance of `Self`.
/// Transforms the given `NgynContext` and returns an instance of `Self`.
///
/// ### Arguments
///
/// * `cx` - The mutable reference to the `NgynContext`.
/// * `res` - The mutable reference to the `NgynResponse`.
///
/// ### Examples
///
Expand All @@ -39,12 +37,11 @@ pub trait Transformer<'a> {
pub struct Transducer;

impl<'a> Transducer {
/// Reduces the given `NgynContext` and `NgynResponse` using the specified `Transformer` and returns an instance of `S`.
/// Reduces the given `NgynContext` using the specified `Transformer` and returns an instance of `S`.
///
/// ### Arguments
///
/// * `cx` - The mutable reference to the `NgynContext`.
/// * `res` - The mutable reference to the `NgynResponse`.
///
/// ### Examples
///
Expand All @@ -70,11 +67,11 @@ impl<'a> Transducer {
}

/// Represents a parameter struct.
pub struct Param {
data: Vec<(Cow<'static, str>, Cow<'static, str>)>,
pub struct Param<'a> {
data: Vec<(&'a str, &'a str)>,
}

impl Param {
impl<'a> Param<'a> {
/// Retrieves the value associated with the specified `id` from the parameter data.
///
/// ### Arguments
Expand All @@ -91,8 +88,8 @@ impl Param {
/// ```rust ignore
/// let param = Param {
/// data: vec![
/// (Cow::Borrowed("id"), Cow::Borrowed("123")),
/// (Cow::Borrowed("name"), Cow::Borrowed("John")),
/// ("id", "123"),
/// ("name", "John"),
/// ],
/// };
///
Expand All @@ -102,15 +99,15 @@ impl Param {
/// ```
pub fn get(&self, id: &str) -> Option<String> {
for (key, value) in &self.data {
if key == id {
if *key == id {
return Some(value.to_string());
}
}
None
}
}

impl Transformer<'_> for Param {
impl<'a: 'b, 'b> Transformer<'a> for Param<'b> {
/// Transforms the given `NgynContext` into a `Param` instance.
///
/// ### Arguments
Expand All @@ -124,29 +121,28 @@ impl Transformer<'_> for Param {
/// ### Examples
///
/// ```rust ignore
/// use crate::{context::NgynContext, NgynResponse};
///
/// let mut cx = NgynContext::new();
/// use crate::context::NgynContext;
///
/// let mut cx = NgynContext::default();
/// let param: Param = Param::transform(&mut cx);
/// ```
fn transform(cx: &mut NgynContext) -> Self {
let data: Vec<(Cow<'static, str>, Cow<'static, str>)> = cx
fn transform(cx: &'a mut NgynContext) -> Self {
let data: Vec<(&'a str, &'a str)> = cx
.params()
.unwrap_or_else(|| panic!("Extracting params should only be done in routes.")) // Infallible, only fails if the route is invalid
.unwrap_or_else(|| panic!("Extracting params should only be done in route handlers.")) // Infallible, only fails if the route is invalid
.iter()
.map(|(key, value)| (Cow::Owned(key.to_string()), Cow::Owned(value.to_string())))
.map(|(key, value)| (key.as_str(), value.as_str()))
.collect();
Param { data }
}
}

/// Represents a query struct.
pub struct Query {
url: http::uri::Uri,
pub struct Query<'q> {
uri: &'q http::uri::Uri,
}

impl Query {
impl<'q> Query<'q> {
/// Retrieves the value associated with the specified `id` from the query parameters.
///
/// ### Arguments
Expand All @@ -164,14 +160,14 @@ impl Query {
/// use hyper::Uri;
///
/// let uri: Uri = "https://example.com/?id=123&name=John".parse().unwrap();
/// let query = Query { url: uri };
/// let query = Query { uri: &uri };
///
/// assert_eq!(query.get("id"), Some("123".to_string()));
/// assert_eq!(query.get("name"), Some("John".to_string()));
/// assert_eq!(query.get("age"), None);
/// ```
pub fn get(&self, id: &str) -> Option<String> {
let query = self.url.query().unwrap_or("");
let query = self.uri.query().unwrap_or("");
let query = url::form_urlencoded::parse(query.as_bytes());
for (key, value) in query {
if key == id {
Expand All @@ -182,7 +178,7 @@ impl Query {
}
}

impl Transformer<'_> for Query {
impl<'a: 'q, 'q> Transformer<'a> for Query<'q> {
/// Transforms the given `NgynContext` into a `Query` instance.
///
/// ### Arguments
Expand All @@ -196,7 +192,7 @@ impl Transformer<'_> for Query {
/// ### Examples
///
/// ```rust ignore
/// use crate::{context::NgynContext, NgynResponse};
/// use crate::context::NgynContext;
/// use hyper::Uri;
///
/// let mut cx = NgynContext::default();
Expand All @@ -206,9 +202,9 @@ impl Transformer<'_> for Query {
///
/// let query: Query = Query::transform(&mut cx);
/// ```
fn transform(cx: &mut NgynContext) -> Self {
fn transform(cx: &'a mut NgynContext) -> Self {
Query {
url: cx.request().uri().clone(),
uri: cx.request().uri(),
}
}
}
Expand Down Expand Up @@ -320,9 +316,9 @@ impl<'a: 'b, 'b> Transformer<'a> for Body<'b> {
/// ### Examples
///
/// ```rust ignore
/// use crate::{context::NgynContext, NgynResponse};
/// use crate::context::NgynContext;
///
/// let mut cx = NgynContext::new();
/// let mut cx = NgynContext::default();
///
/// let dto: Body = Body::transform(&mut cx);
/// ```
Expand Down

0 comments on commit d178081

Please sign in to comment.