Skip to content

Commit 0aeebac

Browse files
committed
Refactor Builder Adapter common to both warp and axum
Resulted in some minor code re-org as well Signed-off-by: Jim Crossley <[email protected]>
1 parent 5cc2fdd commit 0aeebac

File tree

9 files changed

+20
-58
lines changed

9 files changed

+20
-58
lines changed

src/binding/axum/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
7878
pub mod extract;
7979
pub mod response;
80-
mod server_response;
8180

8281
#[cfg(test)]
8382
mod tests {

src/binding/axum/response.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use axum_lib as axum;
33
use axum::{body::Body, http::Response, response::IntoResponse};
44
use http::{header, StatusCode};
55

6-
use super::server_response::event_to_response;
6+
use crate::binding::http::builder::adapter::to_response;
77
use crate::event::Event;
88

99
impl IntoResponse for Event {
1010
type Body = Body;
1111
type BodyError = <Self::Body as axum::body::HttpBody>::Error;
1212

1313
fn into_response(self) -> Response<Body> {
14-
match event_to_response(self) {
14+
match to_response(self) {
1515
Ok(resp) => resp,
1616
Err(err) => Response::builder()
1717
.status(StatusCode::INTERNAL_SERVER_ERROR)

src/binding/axum/server_response.rs renamed to src/binding/http/builder/adapter.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use axum_lib as axum;
2-
3-
use axum::{body::Body, http::Response};
1+
use http::Response;
2+
use hyper::body::Body;
43
use std::cell::Cell;
54

65
use crate::binding::http::{Builder, Serializer};
@@ -28,7 +27,7 @@ impl Builder<Response<Body>> for Adapter {
2827
}
2928
}
3029

31-
pub fn event_to_response(event: Event) -> std::result::Result<Response<Body>, Error> {
30+
pub fn to_response(event: Event) -> std::result::Result<Response<Body>, Error> {
3231
BinaryDeserializer::deserialize_binary(
3332
event,
3433
Serializer::new(Adapter {

src/binding/http/builder/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[cfg(feature = "hyper")]
2+
pub mod adapter;
3+
4+
use crate::message::Result;
5+
6+
pub trait Builder<R> {
7+
fn header(&mut self, key: &str, value: http::header::HeaderValue);
8+
fn body(&mut self, bytes: Vec<u8>) -> Result<R>;
9+
fn finish(&mut self) -> Result<R>;
10+
}

src/binding/http/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod builder;
12
mod deserializer;
23
mod headers;
34

@@ -9,7 +10,7 @@ use deserializer::Deserializer;
910
pub use headers::Headers;
1011
mod serializer;
1112

12-
pub use serializer::Builder;
13+
pub use builder::Builder;
1314
pub use serializer::Serializer;
1415

1516
pub static SPEC_VERSION_HEADER: &str = "ce-specversion";

src/binding/http/serializer.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{cell::RefCell, rc::Rc};
22

3+
use crate::binding::http::builder::Builder;
34
use crate::binding::{
45
http::{header_prefix, SPEC_VERSION_HEADER},
56
CLOUDEVENTS_JSON_HEADER,
@@ -17,12 +18,6 @@ macro_rules! str_to_header_value {
1718
};
1819
}
1920

20-
pub trait Builder<R> {
21-
fn header(&mut self, key: &str, value: http::header::HeaderValue);
22-
fn body(&mut self, bytes: Vec<u8>) -> Result<R>;
23-
fn finish(&mut self) -> Result<R>;
24-
}
25-
2621
pub struct Serializer<T> {
2722
builder: Rc<RefCell<dyn Builder<T>>>,
2823
}

src/binding/warp/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,5 @@
6161
//! warp::serve(routes).run(([127, 0, 0, 1], 3030));
6262
//! ```
6363
64-
mod server_response;
65-
6664
pub mod filter;
6765
pub mod reply;

src/binding/warp/reply.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use warp_lib as warp;
22

3-
use super::server_response::event_to_response;
3+
use crate::binding::http::builder::adapter::to_response;
44

55
use crate::Event;
66
use http::StatusCode;
@@ -20,7 +20,7 @@ use warp::reply::Response;
2020
/// .map(|| from_event(Event::default()));
2121
/// ```
2222
pub fn from_event(event: Event) -> Response {
23-
match event_to_response(event) {
23+
match to_response(event) {
2424
Ok(response) => response,
2525
Err(e) => warp::http::response::Response::builder()
2626
.status(StatusCode::INTERNAL_SERVER_ERROR)

src/binding/warp/server_response.rs

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)