Skip to content

Commit

Permalink
Added impl From<VCDateTime> for chrono::DateTime<Tz>.
Browse files Browse the repository at this point in the history
Previously there was no such conversion, and because the timestamp
attribute of VCDateTime was private, there was no reasonable way to
convert VCDateTime back into chrono::DateTime<Tz> outside the crate.
This makes it so that conversions both ways exist.
  • Loading branch information
vdods committed Mar 29, 2022
1 parent f4fc478 commit 5f7d1a7
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/vc.rs
Expand Up @@ -729,6 +729,15 @@ where
}
}

impl<Tz: chrono::TimeZone> From<VCDateTime> for DateTime<Tz>
where
chrono::DateTime<Tz>: From<chrono::DateTime<chrono::FixedOffset>>
{
fn from(vc_date_time: VCDateTime) -> Self {
Self::from(vc_date_time.date_time)
}
}

pub fn base64_encode_json<T: Serialize>(object: &T) -> Result<String, Error> {
let json = serde_json::to_string(&object)?;
Ok(base64::encode_config(json, base64::URL_SAFE_NO_PAD))
Expand Down Expand Up @@ -2467,6 +2476,14 @@ pub(crate) mod tests {
println!("{}", serde_json::to_string_pretty(&doc).unwrap());
}

#[test]
fn test_vc_date_time_roundtrip() {
let expected_utc_now = chrono::Utc::now();
let vc_date_time_now = VCDateTime::from(expected_utc_now);
let roundtripped_utc_now = chrono::DateTime::<chrono::Utc>::from(vc_date_time_now);
assert_eq!(roundtripped_utc_now, expected_utc_now);
}

#[async_std::test]
async fn generate_jwt() {
let vc_str = r###"{
Expand Down

0 comments on commit 5f7d1a7

Please sign in to comment.