diff --git a/crates/rubedo/src/http.rs b/crates/rubedo/src/http.rs index 8d1f60f..293f834 100644 --- a/crates/rubedo/src/http.rs +++ b/crates/rubedo/src/http.rs @@ -1070,6 +1070,20 @@ impl From<&Json> for UnpackedResponseBody { } } +impl From for UnpackedResponseBody { + // from + /// Converts a [`UnsyncBoxBody`](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`]. @@ -1120,6 +1134,23 @@ impl<'a> From> for UnpackedResponseBody { } } +impl From> for UnpackedResponseBody +where + E: Error + 'static, +{ + // from + /// Converts a [`UnsyncBoxBody`](UnsyncBoxBody) to an + /// [`UnpackedResponseBody`]. + fn from(b: UnsyncBoxBody) -> 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 for UnpackedResponseBody { // from /// Converts a [`u8`] to an [`UnpackedResponseBody`]. diff --git a/crates/rubedo/src/tests/http.rs b/crates/rubedo/src/tests/http.rs index 3dd48f2..253ed82 100644 --- a/crates/rubedo/src/tests/http.rs +++ b/crates/rubedo/src/tests/http.rs @@ -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", @@ -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() });