Skip to content

Commit

Permalink
feat: add CBOR support
Browse files Browse the repository at this point in the history
  • Loading branch information
gengteng committed Apr 14, 2024
1 parent 0812074 commit 6fcb85f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
23 changes: 13 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "axum-serde"
version = "0.3.0"
version = "0.4.0"
description = "Provides multiple serde-based extractors / responses for the Axum web framework, also offers a macro to easily customize extractors / responses."
authors = ["GengTeng <[email protected]>"]
license = "MIT"
Expand All @@ -24,29 +24,32 @@ edition = "2021"
features = ["full"]

[dependencies]
axum = "0.7.1"
serde = "1.0.193"
axum = "0.7.5"
serde = "1.0.197"
mime = "0.3.17"
serde_yaml = { version = "0.9.27", optional = true }
rmp-serde = { version = "1.1.2", optional = true }
toml_ = { package = "toml", version = "0.8.8", optional = true }
quick-xml = { version = "0.31.0", optional = true, features = ["serialize"] }
sonic-rs = { version = "0.3.3", optional = true }
axum-core = "0.4.0"
async-trait = "0.1.74"
thiserror = "1.0.50"
bytes = "1.5.0"
ciborium = { version = "0.2.2", optional = true }
axum-core = "0.4.3"
async-trait = "0.1.79"
thiserror = "1.0.58"
bytes = "1.6.0"

[dev-dependencies]
axum-test = "14.0.0"
serde = { version = "1.0.193", features = ["derive"] }
tokio = { version = "1.34.0", features = ["macros"] }
serde = { version = "1.0.197", features = ["derive"] }
tokio = { version = "1.37.0", features = ["macros"] }

[features]
yaml = ["dep:serde_yaml"]
msgpack = ["dep:rmp-serde"]
toml = ["dep:toml_"]
xml = ["dep:quick-xml"]
xml_encoding = ["xml", "quick-xml/encoding"]
full = ["yaml", "msgpack", "toml", "xml", "sonic"]
sonic = ["dep:sonic-rs"]
cbor = ["dep:ciborium"]
full = ["yaml", "msgpack", "toml", "xml", "sonic", "cbor"]

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ curl -X POST http://localhost:8080/j2y -H "Content-Type: application/json" -d '{
| `Toml<T>` | toml | [toml](https://crates.io/crates/toml) v0.8.8 |
| `Xml<T>` | xml | [quick-xml](https://crates.io/crates/quick-xml) v0.31.0 |
| `Sonic<T>` | sonic | [sonic-rs](https://crates.io/crates/sonic-rs) v0.3.3 |
| `Cbor<T>` | cbor | [ciborium](https://crates.io/crates/ciborium) v0.2.2 |

## 🎁 Custom extractor / response

Expand Down
27 changes: 27 additions & 0 deletions src/cbor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! # CBOR
use crate::extractor;
use serde::de::DeserializeOwned;
use serde::Serialize;

type CborDeError = ciborium::de::Error<std::io::Error>;

extractor!(
CBOR,
Cbor,
"application/cbor",
from_slice,
CborDeError,
to_vec,
cbor_test
);

fn from_slice<T: DeserializeOwned>(s: &[u8]) -> Result<T, CborDeError> {
ciborium::de::from_reader(s)
}

fn to_vec<T: Serialize>(value: &T) -> Result<Vec<u8>, ciborium::ser::Error<std::io::Error>> {
let mut vec = Vec::with_capacity(128);
ciborium::into_writer(value, &mut vec)?;
Ok(vec)
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub mod xml;
#[cfg(feature = "yaml")]
pub mod yaml;

#[cfg(feature = "cbor")]
pub mod cbor;

use axum::http::{header, HeaderMap};
use mime::Mime;

Expand Down

0 comments on commit 6fcb85f

Please sign in to comment.