Skip to content

Commit

Permalink
show folks we can use const to "strongly type" keys
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcountryman committed Sep 22, 2023
1 parent 6c830f6 commit 73ad3da
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
6 changes: 4 additions & 2 deletions examples/counter-extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use serde::{Deserialize, Serialize};
use tower::ServiceBuilder;
use tower_sessions::{time::Duration, MemoryStore, Session, SessionManagerLayer};

const COUNTER_KEY: &str = "counter";

#[derive(Default, Deserialize, Serialize)]
struct Counter(usize);

Expand All @@ -24,12 +26,12 @@ where
let session = Session::from_request_parts(req, state).await?;

let counter: Counter = session
.get("counter")
.get(COUNTER_KEY)
.expect("Could not deserialize.")
.unwrap_or_default();

session
.insert("counter", counter.0 + 1)
.insert(COUNTER_KEY, counter.0 + 1)
.expect("Could not serialize.");

Ok(counter)
Expand Down
6 changes: 4 additions & 2 deletions examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize};
use tower::ServiceBuilder;
use tower_sessions::{time::Duration, MemoryStore, Session, SessionManagerLayer};

const COUNTER_KEY: &str = "counter";

#[derive(Default, Deserialize, Serialize)]
struct Counter(usize);

Expand Down Expand Up @@ -37,12 +39,12 @@ async fn main() {

async fn handler(session: Session) -> impl IntoResponse {
let counter: Counter = session
.get("counter")
.get(COUNTER_KEY)
.expect("Could not deserialize.")
.unwrap_or_default();

session
.insert("counter", counter.0 + 1)
.insert(COUNTER_KEY, counter.0 + 1)
.expect("Could not serialize.");

format!("Current count: {}", counter.0)
Expand Down
6 changes: 4 additions & 2 deletions examples/postgres-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize};
use tower::ServiceBuilder;
use tower_sessions::{sqlx::PgPool, time::Duration, PostgresStore, Session, SessionManagerLayer};

const COUNTER_KEY: &str = "counter";

#[derive(Serialize, Deserialize, Default)]
struct Counter(usize);

Expand Down Expand Up @@ -50,12 +52,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

async fn handler(session: Session) -> impl IntoResponse {
let counter: Counter = session
.get("counter")
.get(COUNTER_KEY)
.expect("Could not deserialize.")
.unwrap_or_default();

session
.insert("counter", counter.0 + 1)
.insert(COUNTER_KEY, counter.0 + 1)
.expect("Could not serialize.");

format!("Current count: {}", counter.0)
Expand Down
6 changes: 4 additions & 2 deletions examples/redis-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize};
use tower::ServiceBuilder;
use tower_sessions::{fred::prelude::*, time::Duration, RedisStore, Session, SessionManagerLayer};

const COUNTER_KEY: &str = "counter";

#[derive(Serialize, Deserialize, Default)]
struct Counter(usize);

Expand Down Expand Up @@ -43,12 +45,12 @@ async fn main() {

async fn handler(session: Session) -> impl IntoResponse {
let counter: Counter = session
.get("counter")
.get(COUNTER_KEY)
.expect("Could not deserialize.")
.unwrap_or_default();

session
.insert("counter", counter.0 + 1)
.insert(COUNTER_KEY, counter.0 + 1)
.expect("Could not serialize.");

format!("Current count: {}", counter.0)
Expand Down
6 changes: 4 additions & 2 deletions examples/sqlite-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize};
use tower::ServiceBuilder;
use tower_sessions::{sqlx::SqlitePool, time::Duration, Session, SessionManagerLayer, SqliteStore};

const COUNTER_KEY: &str = "counter";

#[derive(Serialize, Deserialize, Default)]
struct Counter(usize);

Expand Down Expand Up @@ -49,12 +51,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

async fn handler(session: Session) -> impl IntoResponse {
let counter: Counter = session
.get("counter")
.get(COUNTER_KEY)
.expect("Could not deserialize.")
.unwrap_or_default();

session
.insert("counter", counter.0 + 1)
.insert(COUNTER_KEY, counter.0 + 1)
.expect("Could not serialize.");

format!("Current count: {}", counter.0)
Expand Down

0 comments on commit 73ad3da

Please sign in to comment.