Skip to content

Commit

Permalink
fix(api): implement TryFromConn for Vec<u8> and String
Browse files Browse the repository at this point in the history
instead of using the FromConn blanket impl with Error = ();
  • Loading branch information
jbr committed May 30, 2024
1 parent e66ca2a commit ea8b215
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
17 changes: 1 addition & 16 deletions api/src/from_conn.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use trillium::{async_trait, Conn};

use crate::TryFromConn;
use trillium::{async_trait, Conn};

/// A trait to extract content from [`Conn`]s to be used as the second
/// argument to an api handler. Implement this for your types.
Expand All @@ -18,20 +17,6 @@ impl FromConn for () {
}
}

#[async_trait]
impl FromConn for String {
async fn from_conn(conn: &mut Conn) -> Option<Self> {
conn.request_body_string().await.ok()
}
}

#[async_trait]
impl FromConn for Vec<u8> {
async fn from_conn(conn: &mut Conn) -> Option<Self> {
conn.request_body().await.read_bytes().await.ok()
}
}

#[async_trait]
impl<E: FromConn> FromConn for Option<E> {
async fn from_conn(conn: &mut Conn) -> Option<Self> {
Expand Down
20 changes: 20 additions & 0 deletions api/src/try_from_conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ impl<T: FromConn> TryFromConn for T {
}
}

#[async_trait]
impl TryFromConn for Vec<u8> {
type Error = crate::Error;
async fn try_from_conn(conn: &mut Conn) -> Result<Self, Self::Error> {
conn.request_body()
.await
.read_bytes()
.await
.map_err(Into::into)
}
}

#[async_trait]
impl TryFromConn for String {
type Error = crate::Error;
async fn try_from_conn(conn: &mut Conn) -> Result<Self, Self::Error> {
conn.request_body_string().await.map_err(Into::into)
}
}

#[cfg(feature = "url")]
#[async_trait]
impl TryFromConn for url::Url {
Expand Down

0 comments on commit ea8b215

Please sign in to comment.