Skip to content

Commit

Permalink
add cbor responder
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Jan 2, 2023
1 parent 3990314 commit 0aad1ea
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
1 change: 1 addition & 0 deletions actix-web-lab/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add `middleware::map_response()` for mapping responses with an async function.
- Add `middleware::map_response_body()` for mapping response bodies with an async function.
- Add `respond::{MessagePack,MessagePackNamed}` responders.
- Add `respond::Cbor` responder.

## 0.18.8 - 2022-12-10

Expand Down
21 changes: 15 additions & 6 deletions actix-web-lab/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ rustdoc-args = ["--cfg", "docsrs"]
all-features = true

[features]
default = ["derive", "spa"]
default = ["derive", "spa", "cbor"]
derive = ["actix-web-lab-derive"]
spa = ["actix-files"]

cbor = ["serde_cbor_2"]
msgpack = ["rmp-serde"]
spa = ["actix-files"]

[dependencies]
actix-web-lab-derive = { version = "=0.18.0", optional = true }
Expand Down Expand Up @@ -57,12 +59,15 @@ serde_html_form = "0.1"
tokio = { version = "1.13.1", features = ["sync", "macros"] }
tracing = { version = "0.1.30", features = ["log"] }

# spa
actix-files = { version = "0.6", optional = true }
# cbor
serde_cbor_2 = { version = "0.12.0-dev", optional = true }

# msgpack
rmp-serde = { version = "1", optional = true }

# spa
actix-files = { version = "0.6", optional = true }

[dev-dependencies]
actix-web-lab-derive = "=0.18.0"

Expand All @@ -87,9 +92,13 @@ time = { version = "0.3", features = ["formatting"] }
tokio = { version = "1.13.1", features = ["full"] }

[[example]]
name = "spa"
required-features = ["spa"]
name = "cbor"
required-features = ["cbor"]

[[example]]
name = "msgpack"
required-features = ["msgpack"]

[[example]]
name = "spa"
required-features = ["spa"]
34 changes: 34 additions & 0 deletions actix-web-lab/examples/cbor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::io;

use actix_web::{get, http::StatusCode, App, HttpServer, Responder};
use actix_web_lab::respond::Cbor;
use serde::Serialize;
use tracing::info;

#[derive(Debug, Serialize)]
struct Test {
one: u32,
two: String,
}

#[get("/")]
async fn index() -> impl Responder {
Cbor(Test {
one: 42,
two: "two".to_owned(),
})
}

#[actix_web::main]
async fn main() -> io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));

let bind = ("127.0.0.1", 8080);
info!("staring server at http://{}:{}", &bind.0, &bind.1);

HttpServer::new(|| App::new().service(index))
.workers(1)
.bind(bind)?
.run()
.await
}
28 changes: 28 additions & 0 deletions actix-web-lab/src/cbor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! CBOR responder.
use actix_web::{HttpRequest, HttpResponse, Responder};
use bytes::Bytes;
use derive_more::{Deref, DerefMut, Display};
use mime::Mime;
use once_cell::sync::Lazy;
use serde::Serialize;

static CBOR_MIME: Lazy<Mime> = Lazy::new(|| "application/cbor".parse().unwrap());

/// CBOR responder.
#[cfg_attr(docsrs, doc(cfg(feature = "cbor")))]
#[derive(Debug, Deref, DerefMut, Display)]
pub struct Cbor<T>(pub T);

impl<T: Serialize> Responder for Cbor<T> {
type Body = Bytes;

fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body> {
let body = Bytes::from(serde_cbor_2::to_vec(&self.0).unwrap());

HttpResponse::Ok()
.content_type(CBOR_MIME.clone())
.message_body(body)
.unwrap()
}
}
2 changes: 2 additions & 0 deletions actix-web-lab/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ mod body_limit;
mod bytes;
mod cache_control;
mod catch_panic;
#[cfg(feature = "cbor")]
mod cbor;
mod content_length;
mod csv;
mod display_stream;
Expand Down
3 changes: 3 additions & 0 deletions actix-web-lab/src/respond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
pub use crate::{csv::Csv, display_stream::DisplayStream, html::Html, ndjson::NdJson};

#[cfg(feature = "cbor")]
pub use crate::cbor::Cbor;

#[cfg(feature = "msgpack")]
pub use crate::msgpack::{MessagePack, MessagePackNamed};

0 comments on commit 0aad1ea

Please sign in to comment.