Skip to content

Commit

Permalink
Replace chrono with time crate
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Okushi <jtitor@2k36.org>
  • Loading branch information
JohnTitor committed Jun 21, 2022
1 parent 5f7f472 commit 1d1e718
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Expand Up @@ -31,10 +31,9 @@ log = "0.4.14"
version = "1.0.126"
features = ["rc", "derive"]

[dependencies.chrono]
version = "0.4.19"
default-features = false
features = ["clock", "serde", "std"]
[dependencies.time]
version = "0.3.10"
features = ["serde"]

[dev-dependencies.async-std]
version = "1.9.0"
Expand Down
2 changes: 1 addition & 1 deletion src/cookie_store.rs
Expand Up @@ -33,7 +33,7 @@ impl CookieStore {
impl SessionStore for CookieStore {
async fn load_session(&self, cookie_value: String) -> Result<Option<Session>> {
let serialized = base64::decode(&cookie_value)?;
let session: Session = bincode::deserialize(&serialized)?;
let session: Session = bincode::deserialize(&serialized[..])?;
Ok(session.validate())
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -63,9 +63,9 @@ pub use session_store::SessionStore;
pub use async_trait::async_trait;
pub use base64;
pub use blake3;
pub use chrono;
pub use hmac;
pub use log;
pub use serde;
pub use serde_json;
pub use sha2;
pub use time;
22 changes: 14 additions & 8 deletions src/session.rs
@@ -1,13 +1,14 @@
use chrono::{DateTime, Duration, Utc};
use rand::RngCore;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
convert::TryFrom,
sync::{
atomic::{AtomicBool, Ordering},
Arc, RwLock,
},
};
use time::OffsetDateTime as DateTime;

/// # The main session type.
///
Expand Down Expand Up @@ -56,7 +57,7 @@ use std::{
#[derive(Debug, Serialize, Deserialize)]
pub struct Session {
id: String,
expiry: Option<DateTime<Utc>>,
expiry: Option<DateTime>,
data: Arc<RwLock<HashMap<String, String>>>,

#[serde(skip)]
Expand Down Expand Up @@ -355,7 +356,7 @@ impl Session {
/// assert!(session.expiry().is_some());
/// # Ok(()) }) }
/// ```
pub fn expiry(&self) -> Option<&DateTime<Utc>> {
pub fn expiry(&self) -> Option<&DateTime> {
self.expiry.as_ref()
}

Expand All @@ -368,11 +369,11 @@ impl Session {
/// # fn main() -> async_session::Result { async_std::task::block_on(async {
/// let mut session = Session::new();
/// assert_eq!(None, session.expiry());
/// session.set_expiry(chrono::Utc::now());
/// session.set_expiry(time::OffsetDateTime::now_utc());
/// assert!(session.expiry().is_some());
/// # Ok(()) }) }
/// ```
pub fn set_expiry(&mut self, expiry: DateTime<Utc>) {
pub fn set_expiry(&mut self, expiry: DateTime) {
self.expiry = Some(expiry);
}

Expand All @@ -390,7 +391,7 @@ impl Session {
/// # Ok(()) }) }
/// ```
pub fn expire_in(&mut self, ttl: std::time::Duration) {
self.expiry = Some(Utc::now() + Duration::from_std(ttl).unwrap());
self.expiry = Some(DateTime::now_utc() + ttl);
}

/// predicate function to determine if this session is
Expand All @@ -415,7 +416,7 @@ impl Session {
/// ```
pub fn is_expired(&self) -> bool {
match self.expiry {
Some(expiry) => expiry < Utc::now(),
Some(expiry) => expiry < DateTime::now_utc(),
None => false,
}
}
Expand Down Expand Up @@ -509,7 +510,12 @@ impl Session {
/// ```
/// Duration from now to the expiry time of this session
pub fn expires_in(&self) -> Option<std::time::Duration> {
self.expiry?.signed_duration_since(Utc::now()).to_std().ok()
let dur = self.expiry? - DateTime::now_utc();
if dur.is_negative() {
None
} else {
std::time::Duration::try_from(dur).ok()
}
}

/// takes the cookie value and consume this session.
Expand Down

0 comments on commit 1d1e718

Please sign in to comment.