Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix compilation #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use actix_http::error::PayloadError;
use actix_http::http::StatusCode;
use actix_http::ResponseError;
use actix_web::HttpResponse;
use futures_util::core_reexport::fmt::{Display, Formatter};

#[derive(Debug)]
pub struct CborError(serde_cbor::Error);
Expand Down Expand Up @@ -40,8 +39,8 @@ impl From<PayloadError> for CborPayloadError {
}
}

impl Display for CborPayloadError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
impl fmt::Display for CborPayloadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CborPayloadError::Overflow => writeln!(f, "Cbor payload size is bigger than allowed"),
CborPayloadError::ContentType => writeln!(f, "Content type error"),
Expand All @@ -61,9 +60,7 @@ impl Error for CborPayloadError {}
impl ResponseError for CborPayloadError {
fn error_response(&self) -> HttpResponse {
match *self {
CborPayloadError::Overflow => {
HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE)
}
CborPayloadError::Overflow => HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE),
_ => HttpResponse::new(StatusCode::BAD_REQUEST),
}
}
Expand All @@ -87,4 +84,4 @@ impl From<serde_cbor::Error> for CborError {
fn from(e: serde_cbor::Error) -> Self {
Self(e)
}
}
}
32 changes: 20 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ extern crate serde;
use std::fmt;
use std::ops::{Deref, DerefMut};

use actix_http::{Payload, PayloadStream, Response};
use actix_http::http::StatusCode;
use actix_web::{FromRequest, HttpRequest, Responder};
use actix_http::{Payload, PayloadStream, Response};
#[cfg(feature = "compress")]
use actix_web::dev::Decompress;
use futures_util::core_reexport::fmt::Formatter;
use futures_util::future::{err, LocalBoxFuture, ok, Ready};
use actix_web::{FromRequest, HttpRequest, Responder};
use futures_util::future::{err, ok, LocalBoxFuture, Ready};
use futures_util::FutureExt;
use serde::de::DeserializeOwned;
use serde::Serialize;
Expand All @@ -39,9 +38,9 @@ pub use body::*;
pub use config::*;
pub use error::*;

mod error;
mod config;
mod body;
mod config;
mod error;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -93,20 +92,26 @@ impl<T> DerefMut for Cbor<T> {
}
}

impl<T> fmt::Debug for Cbor<T> where T: fmt::Debug {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
impl<T> fmt::Debug for Cbor<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Cbor: {:?}", self.0)
}
}

impl<T> Responder for Cbor<T> where T: Serialize {
impl<T> Responder for Cbor<T>
where
T: Serialize,
{
type Error = CborError;
type Future = Ready<Result<Response, Self::Error>>;

fn respond_to(self, _: &HttpRequest) -> Self::Future {
let body = match serde_cbor::to_vec(&self.0) {
Ok(body) => body,
Err(e) => return err(e.into())
Err(e) => return err(e.into()),
};

ok(Response::build(StatusCode::OK)
Expand All @@ -115,7 +120,10 @@ impl<T> Responder for Cbor<T> where T: Serialize {
}
}

impl<T> FromRequest for Cbor<T> where T: DeserializeOwned + 'static {
impl<T> FromRequest for Cbor<T>
where
T: DeserializeOwned + 'static,
{
type Error = actix_web::Error;
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
type Config = CborConfig;
Expand Down Expand Up @@ -148,4 +156,4 @@ impl<T> FromRequest for Cbor<T> where T: DeserializeOwned + 'static {
})
.boxed_local()
}
}
}