Skip to content

Commit 48f77ac

Browse files
committed
feat!:rework blanket impls, support IntoResponse types
1 parent 9efb9d2 commit 48f77ac

File tree

13 files changed

+482
-329
lines changed

13 files changed

+482
-329
lines changed

Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "axum-codec"
3-
version = "0.0.6"
3+
version = "0.0.7"
44
edition = "2021"
55
description = "A multi-codec extractor and response writer for Axum"
66
license = "MIT OR Apache-2.0"
@@ -13,7 +13,7 @@ members = ["macros", ".", "examples/aide-validator", "examples/basic"]
1313
[dependencies]
1414
aide = { version = "0.13", optional = true, default-features = false, features = ["axum"] }
1515
axum = { version = "0.7", default-features = false }
16-
axum-codec-macros = { path = "macros", version = "0.0.4", optional = true }
16+
axum-codec-macros = { path = "macros", version = "0.0.5" }
1717
bincode = { version = "2.0.0-rc.3", default-features = false, features = ["std"], optional = true }
1818
bitcode = { version = "0.6", default-features = false, optional = true }
1919
ciborium = { version = "0.2", optional = true }
@@ -40,27 +40,27 @@ default = ["json", "macros", "pretty-errors"]
4040

4141
# Enables all codecs
4242
full-codecs = ["bincode", "bitcode", "cbor", "json", "msgpack", "toml", "yaml"]
43-
macros = ["dep:axum-codec-macros", "schemars?/derive", "bincode?/derive", "bitcode?/derive", "serde?/derive", "validator?/derive"]
43+
macros = ["schemars?/derive", "bincode?/derive", "bitcode?/derive", "serde?/derive", "validator?/derive"]
4444

4545
# Enables support for {get,put,..}_with and relevant chaning methods
4646
# to add documentation to routes
47-
aide = ["dep:aide", "dep:schemars", "axum-codec-macros?/aide", "axum/json", "axum/form", "axum/original-uri", "axum/query", "axum/tokio", "axum/matched-path"]
47+
aide = ["dep:aide", "dep:schemars", "axum-codec-macros/aide", "axum/json", "axum/form", "axum/original-uri", "axum/query", "axum/tokio", "axum/matched-path"]
4848

4949
# Enables support for `validator`, adds an additional `validator::Validate` bound to `T` in `Codec<T>`
50-
validator = ["dep:validator", "axum-codec-macros?/validator"]
50+
validator = ["dep:validator", "axum-codec-macros/validator"]
5151

5252
# Enables more verbose (and expensive) error handling machinery, but significantly
5353
# improves the quality of error messages for consumers of the API.
5454
pretty-errors = ["macros"]
5555

56-
bincode = ["dep:bincode", "axum-codec-macros?/bincode"]
57-
bitcode = ["dep:bitcode", "axum-codec-macros?/bitcode"]
56+
bincode = ["dep:bincode", "axum-codec-macros/bincode"]
57+
bitcode = ["dep:bitcode", "axum-codec-macros/bitcode"]
5858
cbor = ["dep:ciborium", "serde"]
5959
json = ["dep:serde_json", "serde"]
6060
msgpack = ["dep:rmp-serde", "serde"]
6161
toml = ["dep:toml", "serde"]
6262
yaml = ["dep:serde_yaml", "serde"]
6363

6464
# Should not be manually enabled, but will not cause any issues if it is.
65-
serde = ["dep:serde", "axum-codec-macros?/serde"]
65+
serde = ["dep:serde", "axum-codec-macros/serde"]
6666

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ struct User {
5454
age: u8,
5555
}
5656

57-
async fn me() -> User {
58-
User {
57+
async fn me() -> Codec<User> {
58+
Codec(User {
5959
name: "Alice".into(),
6060
age: 42,
61-
}
61+
})
6262
}
6363

6464
/// A manual implementation of the handler above.
@@ -77,9 +77,9 @@ struct Greeting {
7777

7878
/// Specify `impl IntoCodecResponse`, similar to `axum`
7979
async fn greet(Codec(user): Codec<User>) -> impl IntoCodecResponse {
80-
Greeting {
80+
Codec(Greeting {
8181
message: format!("Hello, {}! You are {} years old.", user.name, user.age),
82-
}
82+
})
8383
}
8484

8585
#[tokio::main]

examples/aide-validator/src/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::sync::Arc;
33
use aide::axum::ApiRouter;
44
use axum::{extract::State, response::IntoResponse, Extension};
55
use axum_codec::{
6+
handler::IntoCodecResponse,
67
routing::{get, post},
78
Codec,
89
};
@@ -14,26 +15,26 @@ struct User {
1415
age: u8,
1516
}
1617

17-
async fn me() -> User {
18-
User {
18+
async fn me() -> impl IntoCodecResponse {
19+
Codec(User {
1920
name: "Alice".into(),
2021
age: 42,
21-
}
22+
})
2223
}
2324

2425
#[axum_codec::apply(encode)]
2526
struct Greeting {
2627
message: String,
2728
}
2829

29-
async fn greet(Codec(user): Codec<User>) -> Greeting {
30-
Greeting {
30+
async fn greet(Codec(user): Codec<User>) -> Codec<Greeting> {
31+
Codec(Greeting {
3132
message: format!("Hello, {}! You are {} years old.", user.name, user.age),
32-
}
33+
})
3334
}
3435

35-
async fn state(State(state): State<String>) -> Greeting {
36-
Greeting { message: state }
36+
async fn state(State(state): State<String>) -> Codec<Greeting> {
37+
Codec(Greeting { message: state })
3738
}
3839

3940
async fn openapi(Extension(api): Extension<Arc<aide::openapi::OpenApi>>) -> impl IntoResponse {

examples/basic/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ struct User {
1717
}
1818

1919
async fn me() -> impl IntoCodecResponse {
20-
User {
20+
Codec(User {
2121
name: "Alice".into(),
2222
age: 42,
23-
}
23+
})
2424
}
2525

2626
#[axum_codec::apply(encode)]
2727
struct Greeting {
2828
message: String,
2929
}
3030

31-
async fn greet(Codec(user): Codec<User>) -> Greeting {
32-
Greeting {
31+
async fn greet(Codec(user): Codec<User>) -> Codec<Greeting> {
32+
Codec(Greeting {
3333
message: format!("Hello, {}! You are {} years old.", user.name, user.age),
34-
}
34+
})
3535
}
3636

37-
async fn state(State(state): State<String>) -> Greeting {
38-
Greeting { message: state }
37+
async fn state(State(state): State<String>) -> Codec<Greeting> {
38+
Codec(Greeting { message: state })
3939
}
4040

4141
#[tokio::main]

macros/Cargo.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "axum-codec-macros"
3-
version = "0.0.4"
3+
version = "0.0.5"
44
edition = "2021"
55
description = "Procedural macros for axum-codec"
66
license = "MIT OR Apache-2.0"
@@ -11,13 +11,14 @@ repository = "https://github.com/matteopolak/axum-codec"
1111
proc-macro = true
1212

1313
[dependencies]
14+
proc-macro2 = "1"
1415
quote = "1"
15-
syn = "2"
16+
syn = { version = "2", optional = true }
1617

1718
[features]
18-
bincode = []
19-
bitcode = []
20-
serde = []
21-
aide = []
22-
validator = []
19+
bincode = ["dep:syn"]
20+
bitcode = ["dep:syn"]
21+
serde = ["dep:syn"]
22+
aide = ["dep:syn"]
23+
validator = ["dep:syn"]
2324

0 commit comments

Comments
 (0)