From a869c255389db88cf003063d14af86bea2c40bfc Mon Sep 17 00:00:00 2001 From: cduvray Date: Mon, 10 Jul 2023 08:57:22 +0200 Subject: [PATCH] refactor: NumericDate.inner replaced by From - "inner" should be inner and not public, I think From trait implementation looks nicer - tests added --- jwt-authorizer/src/claims.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/jwt-authorizer/src/claims.rs b/jwt-authorizer/src/claims.rs index fe7605c..253f2b0 100644 --- a/jwt-authorizer/src/claims.rs +++ b/jwt-authorizer/src/claims.rs @@ -1,13 +1,14 @@ use serde::Deserialize; -/// Seconds since the epoch +/// The number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time ignoring leap seconds. +/// (https://www.rfc-editor.org/rfc/rfc7519#section-2) #[derive(Deserialize, Clone, PartialEq, Eq, Debug)] pub struct NumericDate(i64); -impl NumericDate { - /// Get the underlying unix timestamp - pub fn inner(&self) -> i64 { - self.0 +/// accesses the underlying value +impl From for i64 { + fn from(t: NumericDate) -> Self { + t.0 } } @@ -106,11 +107,27 @@ mod tests { #[test] fn from_numeric_date() { + let exp: i64 = NumericDate(1516239022).into(); + assert_eq!(exp, 1516239022); + } + + #[test] + fn chrono_from_numeric_date() { let exp: DateTime = NumericDate(1516239022).into(); assert_eq!(exp, Utc.timestamp_opt(1516239022, 0).unwrap()); assert_eq!(exp, DateTime::parse_from_rfc3339("2018-01-18T01:30:22.000Z").unwrap()); } + #[cfg(feature = "time")] + #[test] + fn time_from_numeric_date() { + use time::macros::datetime; + use time::OffsetDateTime; + + let exp: OffsetDateTime = NumericDate(1516239022).into(); + assert_eq!(exp, datetime!(2018-01-18 01:30:22 UTC)); + } + #[test] fn rfc_claims() { let jwt_json = json!({