Skip to content

Commit

Permalink
api: FromConn for serde_json::Value and querystrong::QueryStrong
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Aug 15, 2023
1 parent b98e4ca commit 8884f1f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ keywords = ["trillium", "framework", "async"]
categories = ["web-programming::http-server", "web-programming"]

[features]
default = ["forms"]
default = ["forms", "querystrong"]
forms = ["serde_urlencoded", "form_urlencoded"]

[dependencies]
form_urlencoded = { version = "1.2.0", optional = true }
log = "0.4.19"
mime = "0.3.17"
querystrong = { version = "0.3.0", optional = true }
serde = { version = "1.0.181", features = ["derive"] }
serde_json = "1.0.104"
serde_path_to_error = "0.1.14"
Expand Down
17 changes: 17 additions & 0 deletions api/src/from_conn.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ApiConnExt;
use trillium::{async_trait, Conn};

/// A trait to extract content from [`Conn`]s to be used as the second
Expand Down Expand Up @@ -37,6 +38,22 @@ impl<E: FromConn> FromConn for Option<E> {
}
}

#[async_trait]
impl FromConn for serde_json::Value {
async fn from_conn(conn: &mut Conn) -> Option<Self> {
let res = conn.deserialize_json::<serde_json::Value>().await;
conn.store_error(res)
}
}

#[cfg(feature = "querystrong")]
#[async_trait]
impl FromConn for querystrong::QueryStrong {
async fn from_conn(conn: &mut Conn) -> Option<Self> {
Some(conn.querystring().parse().unwrap_or_default())
}
}

macro_rules! impl_from_conn_tuple {
($($name:ident)+) => (
#[async_trait]
Expand Down

0 comments on commit 8884f1f

Please sign in to comment.