Skip to content

Commit 117a117

Browse files
committed
rework crate into a middleware
This changes the structure of the crate such that it is now a middleware in addition to being an extractor. Doing so allows us to improve the ergonomics of the API such that calling `save` and awaiting a future is no longer needed. Now applications will need to install the `MeessagesManagerLayer` after `tower-sessions` has been installed (either directly or via a middleware that wraps it). Also note that the iterator impplementation has been updated to use `Message` directly. Fields of `Message` have been made public as well.
1 parent 710053b commit 117a117

File tree

5 files changed

+172
-76
lines changed

5 files changed

+172
-76
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Unreleased
22

3+
# 0.2.0
4+
5+
**Breaking Changes**
6+
7+
- Rework crate into a middleware
8+
9+
This changes the structure of the crate such that it is now a middleware in addition to being an extractor. Doing so allows us to improve the ergonomics of the API such that calling `save` and awaiting a future is no longer needed.
10+
11+
Now applications will need to install the `MeessagesManagerLayer` after `tower-sessions` has been installed (either directly or via a middleware that wraps it).
12+
13+
Also note that the iterator impplementation has been updated to use `Message` directly. Fields of `Message` have been made public as well.
14+
315
# 0.1.0
416

517
- Initial release :tada:

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
22
name = "axum-messages"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55
authors = ["Max Countryman <[email protected]>"]
66
categories = ["asynchronous", "network-programming", "web-programming"]
77
description = "🛎️ One-time notification messages for Axum."
88
homepage = "https://github.com/maxcountryman/axum-messages"
9-
keywords = ["axum", "flash", "message", "messages"]
9+
keywords = ["axum", "flash", "message", "messages", "notification"]
1010
license = "MIT"
1111
readme = "README.md"
1212
repository = "https://github.com/maxcountryman/axum-messages"
@@ -15,7 +15,9 @@ repository = "https://github.com/maxcountryman/axum-messages"
1515
async-trait = "0.1.77"
1616
axum-core = "0.4.3"
1717
http = "1.0.0"
18+
parking_lot = "0.12.1"
1819
serde = { version = "1.0.195", features = ["derive"] }
20+
tower = "0.4"
1921
tower-sessions-core = "0.9.1"
2022

2123
[dev-dependencies]

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To use the crate in your project, add the following to your `Cargo.toml` file:
3434

3535
```toml
3636
[dependencies]
37-
axum-messages = "0.1.0"
37+
axum-messages = "0.2.0"
3838
```
3939

4040
## 🤸 Usage
@@ -49,7 +49,7 @@ use axum::{
4949
routing::get,
5050
Router,
5151
};
52-
use axum_messages::Messages;
52+
use axum_messages::{Messages, MessagesManagerLayer};
5353
use time::Duration;
5454
use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer};
5555

@@ -63,6 +63,7 @@ async fn main() {
6363
let app = Router::new()
6464
.route("/", get(set_messages_handler))
6565
.route("/read-messages", get(read_messages_handler))
66+
.layer(MessagesManagerLayer)
6667
.layer(session_layer);
6768

6869
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
@@ -75,7 +76,7 @@ async fn main() {
7576
async fn read_messages_handler(messages: Messages) -> impl IntoResponse {
7677
let messages = messages
7778
.into_iter()
78-
.map(|(level, message)| format!("{:?}: {}", level, message))
79+
.map(|message| format!("{:?}: {}", message.level, message))
7980
.collect::<Vec<_>>()
8081
.join(", ");
8182

@@ -89,10 +90,7 @@ async fn read_messages_handler(messages: Messages) -> impl IntoResponse {
8990
async fn set_messages_handler(messages: Messages) -> impl IntoResponse {
9091
messages
9192
.info("Hello, world!")
92-
.debug("This is a debug message.")
93-
.save()
94-
.await
95-
.unwrap();
93+
.debug("This is a debug message.");
9694

9795
Redirect::to("/read-messages")
9896
}

examples/basic.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use axum::{
55
routing::get,
66
Router,
77
};
8-
use axum_messages::Messages;
8+
use axum_messages::{Messages, MessagesManagerLayer};
99
use time::Duration;
1010
use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer};
1111

@@ -19,6 +19,7 @@ async fn main() {
1919
let app = Router::new()
2020
.route("/", get(set_messages_handler))
2121
.route("/read-messages", get(read_messages_handler))
22+
.layer(MessagesManagerLayer)
2223
.layer(session_layer);
2324

2425
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
@@ -31,7 +32,7 @@ async fn main() {
3132
async fn read_messages_handler(messages: Messages) -> impl IntoResponse {
3233
let messages = messages
3334
.into_iter()
34-
.map(|(level, message)| format!("{:?}: {}", level, message))
35+
.map(|message| format!("{:?}: {}", message.level, message))
3536
.collect::<Vec<_>>()
3637
.join(", ");
3738

@@ -45,10 +46,7 @@ async fn read_messages_handler(messages: Messages) -> impl IntoResponse {
4546
async fn set_messages_handler(messages: Messages) -> impl IntoResponse {
4647
messages
4748
.info("Hello, world!")
48-
.debug("This is a debug message.")
49-
.save()
50-
.await
51-
.unwrap();
49+
.debug("This is a debug message.");
5250

5351
Redirect::to("/read-messages")
5452
}

0 commit comments

Comments
 (0)