mirror of
https://github.com/actix/actix-extras.git
synced 2025-02-02 10:59:03 +01:00
Merge pull request #4 from hstefan/rustfmt
Normalizing code formatting based on actix-web style
This commit is contained in:
commit
0ea3ad0797
@ -32,8 +32,7 @@ fn main() {
|
|||||||
App::new()
|
App::new()
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.service(web::resource("/").route(web::post().to(index)))
|
.service(web::resource("/").route(web::post().to(index)))
|
||||||
})
|
}).bind("127.0.0.1:8081")
|
||||||
.bind("127.0.0.1:8081")
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.shutdown_timeout(1)
|
.shutdown_timeout(1)
|
||||||
.start();
|
.start();
|
||||||
|
2
rustfmt.toml
Normal file
2
rustfmt.toml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
max_width = 89
|
||||||
|
reorder_imports = true
|
71
src/lib.rs
71
src/lib.rs
@ -1,30 +1,31 @@
|
|||||||
extern crate actix;
|
extern crate actix;
|
||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
extern crate bytes;
|
extern crate bytes;
|
||||||
extern crate futures;
|
|
||||||
extern crate derive_more;
|
extern crate derive_more;
|
||||||
|
extern crate futures;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
extern crate http;
|
extern crate http;
|
||||||
|
|
||||||
extern crate prost;
|
extern crate prost;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[macro_use] extern crate prost_derive;
|
#[macro_use]
|
||||||
|
extern crate prost_derive;
|
||||||
|
|
||||||
use std::fmt;
|
|
||||||
use derive_more::Display;
|
use derive_more::Display;
|
||||||
|
use std::fmt;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
use bytes::{BytesMut, IntoBuf};
|
use bytes::{BytesMut, IntoBuf};
|
||||||
use prost::Message;
|
|
||||||
use prost::DecodeError as ProtoBufDecodeError;
|
use prost::DecodeError as ProtoBufDecodeError;
|
||||||
use prost::EncodeError as ProtoBufEncodeError;
|
use prost::EncodeError as ProtoBufEncodeError;
|
||||||
|
use prost::Message;
|
||||||
|
|
||||||
use futures::{Poll, Future, Stream};
|
|
||||||
use actix_web::http::header::{CONTENT_TYPE, CONTENT_LENGTH};
|
|
||||||
use actix_web::{Responder, HttpMessage, HttpRequest, HttpResponse, FromRequest};
|
|
||||||
use actix_web::dev::{HttpResponseBuilder, Payload};
|
use actix_web::dev::{HttpResponseBuilder, Payload};
|
||||||
use actix_web::error::{Error, PayloadError, ResponseError};
|
use actix_web::error::{Error, PayloadError, ResponseError};
|
||||||
|
use actix_web::http::header::{CONTENT_LENGTH, CONTENT_TYPE};
|
||||||
|
use actix_web::{FromRequest, HttpMessage, HttpRequest, HttpResponse, Responder};
|
||||||
|
use futures::{Future, Poll, Stream};
|
||||||
|
|
||||||
#[derive(Debug, Display)]
|
#[derive(Debug, Display)]
|
||||||
pub enum ProtoBufPayloadError {
|
pub enum ProtoBufPayloadError {
|
||||||
@ -46,7 +47,6 @@ pub enum ProtoBufPayloadError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ResponseError for ProtoBufPayloadError {
|
impl ResponseError for ProtoBufPayloadError {
|
||||||
|
|
||||||
fn error_response(&self) -> HttpResponse {
|
fn error_response(&self) -> HttpResponse {
|
||||||
match *self {
|
match *self {
|
||||||
ProtoBufPayloadError::Overflow => HttpResponse::PayloadTooLarge().into(),
|
ProtoBufPayloadError::Overflow => HttpResponse::PayloadTooLarge().into(),
|
||||||
@ -83,13 +83,19 @@ impl<T: Message> DerefMut for ProtoBuf<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Message> fmt::Debug for ProtoBuf<T> where T: fmt::Debug {
|
impl<T: Message> fmt::Debug for ProtoBuf<T>
|
||||||
|
where
|
||||||
|
T: fmt::Debug,
|
||||||
|
{
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "ProtoBuf: {:?}", self.0)
|
write!(f, "ProtoBuf: {:?}", self.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Message> fmt::Display for ProtoBuf<T> where T: fmt::Display {
|
impl<T: Message> fmt::Display for ProtoBuf<T>
|
||||||
|
where
|
||||||
|
T: fmt::Display,
|
||||||
|
{
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt::Display::fmt(&self.0, f)
|
fmt::Display::fmt(&self.0, f)
|
||||||
}
|
}
|
||||||
@ -100,7 +106,6 @@ pub struct ProtoBufConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ProtoBufConfig {
|
impl ProtoBufConfig {
|
||||||
|
|
||||||
/// Change max size of payload. By default max size is 256Kb
|
/// Change max size of payload. By default max size is 256Kb
|
||||||
pub fn limit(&mut self, limit: usize) -> &mut Self {
|
pub fn limit(&mut self, limit: usize) -> &mut Self {
|
||||||
self.limit = limit;
|
self.limit = limit;
|
||||||
@ -115,7 +120,8 @@ impl Default for ProtoBufConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> FromRequest for ProtoBuf<T>
|
impl<T> FromRequest for ProtoBuf<T>
|
||||||
where T: Message + Default + 'static
|
where
|
||||||
|
T: Message + Default + 'static,
|
||||||
{
|
{
|
||||||
type Config = ProtoBufConfig;
|
type Config = ProtoBufConfig;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
@ -123,14 +129,16 @@ impl<T> FromRequest for ProtoBuf<T>
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||||
let limit = req.app_data::<ProtoBufConfig>().map(|c| c.limit).unwrap_or(262_144);
|
let limit = req
|
||||||
|
.app_data::<ProtoBufConfig>()
|
||||||
|
.map(|c| c.limit)
|
||||||
|
.unwrap_or(262_144);
|
||||||
Box::new(
|
Box::new(
|
||||||
ProtoBufMessage::new(req, payload)
|
ProtoBufMessage::new(req, payload)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.map_err(move |e| {
|
.map_err(move |e| e.into())
|
||||||
e.into()
|
.map(ProtoBuf),
|
||||||
})
|
)
|
||||||
.map(ProtoBuf))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +148,8 @@ impl<T: Message + Default> Responder for ProtoBuf<T> {
|
|||||||
|
|
||||||
fn respond_to(self, _: &HttpRequest) -> Self::Future {
|
fn respond_to(self, _: &HttpRequest) -> Self::Future {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
self.0.encode(&mut buf)
|
self.0
|
||||||
|
.encode(&mut buf)
|
||||||
.map_err(|e| Error::from(ProtoBufPayloadError::Serialize(e)))
|
.map_err(|e| Error::from(ProtoBufPayloadError::Serialize(e)))
|
||||||
.and_then(|()| {
|
.and_then(|()| {
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
@ -159,7 +168,6 @@ pub struct ProtoBufMessage<T: Message + Default>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Message + Default> ProtoBufMessage<T> {
|
impl<T: Message + Default> ProtoBufMessage<T> {
|
||||||
|
|
||||||
/// Create `ProtoBufMessage` for request.
|
/// Create `ProtoBufMessage` for request.
|
||||||
pub fn new(req: &HttpRequest, payload: &mut Payload) -> Self {
|
pub fn new(req: &HttpRequest, payload: &mut Payload) -> Self {
|
||||||
if req.content_type() != "application/protobuf" {
|
if req.content_type() != "application/protobuf" {
|
||||||
@ -197,8 +205,7 @@ impl<T: Message + Default> ProtoBufMessage<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Message + Default + 'static> Future for ProtoBufMessage<T>
|
impl<T: Message + Default + 'static> Future for ProtoBufMessage<T> {
|
||||||
{
|
|
||||||
type Item = T;
|
type Item = T;
|
||||||
type Error = ProtoBufPayloadError;
|
type Error = ProtoBufPayloadError;
|
||||||
|
|
||||||
@ -236,19 +243,18 @@ impl<T: Message + Default + 'static> Future for ProtoBufMessage<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub trait ProtoBufResponseBuilder {
|
pub trait ProtoBufResponseBuilder {
|
||||||
|
|
||||||
fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error>;
|
fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProtoBufResponseBuilder for HttpResponseBuilder {
|
impl ProtoBufResponseBuilder for HttpResponseBuilder {
|
||||||
|
|
||||||
fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error> {
|
fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error> {
|
||||||
self.header(CONTENT_TYPE, "application/protobuf");
|
self.header(CONTENT_TYPE, "application/protobuf");
|
||||||
|
|
||||||
let mut body = Vec::new();
|
let mut body = Vec::new();
|
||||||
value.encode(&mut body).map_err(ProtoBufPayloadError::Serialize)?;
|
value
|
||||||
|
.encode(&mut body)
|
||||||
|
.map_err(ProtoBufPayloadError::Serialize)?;
|
||||||
Ok(self.body(body))
|
Ok(self.body(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -256,8 +262,8 @@ impl ProtoBufResponseBuilder for HttpResponseBuilder {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use http::header;
|
|
||||||
use actix_web::test::{block_on, TestRequest};
|
use actix_web::test::{block_on, TestRequest};
|
||||||
|
use http::header;
|
||||||
|
|
||||||
impl PartialEq for ProtoBufPayloadError {
|
impl PartialEq for ProtoBufPayloadError {
|
||||||
fn eq(&self, other: &ProtoBufPayloadError) -> bool {
|
fn eq(&self, other: &ProtoBufPayloadError) -> bool {
|
||||||
@ -285,10 +291,16 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_protobuf() {
|
fn test_protobuf() {
|
||||||
let protobuf = ProtoBuf(MyObject{number: 9 , name: "test".to_owned()});
|
let protobuf = ProtoBuf(MyObject {
|
||||||
|
number: 9,
|
||||||
|
name: "test".to_owned(),
|
||||||
|
});
|
||||||
let req = TestRequest::default().to_http_request();
|
let req = TestRequest::default().to_http_request();
|
||||||
let resp = protobuf.respond_to(&req).unwrap();
|
let resp = protobuf.respond_to(&req).unwrap();
|
||||||
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "application/protobuf");
|
assert_eq!(
|
||||||
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
|
"application/protobuf"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -313,7 +325,8 @@ mod tests {
|
|||||||
header::CONTENT_LENGTH,
|
header::CONTENT_LENGTH,
|
||||||
header::HeaderValue::from_static("10000"),
|
header::HeaderValue::from_static("10000"),
|
||||||
).to_http_parts();
|
).to_http_parts();
|
||||||
let protobuf = block_on(ProtoBufMessage::<MyObject>::new(&req, &mut pl).limit(100));
|
let protobuf =
|
||||||
|
block_on(ProtoBufMessage::<MyObject>::new(&req, &mut pl).limit(100));
|
||||||
assert_eq!(protobuf.err().unwrap(), ProtoBufPayloadError::Overflow);
|
assert_eq!(protobuf.err().unwrap(), ProtoBufPayloadError::Overflow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user