From c5d5c293a224b55eb103180e4cb359a56a2e3e70 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Thu, 12 Sep 2024 14:17:29 -0700 Subject: [PATCH] feat: use time crate to parse ISO8601 datetimes from API --- Cargo.lock | 54 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/libcanvas/courses.rs | 13 ++++++---- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7962e13..a4d4cd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,6 +74,7 @@ version = "0.1.0" dependencies = [ "reqwest", "serde", + "time", "tokio", ] @@ -108,6 +109,16 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", + "serde", +] + [[package]] name = "encoding_rs" version = "0.8.34" @@ -488,6 +499,12 @@ dependencies = [ "tempfile", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "object" version = "0.36.4" @@ -614,6 +631,12 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "proc-macro2" version = "1.0.86" @@ -952,6 +975,37 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "time" +version = "0.3.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tinyvec" version = "1.8.0" diff --git a/Cargo.toml b/Cargo.toml index 5a8188b..c67a5d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" reqwest = { version = "0.12.7", features = ["json"] } tokio = { version = "1.15", features = ["full"] } serde = { version = "1.0.210", features = ["derive"] } +time = { version = "0.3.36", features = ["serde", "parsing", "formatting"] } diff --git a/src/libcanvas/courses.rs b/src/libcanvas/courses.rs index 7a80700..098b065 100644 --- a/src/libcanvas/courses.rs +++ b/src/libcanvas/courses.rs @@ -1,5 +1,7 @@ use super::CanvasClient; use serde::{Deserialize, Serialize}; +use time::serde::iso8601; +use time::OffsetDateTime; impl CanvasClient { pub async fn get_courses(&self) -> Result, reqwest::Error> { @@ -13,8 +15,6 @@ impl CanvasClient { } } -// Some time options are ISO 8601 standard times but they are parsed as Strings for now for -// simplicity /// Represents a response from the `/courses` API endpoint. Some strings are plaintext and some are /// HTML. Some JSON objects which have not yet been typed are deserialized into plaintext instead. #[derive(Serialize, Deserialize, Debug)] @@ -23,10 +23,12 @@ pub struct Course { pub name: String, pub account_id: u32, pub uuid: String, - pub start_at: Option, + #[serde(with = "iso8601::option")] + pub start_at: Option, pub grading_standard_id: Option, pub is_public: bool, - pub created_at: String, + #[serde(with = "iso8601")] + pub created_at: OffsetDateTime, pub course_code: String, pub default_view: Option, pub root_account_id: u32, @@ -42,7 +44,8 @@ pub struct Course { pub syllabus_body: Option, pub needs_grading_count: Option, pub grade_passback_setting: Option, - pub end_at: Option, + #[serde(with = "iso8601::option")] + pub end_at: Option, pub public_syllabus: bool, pub public_syllabus_to_auth: bool, pub storage_quota_mb: usize,