Skip to content

Commit

Permalink
refactor: extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
gengteng committed Dec 14, 2023
1 parent a59c65a commit ac177c7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "axum-serde"
version = "0.1.3"
version = "0.2.0"
description = "Provides multiple serde-based extractors / responses for the Axum web framework, also offers a macro to easily customize extractors / responses."
authors = ["GengTeng <me@gteng.org>"]
license = "MIT"
Expand Down
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,20 @@ Use the `extractor` macro to create custom extractors with minimal boilerplate:

* Example

```rust,ignore
```rust
use axum_serde::{
extractor,
macros::{DeserializeOwned, Serialize},
};

extractor!(
MyFormat, // The name of the data format.
MyFmt, // The actual type name of the HTTP extractor/response.
application, // The main type of the Content-Type that this extractor supports.
myfmt, // The subtype of the Content-Type that this extractor supports.
from_slice, // A function identifier for deserializing data from the HTTP request body.
String, // The type of error that can occur when deserializing from the request body.
to_vec, // A function identifier for serializing the HTTP response body to bytes.
myfmt // The test module name.
MyFormat, // The name of the data format.
MyFmt, // The actual type name of the HTTP extractor/response.
"application/myfmt", // The Content-Type that this extractor supports.
from_slice, // A function identifier for deserializing data from the HTTP request body.
String, // The type of error that can occur when deserializing from the request body.
to_vec, // A function identifier for serializing the HTTP response body to bytes.
myfmt // The test module name.
);

fn from_slice<T: DeserializeOwned>(_bytes: &[u8]) -> Result<T, String> {
Expand Down
14 changes: 6 additions & 8 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ pub use serde::{de::DeserializeOwned, Deserialize, Serialize};
///
/// * `$name` - The name of the data format.
/// * `$ext` - The actual type name of the HTTP extractor/response.
/// * `$type_` - The main type of the Content-Type that this extractor supports.
/// * `$subtype` - The subtype of the Content-Type that this extractor supports.
/// * `$content_type` - The Content-Type that this extractor supports.
/// * `$de` - A function identifier for deserializing data from the HTTP request body.
/// * `$de_err` - The type of error that can occur when deserializing from the request body.
/// * `$ser` - A function identifier for serializing the HTTP response body to bytes.
Expand All @@ -31,8 +30,7 @@ macro_rules! extractor {
(
$name:tt,
$ext:tt,
$type_:tt,
$subtype:tt,
$content_type: tt,
$de:ident,
$de_err:ident,
$ser:ident,
Expand All @@ -45,7 +43,7 @@ macro_rules! extractor {
#[doc = "implements [`serde::Deserialize`]. The request will be rejected (and a [`crate::Rejection`] will"]
#[doc = "be returned) if:"]
#[doc = "- The request doesn't have a `Content-Type:"]
#[doc = concat!(stringify!($type_), "/", stringify!($subtype))]
#[doc = $content_type]
#[doc = "` (or similar) header."]
#[doc = "- The body doesn't contain syntactically valid "]
#[doc = stringify!($name)]
Expand Down Expand Up @@ -89,7 +87,7 @@ macro_rules! extractor {
#[doc = " `"]
#[doc = stringify!($ext)]
#[doc = "`, and will automatically set `Content-Type:"]
#[doc = concat!(stringify!($type_), "/", stringify!($subtype))]
#[doc = $content_type]
#[doc = "` header."]
#[doc = ""]
#[doc = " # Response example"]
Expand Down Expand Up @@ -160,7 +158,7 @@ macro_rules! extractor {
#[doc = "Content type of "]
#[doc = stringify!($name)]
#[doc = " format."]
pub const CONTENT_TYPE: &'static str = concat!(stringify!($type_), "/", stringify!($subtype));
pub const CONTENT_TYPE: &'static str = $content_type;

#[doc = concat!("Construct a `", stringify!($ext), "<T>` from a byte slice.")]
#[doc = concat!("Most users should prefer to use the FromRequest impl but special cases may require first extracting a Request into Bytes then optionally constructing a `", stringify!($ext), "<T>`.")]
Expand Down Expand Up @@ -240,7 +238,7 @@ macro_rules! extractor {
}

const TEST_ROUTE: &'static str = "/value";
const EXT_CONTENT_TYPE: &'static str = concat!(stringify!($type_), "/", stringify!($subtype));
const EXT_CONTENT_TYPE: &'static str = $content_type;

#[tokio::test]
async fn extractor() {
Expand Down
6 changes: 2 additions & 4 deletions src/msgpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use rmp_serde::{from_slice, to_vec, to_vec_named};
extractor!(
MessagePack,
MsgPack,
application,
msgpack,
"application/msgpack",
from_slice,
Error,
to_vec_named,
Expand All @@ -18,8 +17,7 @@ extractor!(
extractor!(
MessagePack,
MsgPackRaw,
application,
msgpack,
"application/msgpack",
from_slice,
Error,
to_vec,
Expand Down
3 changes: 1 addition & 2 deletions src/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ fn to_vec<T: Serialize>(value: &T) -> Result<Vec<u8>, toml_::ser::Error> {
extractor!(
TOML,
Toml,
application,
toml,
"application/toml",
from_slice,
Error,
to_vec,
Expand Down
3 changes: 1 addition & 2 deletions src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ fn to_vec<T: Serialize>(value: &T) -> Result<Vec<u8>, DeError> {
extractor!(
XML,
Xml,
application,
xml,
"application/xml",
from_slice,
DeError,
to_vec,
Expand Down
3 changes: 1 addition & 2 deletions src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ fn to_vec<T: Serialize>(value: &T) -> Result<Vec<u8>, Error> {
extractor!(
YAML,
Yaml,
application,
yaml,
"application/yaml",
from_slice,
Error,
to_vec,
Expand Down

0 comments on commit ac177c7

Please sign in to comment.