Skip to content

Commit b6c24a4

Browse files
committed
chrono::NaiveDate support in AutoJsJson
1 parent 2bbf4d9 commit b6c24a4

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

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

1112
### Changed
1213

crates/vertigo/src/driver_module/js_value/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ mod memory_block;
55
mod memory_block_read;
66
mod memory_block_write;
77
mod serialize;
8+
#[cfg(feature = "chrono")]
9+
mod serialize_chrono;
810

911
pub use js_json_struct::JsJson;
1012
pub use js_value_struct::JsValue;

crates/vertigo/src/driver_module/js_value/serialize.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -300,26 +300,6 @@ impl<T: JsJsonDeserialize> JsJsonDeserialize for BTreeMap<String, T> {
300300
}
301301
}
302302

303-
#[cfg(feature = "chrono")]
304-
impl JsJsonSerialize for chrono::DateTime<chrono::Utc> {
305-
fn to_json(self) -> JsJson {
306-
self.to_rfc3339().to_json()
307-
}
308-
}
309-
310-
#[cfg(feature = "chrono")]
311-
impl JsJsonDeserialize for chrono::DateTime<chrono::Utc> {
312-
fn from_json(context: JsJsonContext, json: JsJson) -> Result<Self, JsJsonContext> {
313-
let datetime_str = String::from_json(context.clone(), json)?;
314-
chrono::DateTime::parse_from_rfc3339(&datetime_str)
315-
.map_err(|err| {
316-
let message = ["DateTime parsing failed: ", &err.to_string()].concat();
317-
context.add(message)
318-
})
319-
.map(|dt| dt.to_utc())
320-
}
321-
}
322-
323303
/// Deserialize from JsJson to T
324304
pub fn from_json<T: JsJsonDeserialize>(json: JsJson) -> Result<T, String> {
325305
let context = JsJsonContext::new("root");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use super::js_json_struct::JsJson;
2+
use super::{JsJsonContext, JsJsonDeserialize, JsJsonSerialize};
3+
4+
pub static NAIVE_DATE_FORMAT: &str = "%Y-%m-%d";
5+
6+
impl JsJsonSerialize for chrono::DateTime<chrono::Utc> {
7+
fn to_json(self) -> JsJson {
8+
self.to_rfc3339().to_json()
9+
}
10+
}
11+
12+
impl JsJsonDeserialize for chrono::DateTime<chrono::Utc> {
13+
fn from_json(context: JsJsonContext, json: JsJson) -> Result<Self, JsJsonContext> {
14+
let datetime_str = String::from_json(context.clone(), json)?;
15+
chrono::DateTime::parse_from_rfc3339(&datetime_str)
16+
.map_err(|err| {
17+
let message = ["DateTime parsing failed: ", &err.to_string()].concat();
18+
context.add(message)
19+
})
20+
.map(|dt| dt.to_utc())
21+
}
22+
}
23+
24+
impl JsJsonSerialize for chrono::NaiveDate {
25+
fn to_json(self) -> JsJson {
26+
self.format(NAIVE_DATE_FORMAT).to_string().to_json()
27+
}
28+
}
29+
30+
impl JsJsonDeserialize for chrono::NaiveDate {
31+
fn from_json(context: JsJsonContext, json: JsJson) -> Result<Self, JsJsonContext> {
32+
let datetime_str = String::from_json(context.clone(), json)?;
33+
chrono::NaiveDate::parse_from_str(&datetime_str, NAIVE_DATE_FORMAT)
34+
.map_err(|err| {
35+
let message = ["DateTime parsing failed: ", &err.to_string()].concat();
36+
context.add(message)
37+
})
38+
}
39+
}

0 commit comments

Comments
 (0)