Skip to content

Commit de136e3

Browse files
committed
redefine SessionStore to take &mut Session and &str
1 parent a1a094e commit de136e3

File tree

5 files changed

+41
-35
lines changed

5 files changed

+41
-35
lines changed

src/cookie_store.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ pub enum CookieStoreError {
5151
impl SessionStore for CookieStore {
5252
type Error = CookieStoreError;
5353

54-
async fn load_session(&self, cookie_value: String) -> Result<Option<Session>, Self::Error> {
54+
async fn load_session(&self, cookie_value: &str) -> Result<Option<Session>, Self::Error> {
5555
let serialized = BASE64.decode(cookie_value)?;
5656
let session: Session = bincode_json::from_slice(&serialized)?;
5757
Ok(session.validate())
5858
}
5959

60-
async fn store_session(&self, session: Session) -> Result<Option<String>, Self::Error> {
61-
let serialized = bincode_json::to_vec(&session)?;
60+
async fn store_session(&self, session: &mut Session) -> Result<Option<String>, Self::Error> {
61+
let serialized = bincode_json::to_vec(session)?;
6262
Ok(Some(BASE64.encode(serialized)))
6363
}
6464

65-
async fn destroy_session(&self, _session: Session) -> Result<(), Self::Error> {
65+
async fn destroy_session(&self, _session: &mut Session) -> Result<(), Self::Error> {
6666
Ok(())
6767
}
6868

@@ -82,7 +82,7 @@ mod tests {
8282
let mut session = Session::new();
8383
session.insert("key", "Hello")?;
8484
let cloned = session.clone();
85-
let cookie_value = store.store_session(session).await?.unwrap();
85+
let cookie_value = store.store_session(&mut session).await?.unwrap();
8686
let loaded_session = store.load_session(cookie_value).await?.unwrap();
8787
assert_eq!(cloned.id(), loaded_session.id());
8888
assert_eq!("Hello", &loaded_session.get::<String>("key").unwrap());
@@ -97,14 +97,14 @@ mod tests {
9797
let mut session = Session::new();
9898

9999
session.insert("key", "value")?;
100-
let cookie_value = store.store_session(session).await?.unwrap();
100+
let cookie_value = store.store_session(&mut session).await?.unwrap();
101101

102102
let mut session = store.load_session(cookie_value.clone()).await?.unwrap();
103103
session.insert("key", "other value")?;
104104

105-
let new_cookie_value = store.store_session(session).await?.unwrap();
105+
let new_cookie_value = store.store_session(&mut session).await?.unwrap();
106106
let session = store.load_session(new_cookie_value).await?.unwrap();
107-
assert_eq!(&session.get::<String>("key").unwrap(), "other value");
107+
assert_eq!(&mut session.get::<String>("key").unwrap(), "other value");
108108

109109
Ok(())
110110
}
@@ -115,14 +115,14 @@ mod tests {
115115
let mut session = Session::new();
116116
session.expire_in(Duration::from_secs(1));
117117
let original_expires = *session.expiry().unwrap();
118-
let cookie_value = store.store_session(session).await?.unwrap();
118+
let cookie_value = store.store_session(&mut session).await?.unwrap();
119119

120120
let mut session = store.load_session(cookie_value.clone()).await?.unwrap();
121121

122122
assert_eq!(session.expiry().unwrap(), &original_expires);
123123
session.expire_in(Duration::from_secs(3));
124124
let new_expires = *session.expiry().unwrap();
125-
let cookie_value = store.store_session(session).await?.unwrap();
125+
let cookie_value = store.store_session(&mut session).await?.unwrap();
126126

127127
let session = store.load_session(cookie_value.clone()).await?.unwrap();
128128
assert_eq!(session.expiry().unwrap(), &new_expires);
@@ -141,7 +141,7 @@ mod tests {
141141
session.insert("key", "value")?;
142142
let cloned = session.clone();
143143

144-
let cookie_value = store.store_session(session).await?.unwrap();
144+
let cookie_value = store.store_session(&mut session).await?.unwrap();
145145

146146
let loaded_session = store.load_session(cookie_value.clone()).await?.unwrap();
147147
assert_eq!(cloned.id(), loaded_session.id());

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//! assert!(session.data_changed());
2323
//!
2424
//! // retrieve the cookie value to store in a session cookie
25-
//! let cookie_value = store.store_session(session).await?.unwrap();
25+
//! let cookie_value = store.store_session(&mut session).await?.unwrap();
2626
//!
2727
//! // Retrieve the session using the cookie.
2828
//! let session = store.load_session(cookie_value).await?.unwrap();
@@ -58,6 +58,6 @@ pub use cookie_store::{CookieStore, CookieStoreError};
5858
#[cfg(feature = "memory-store")]
5959
pub use memory_store::{MemoryStore, MemoryStoreError};
6060
pub use session::Session;
61-
pub use session_store::SessionStore;
61+
pub use session_store::{BoxedError, BoxedSessionStore, SessionStore};
6262

6363
pub use async_trait::async_trait;

src/memory_store.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ pub enum MemoryStoreError {
3939
impl SessionStore for MemoryStore {
4040
type Error = MemoryStoreError;
4141

42-
async fn load_session(&self, cookie_value: String) -> Result<Option<Session>, Self::Error> {
43-
let id = Session::id_from_cookie_value(&cookie_value)?;
42+
async fn load_session(&self, cookie_value: &str) -> Result<Option<Session>, Self::Error> {
43+
let id = Session::id_from_cookie_value(cookie_value)?;
4444
log::trace!("loading session by id `{}`", id);
4545
let Occupied(entry) = self.0.entry(id) else {
4646
return Ok(None);
@@ -54,14 +54,14 @@ impl SessionStore for MemoryStore {
5454
}
5555
}
5656

57-
async fn store_session(&self, mut session: Session) -> Result<Option<String>, Self::Error> {
57+
async fn store_session(&self, session: &mut Session) -> Result<Option<String>, Self::Error> {
5858
log::trace!("storing session by id `{}`", session.id());
5959
session.reset_data_changed();
6060
self.0.insert(session.id().to_string(), session.clone());
61-
Ok(session.into_cookie_value())
61+
Ok(session.take_cookie_value())
6262
}
6363

64-
async fn destroy_session(&self, session: Session) -> Result<(), Self::Error> {
64+
async fn destroy_session(&self, session: &mut Session) -> Result<(), Self::Error> {
6565
log::trace!("destroying session by id `{}`", session.id());
6666
self.0.remove(session.id());
6767
Ok(())
@@ -95,7 +95,7 @@ impl MemoryStore {
9595
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { async_std::task::block_on(async {
9696
/// let mut store = MemoryStore::new();
9797
/// assert_eq!(store.count(), 0);
98-
/// store.store_session(Session::new()).await?;
98+
/// store.store_session(&mut Session::new()).await?;
9999
/// assert_eq!(store.count(), 1);
100100
/// # Ok(()) }) }
101101
/// ```
@@ -115,7 +115,7 @@ mod tests {
115115
let mut session = Session::new();
116116
session.insert("key", "Hello")?;
117117
let cloned = session.clone();
118-
let cookie_value = store.store_session(session).await?.unwrap();
118+
let cookie_value = store.store_session(&mut session).await?.unwrap();
119119
let loaded_session = store.load_session(cookie_value).await?.unwrap();
120120
assert_eq!(cloned.id(), loaded_session.id());
121121
assert_eq!("Hello", &loaded_session.get::<String>("key").unwrap());
@@ -130,14 +130,14 @@ mod tests {
130130
let mut session = Session::new();
131131

132132
session.insert("key", "value")?;
133-
let cookie_value = store.store_session(session).await?.unwrap();
133+
let cookie_value = store.store_session(&mut session).await?.unwrap();
134134

135135
let mut session = store.load_session(cookie_value.clone()).await?.unwrap();
136136
session.insert("key", "other value")?;
137137

138-
assert_eq!(store.store_session(session).await?, None);
138+
assert_eq!(store.store_session(&mut session).await?, None);
139139
let session = store.load_session(cookie_value).await?.unwrap();
140-
assert_eq!(&session.get::<String>("key").unwrap(), "other value");
140+
assert_eq!(&mut session.get::<String>("key").unwrap(), "other value");
141141

142142
Ok(())
143143
}
@@ -148,14 +148,14 @@ mod tests {
148148
let mut session = Session::new();
149149
session.expire_in(Duration::from_secs(1));
150150
let original_expires = *session.expiry().unwrap();
151-
let cookie_value = store.store_session(session).await?.unwrap();
151+
let cookie_value = store.store_session(&mut session).await?.unwrap();
152152

153153
let mut session = store.load_session(cookie_value.clone()).await?.unwrap();
154154

155155
assert_eq!(session.expiry().unwrap(), &original_expires);
156156
session.expire_in(Duration::from_secs(3));
157157
let new_expires = *session.expiry().unwrap();
158-
assert_eq!(None, store.store_session(session).await?);
158+
assert_eq!(None, store.store_session(&mut session).await?);
159159

160160
let session = store.load_session(cookie_value.clone()).await?.unwrap();
161161
assert_eq!(session.expiry().unwrap(), &new_expires);
@@ -174,7 +174,7 @@ mod tests {
174174
session.insert("key", "value")?;
175175
let cloned = session.clone();
176176

177-
let cookie_value = store.store_session(session).await?.unwrap();
177+
let cookie_value = store.store_session(&mut session).await?.unwrap();
178178

179179
let loaded_session = store.load_session(cookie_value.clone()).await?.unwrap();
180180
assert_eq!(cloned.id(), loaded_session.id());
@@ -192,26 +192,26 @@ mod tests {
192192
async fn destroying_a_single_session() -> Result<(), MemoryStoreError> {
193193
let store = MemoryStore::new();
194194
for _ in 0..3i8 {
195-
store.store_session(Session::new()).await?;
195+
store.store_session(&mut Session::new()).await?;
196196
}
197197

198-
let cookie = store.store_session(Session::new()).await?.unwrap();
198+
let cookie = store.store_session(&mut Session::new()).await?.unwrap();
199199
assert_eq!(4, store.count());
200-
let session = store.load_session(cookie.clone()).await?.unwrap();
201-
store.destroy_session(session.clone()).await?;
200+
let mut session = store.load_session(cookie.clone()).await?.unwrap();
201+
store.destroy_session(&mut session).await?;
202202
assert_eq!(None, store.load_session(cookie).await?);
203203
assert_eq!(3, store.count());
204204

205205
// attempting to destroy the session again is not an error
206-
assert!(store.destroy_session(session).await.is_ok());
206+
assert!(store.destroy_session(&mut session).await.is_ok());
207207
Ok(())
208208
}
209209

210210
#[async_std::test]
211211
async fn clearing_the_whole_store() -> Result<(), MemoryStoreError> {
212212
let store = MemoryStore::new();
213213
for _ in 0..3i8 {
214-
store.store_session(Session::new()).await?;
214+
store.store_session(&mut Session::new()).await?;
215215
}
216216

217217
assert_eq!(3, store.count());

src/session.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,12 @@ impl Session {
581581
/// # Ok(()) }) }
582582
/// ```
583583
pub fn into_cookie_value(mut self) -> Option<String> {
584+
self.take_cookie_value()
585+
}
586+
587+
/// take the cookie value. this is generally only performed by a
588+
/// session store.
589+
pub fn take_cookie_value(&mut self) -> Option<String> {
584590
self.cookie_value.take()
585591
}
586592
}

src/session_store.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ pub trait SessionStore {
1111
/// The input is expected to be the value of an identifying
1212
/// cookie. This will then be parsed by the session middleware
1313
/// into a session if possible
14-
async fn load_session(&self, cookie_value: String) -> Result<Option<Session>, Self::Error>;
14+
async fn load_session(&self, cookie_value: &str) -> Result<Option<Session>, Self::Error>;
1515

1616
/// Store a session on the storage backend.
1717
///
1818
/// The return value is the value of the cookie to store for the
1919
/// user that represents this session
20-
async fn store_session(&self, session: Session) -> Result<Option<String>, Self::Error>;
20+
async fn store_session(&self, session: &mut Session) -> Result<Option<String>, Self::Error>;
2121

2222
/// Remove a session from the session store
23-
async fn destroy_session(&self, session: Session) -> Result<(), Self::Error>;
23+
async fn destroy_session(&self, session: &mut Session) -> Result<(), Self::Error>;
2424

2525
/// Empties the entire store, destroying all sessions
2626
async fn clear_store(&self) -> Result<(), Self::Error>;

0 commit comments

Comments
 (0)