Reflected changes to actix-web module
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
parent
534cf01fd2
commit
4814505b92
|
@ -4,8 +4,8 @@ use actix_web::web::{Bytes, BytesMut};
|
|||
use actix_web::{web, HttpMessage, HttpRequest};
|
||||
use cloudevents::event::SpecVersion;
|
||||
use cloudevents::message::{
|
||||
BinaryDeserializer, BinarySerializer, Encoding, Error, MessageAttributeValue,
|
||||
MessageDeserializer, StructuredDeserializer, StructuredSerializer,
|
||||
BinaryDeserializer, BinarySerializer, Encoding, MessageAttributeValue,
|
||||
MessageDeserializer, StructuredDeserializer, StructuredSerializer, Result
|
||||
};
|
||||
use cloudevents::{message, Event};
|
||||
use futures::StreamExt;
|
||||
|
@ -27,7 +27,7 @@ impl<'a> BinaryDeserializer for HttpRequestDeserializer<'a> {
|
|||
fn deserialize_binary<R: Sized, V: BinarySerializer<R>>(
|
||||
self,
|
||||
mut visitor: V,
|
||||
) -> Result<R, Error> {
|
||||
) -> Result<R> {
|
||||
if self.encoding() != Encoding::BINARY {
|
||||
return Err(message::Error::WrongEncoding {});
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ impl<'a> BinaryDeserializer for HttpRequestDeserializer<'a> {
|
|||
unwrap_optional_header!(self.req.headers(), headers::SPEC_VERSION_HEADER).unwrap()?,
|
||||
)?;
|
||||
|
||||
visitor.set_spec_version(spec_version.clone())?;
|
||||
visitor = visitor.set_spec_version(spec_version.clone())?;
|
||||
|
||||
let attributes = cloudevents::event::spec_version::ATTRIBUTE_NAMES
|
||||
.get(&spec_version)
|
||||
|
@ -50,12 +50,12 @@ impl<'a> BinaryDeserializer for HttpRequestDeserializer<'a> {
|
|||
let name = &hn.as_str()["ce-".len()..];
|
||||
|
||||
if attributes.contains(&name) {
|
||||
visitor.set_attribute(
|
||||
visitor = visitor.set_attribute(
|
||||
name,
|
||||
MessageAttributeValue::String(String::from(header_value_to_str!(hv)?)),
|
||||
)?
|
||||
} else {
|
||||
visitor.set_extension(
|
||||
visitor = visitor.set_extension(
|
||||
name,
|
||||
MessageAttributeValue::String(String::from(header_value_to_str!(hv)?)),
|
||||
)?
|
||||
|
@ -63,7 +63,7 @@ impl<'a> BinaryDeserializer for HttpRequestDeserializer<'a> {
|
|||
}
|
||||
|
||||
if let Some(hv) = self.req.headers().get("content-type") {
|
||||
visitor.set_attribute(
|
||||
visitor = visitor.set_attribute(
|
||||
"datacontenttype",
|
||||
MessageAttributeValue::String(String::from(header_value_to_str!(hv)?)),
|
||||
)?
|
||||
|
@ -81,7 +81,7 @@ impl<'a> StructuredDeserializer for HttpRequestDeserializer<'a> {
|
|||
fn deserialize_structured<R: Sized, V: StructuredSerializer<R>>(
|
||||
self,
|
||||
visitor: V,
|
||||
) -> Result<R, Error> {
|
||||
) -> Result<R> {
|
||||
if self.encoding() != Encoding::STRUCTURED {
|
||||
return Err(message::Error::WrongEncoding {});
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ impl<'a> MessageDeserializer for HttpRequestDeserializer<'a> {
|
|||
pub async fn request_to_event(
|
||||
req: &HttpRequest,
|
||||
mut payload: web::Payload,
|
||||
) -> Result<Event, actix_web::error::Error> {
|
||||
) -> std::result::Result<Event, actix_web::error::Error> {
|
||||
let mut bytes = BytesMut::new();
|
||||
while let Some(item) = payload.next().await {
|
||||
bytes.extend_from_slice(&item?);
|
||||
|
|
|
@ -4,7 +4,7 @@ use actix_web::http::{HeaderName, HeaderValue};
|
|||
use actix_web::HttpResponse;
|
||||
use cloudevents::event::SpecVersion;
|
||||
use cloudevents::message::{
|
||||
BinaryDeserializer, BinarySerializer, Error, MessageAttributeValue, SerializationResult,
|
||||
BinaryDeserializer, BinarySerializer, MessageAttributeValue, Result,
|
||||
StructuredSerializer,
|
||||
};
|
||||
use cloudevents::Event;
|
||||
|
@ -21,41 +21,41 @@ impl HttpResponseSerializer {
|
|||
}
|
||||
|
||||
impl BinarySerializer<HttpResponse> for HttpResponseSerializer {
|
||||
fn set_spec_version(&mut self, spec_version: SpecVersion) -> SerializationResult {
|
||||
fn set_spec_version(mut self, spec_version: SpecVersion) -> Result<Self> {
|
||||
self.builder.set_header(
|
||||
headers::SPEC_VERSION_HEADER.clone(),
|
||||
str_to_header_value!(spec_version.as_str())?,
|
||||
);
|
||||
SerializationResult::Ok(())
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn set_attribute(&mut self, name: &str, value: MessageAttributeValue) -> SerializationResult {
|
||||
fn set_attribute(mut self, name: &str, value: MessageAttributeValue) -> Result<Self> {
|
||||
self.builder.set_header(
|
||||
headers::ATTRIBUTES_TO_HEADERS.get(name).unwrap().clone(),
|
||||
str_to_header_value!(value.to_string().as_str())?,
|
||||
);
|
||||
SerializationResult::Ok(())
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn set_extension(&mut self, name: &str, value: MessageAttributeValue) -> SerializationResult {
|
||||
fn set_extension(mut self, name: &str, value: MessageAttributeValue) -> Result<Self> {
|
||||
self.builder.set_header(
|
||||
attribute_name_to_header!(name)?,
|
||||
str_to_header_value!(value.to_string().as_str())?,
|
||||
);
|
||||
SerializationResult::Ok(())
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn end_with_data(mut self, bytes: Vec<u8>) -> Result<HttpResponse, Error> {
|
||||
fn end_with_data(mut self, bytes: Vec<u8>) -> Result<HttpResponse> {
|
||||
Ok(self.builder.body(bytes))
|
||||
}
|
||||
|
||||
fn end(mut self) -> Result<HttpResponse, Error> {
|
||||
fn end(mut self) -> Result<HttpResponse> {
|
||||
Ok(self.builder.finish())
|
||||
}
|
||||
}
|
||||
|
||||
impl StructuredSerializer<HttpResponse> for HttpResponseSerializer {
|
||||
fn set_structured_event(mut self, bytes: Vec<u8>) -> Result<HttpResponse, Error> {
|
||||
fn set_structured_event(mut self, bytes: Vec<u8>) -> Result<HttpResponse> {
|
||||
Ok(self
|
||||
.builder
|
||||
.set_header(
|
||||
|
@ -70,7 +70,7 @@ impl StructuredSerializer<HttpResponse> for HttpResponseSerializer {
|
|||
pub async fn event_to_response(
|
||||
event: Event,
|
||||
response: HttpResponseBuilder,
|
||||
) -> Result<HttpResponse, actix_web::error::Error> {
|
||||
) -> std::result::Result<HttpResponse, actix_web::error::Error> {
|
||||
BinaryDeserializer::deserialize_binary(event, HttpResponseSerializer::new(response))
|
||||
.map_err(actix_web::error::ErrorBadRequest)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::{BinarySerializer, Encoding, Error, StructuredSerializer, Result};
|
||||
use super::{BinarySerializer, Encoding, StructuredSerializer, Result};
|
||||
use crate::Event;
|
||||
|
||||
pub trait StructuredDeserializer
|
||||
|
|
Loading…
Reference in New Issue