Skip to content

Commit

Permalink
Added more From implementations for UnpackedResponseBody
Browse files Browse the repository at this point in the history
  - Implemented additional From conversions for UnpackedResponseBody:
    HyperBody and UnsyncBoxBody<Bytes, E>.

  - Added tests.
  • Loading branch information
danwilliams committed Oct 12, 2023
1 parent 3ea91ba commit aced830
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
31 changes: 31 additions & 0 deletions crates/rubedo/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,20 @@ impl From<&Json> for UnpackedResponseBody {
}
}

impl From<HyperBody> for UnpackedResponseBody {
// from
/// Converts a [`UnsyncBoxBody<Bytes, E>`](UnsyncBoxBody) to an
/// [`UnpackedResponseBody`].
fn from(b: HyperBody) -> Self {
let bytes = executor::block_on(to_bytes(b));
let body = match bytes {
Ok(body) => body.to_vec(),
Err(_) => b"Conversion error".to_vec(),
};
Self { body, ..Default::default() }
}
}

impl From<&str> for UnpackedResponseBody {
// from
/// Converts a [`&str`](str) to an [`UnpackedResponseBody`].
Expand Down Expand Up @@ -1120,6 +1134,23 @@ impl<'a> From<Cow<'a, str>> for UnpackedResponseBody {
}
}

impl<E> From<UnsyncBoxBody<Bytes, E>> for UnpackedResponseBody
where
E: Error + 'static,
{
// from
/// Converts a [`UnsyncBoxBody<Bytes, E>`](UnsyncBoxBody) to an
/// [`UnpackedResponseBody`].
fn from(b: UnsyncBoxBody<Bytes, E>) -> Self {
let bytes = executor::block_on(to_bytes(b));
let body = match bytes {
Ok(body) => body.to_vec(),
Err(_) => b"Conversion error".to_vec(),
};
Self { body, ..Default::default() }
}
}

impl From<u8> for UnpackedResponseBody {
// from
/// Converts a [`u8`] to an [`UnpackedResponseBody`].
Expand Down
26 changes: 26 additions & 0 deletions crates/rubedo/src/tests/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,19 @@ mod unpacked_response_body__traits {
assert_eq!(body, UnpackedResponseBody { body: s!("𐍈").into_bytes(), ..Default::default() });
}
#[test]
fn from__hyper_body() {
let body = UnpackedResponseBody::from(HyperBody::from("This is a test"));
assert_eq!(body, UnpackedResponseBody { body: b"This is a test".to_vec(), ..Default::default() });

let hyper_body = HyperBody::from("This is another test");
let body = UnpackedResponseBody::from(hyper_body);
assert_eq!(body, UnpackedResponseBody { body: b"This is another test".to_vec(), ..Default::default() });
// We cannot compare to the original hyper body after calling from(),
// because it has been consumed.
// Uncommenting the line below would cause a compilation error:
//assert_eq!(hyper_body, "This is another test");
}
#[test]
fn from__json() {
let body = UnpackedResponseBody::from(json!({
"foo": "bar",
Expand Down Expand Up @@ -1086,6 +1099,19 @@ mod unpacked_response_body__traits {
//assert_eq!(cow, "This is a test");
}
#[test]
fn from__unsync_box_body() {
let body = UnpackedResponseBody::from(UnsyncBoxBody::new(s!("This is a test")));
assert_eq!(body, UnpackedResponseBody { body: b"This is a test".to_vec(), ..Default::default() });

let unsync_box = UnsyncBoxBody::new(s!("This is another test"));
let body = UnpackedResponseBody::from(unsync_box);
assert_eq!(body, UnpackedResponseBody { body: b"This is another test".to_vec(), ..Default::default() });
// We cannot compare to the original unsync box after calling from(),
// because it has been consumed.
// Uncommenting the line below would cause a compilation error:
//assert_eq!(unsync_box, "This is another test");
}
#[test]
fn from__u8() {
let body = UnpackedResponseBody::from(65);
assert_eq!(body, UnpackedResponseBody { body: vec![65], ..Default::default() });
Expand Down

0 comments on commit aced830

Please sign in to comment.