Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chrono::NaiveDate support in AutoJsJson #306

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

* `Driver::utc_now` (Gets current UTC timestamp)
* `Driver::timezone_offset` (Gets browsers time zone offset in seconds)
* `chrono::NaiveDate` support in `AutoJsJson`

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<'a> GraphEdgeIter<'a> {
}
}

impl<'a> Iterator for GraphEdgeIter<'a> {
impl Iterator for GraphEdgeIter<'_> {
type Item = GraphId;

fn next(&mut self) -> Option<Self::Item> {
Expand Down
2 changes: 2 additions & 0 deletions crates/vertigo/src/driver_module/js_value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod memory_block;
mod memory_block_read;
mod memory_block_write;
mod serialize;
#[cfg(feature = "chrono")]
mod serialize_chrono;

pub use js_json_struct::JsJson;
pub use js_value_struct::JsValue;
Expand Down
20 changes: 0 additions & 20 deletions crates/vertigo/src/driver_module/js_value/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,26 +300,6 @@ impl<T: JsJsonDeserialize> JsJsonDeserialize for BTreeMap<String, T> {
}
}

#[cfg(feature = "chrono")]
impl JsJsonSerialize for chrono::DateTime<chrono::Utc> {
fn to_json(self) -> JsJson {
self.to_rfc3339().to_json()
}
}

#[cfg(feature = "chrono")]
impl JsJsonDeserialize for chrono::DateTime<chrono::Utc> {
fn from_json(context: JsJsonContext, json: JsJson) -> Result<Self, JsJsonContext> {
let datetime_str = String::from_json(context.clone(), json)?;
chrono::DateTime::parse_from_rfc3339(&datetime_str)
.map_err(|err| {
let message = ["DateTime parsing failed: ", &err.to_string()].concat();
context.add(message)
})
.map(|dt| dt.to_utc())
}
}

/// Deserialize from JsJson to T
pub fn from_json<T: JsJsonDeserialize>(json: JsJson) -> Result<T, String> {
let context = JsJsonContext::new("root");
Expand Down
39 changes: 39 additions & 0 deletions crates/vertigo/src/driver_module/js_value/serialize_chrono.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use super::js_json_struct::JsJson;
use super::{JsJsonContext, JsJsonDeserialize, JsJsonSerialize};

pub static NAIVE_DATE_FORMAT: &str = "%Y-%m-%d";

impl JsJsonSerialize for chrono::DateTime<chrono::Utc> {
fn to_json(self) -> JsJson {
self.to_rfc3339().to_json()
}
}

impl JsJsonDeserialize for chrono::DateTime<chrono::Utc> {
fn from_json(context: JsJsonContext, json: JsJson) -> Result<Self, JsJsonContext> {
let datetime_str = String::from_json(context.clone(), json)?;
chrono::DateTime::parse_from_rfc3339(&datetime_str)
.map_err(|err| {
let message = ["DateTime parsing failed: ", &err.to_string()].concat();
context.add(message)
})
.map(|dt| dt.to_utc())
}
}

impl JsJsonSerialize for chrono::NaiveDate {
fn to_json(self) -> JsJson {
self.format(NAIVE_DATE_FORMAT).to_string().to_json()
}
}

impl JsJsonDeserialize for chrono::NaiveDate {
fn from_json(context: JsJsonContext, json: JsJson) -> Result<Self, JsJsonContext> {
let datetime_str = String::from_json(context.clone(), json)?;
chrono::NaiveDate::parse_from_str(&datetime_str, NAIVE_DATE_FORMAT)
.map_err(|err| {
let message = ["DateTime parsing failed: ", &err.to_string()].concat();
context.add(message)
})
}
}
Loading