Skip to content

Commit

Permalink
use Bytes instead of Buf
Browse files Browse the repository at this point in the history
  • Loading branch information
eserilev committed Dec 19, 2023
1 parent 8a5f3f3 commit 3e8f176
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl BeaconNodeHttpClient {
builder = builder.timeout(timeout);
}

let serialized_body = serde_json::to_vec(body).unwrap();
let serialized_body = serde_json::to_vec(body).map_err(Error::InvalidJson)?;

let response = builder.body(serialized_body).send().await?;
ok_or_error(response).await
Expand Down
10 changes: 5 additions & 5 deletions common/warp_utils/src/json.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bytes::{Buf, Bytes};
use bytes::Bytes;
use serde::de::DeserializeOwned;
use std::error::Error as StdError;
use warp::{Filter, Rejection};
Expand All @@ -10,13 +10,13 @@ struct Json;
type BoxError = Box<dyn StdError + Send + Sync>;

impl Json {
fn decode<B: Buf, T: DeserializeOwned>(mut buf: B) -> Result<T, BoxError> {
serde_json::from_slice(&buf.copy_to_bytes(buf.remaining())).map_err(Into::into)
fn decode<T: DeserializeOwned>(bytes: Bytes) -> Result<T, BoxError> {
serde_json::from_slice(&bytes).map_err(Into::into)
}
}

pub fn json<T: DeserializeOwned + Send>() -> impl Filter<Extract = (T,), Error = Rejection> + Copy {
warp::body::bytes().and_then(|buf: Bytes| async move {
Json::decode(buf).map_err(|err| reject::custom_bad_request(format!("{:?}", err)))
warp::body::bytes().and_then(|bytes: Bytes| async move {
Json::decode(bytes).map_err(|err| reject::custom_bad_request(format!("{:?}", err)))
})
}

0 comments on commit 3e8f176

Please sign in to comment.