Skip to content

Commit f3d106b

Browse files
authored
Account for poem 1.2 breaking changes (#166)
* Account for poem 1.2 breaking changes * Update the poem example, too Signed-off-by: Jim Crossley <[email protected]>
1 parent 7f538a3 commit f3d106b

File tree

5 files changed

+12
-40
lines changed

5 files changed

+12
-40
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ http = { version = "0.2", optional = true }
4747
hyper = { version = "^0.14", optional = true }
4848
axum-lib = { version = "^0.2", optional = true, package = "axum" }
4949
http-body = { version = "^0.4", optional = true }
50-
poem-lib = { version = "1.0.23", optional = true, package = "poem" }
50+
poem-lib = { version = "1", optional = true, package = "poem" }
5151

5252
[target."cfg(not(target_arch = \"wasm32\"))".dependencies]
5353
hostname = "^0.3"

example-projects/poem-example/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77
cloudevents-sdk = { path = "../..", features = ["poem"] }
88
tokio = { version = "1.13", features = ["macros", "rt-multi-thread"] }
99
tracing = "0.1"
10-
poem = { version = "=1.0.23" }
10+
poem = { version = "1" }
1111
tracing-subscriber = "0.2"
1212
serde_json = "1.0"
1313

example-projects/poem-example/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async fn main() -> Result<(), std::io::Error> {
4646
}
4747
tracing_subscriber::fmt::init();
4848

49-
let server = Server::new(TcpListener::bind("127.0.0.1:8080")).await?;
49+
let server = Server::new(TcpListener::bind("127.0.0.1:8080"));
5050
server.run(echo_app()).await
5151
}
5252

@@ -79,7 +79,7 @@ mod tests {
7979
.header("content-type", "application/json")
8080
.body(Body::from_json(&j).unwrap());
8181

82-
let resp: Response = app.call(request).await;
82+
let resp: Response = app.call(request).await.unwrap();
8383
assert_eq!(
8484
resp.headers()
8585
.get("ce-specversion")

src/binding/poem/extractor.rs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,20 @@
11
use async_trait::async_trait;
2-
use poem_lib::error::ReadBodyError;
2+
use poem_lib::error::ResponseError;
33
use poem_lib::http::StatusCode;
4-
use poem_lib::{FromRequest, IntoResponse, Request, RequestBody, Response};
4+
use poem_lib::{FromRequest, Request, RequestBody, Result};
55

66
use crate::binding::http::to_event;
77
use crate::Event;
88

9-
#[derive(Debug)]
10-
pub enum ParseEventError {
11-
ReadBody(ReadBodyError),
12-
ParseEvent(crate::message::Error),
13-
}
14-
15-
impl From<ReadBodyError> for ParseEventError {
16-
fn from(err: ReadBodyError) -> Self {
17-
ParseEventError::ReadBody(err)
18-
}
19-
}
20-
21-
impl From<crate::message::Error> for ParseEventError {
22-
fn from(err: crate::message::Error) -> Self {
23-
ParseEventError::ParseEvent(err)
24-
}
25-
}
26-
27-
impl IntoResponse for ParseEventError {
28-
fn into_response(self) -> Response {
29-
match self {
30-
ParseEventError::ReadBody(err) => err.into_response(),
31-
ParseEventError::ParseEvent(err) => (StatusCode::BAD_REQUEST, err.to_string()).into(),
32-
}
9+
impl ResponseError for crate::message::Error {
10+
fn status(&self) -> StatusCode {
11+
StatusCode::BAD_REQUEST
3312
}
3413
}
3514

3615
#[async_trait]
3716
impl<'a> FromRequest<'a> for Event {
38-
type Error = ParseEventError;
39-
40-
async fn from_request(req: &'a Request, body: &mut RequestBody) -> Result<Self, Self::Error> {
17+
async fn from_request(req: &'a Request, body: &mut RequestBody) -> Result<Self> {
4118
Ok(to_event(req.headers(), body.take()?.into_vec().await?)?)
4219
}
4320
}
@@ -79,12 +56,9 @@ mod tests {
7956
.finish();
8057

8158
let (req, mut body) = req.split();
82-
let resp = Event::from_request(&req, &mut body).await.into_response();
59+
let resp = Event::from_request(&req, &mut body).await.err().unwrap();
8360
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
84-
assert_eq!(
85-
resp.into_body().into_string().await.unwrap(),
86-
"Invalid specversion BAD SPECIFICATION"
87-
);
61+
assert_eq!(resp.to_string(), "Invalid specversion BAD SPECIFICATION");
8862
}
8963

9064
#[tokio::test]

src/binding/poem/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,3 @@
5555
5656
mod extractor;
5757
mod response;
58-
59-
pub use extractor::ParseEventError;

0 commit comments

Comments
 (0)