From 99e024f252b3b52eff7f0ff1764c9948ebff6bf2 Mon Sep 17 00:00:00 2001 From: Eric Rodrigues Pires Date: Sun, 9 Nov 2025 21:15:33 -0300 Subject: [PATCH] Improve datetime for TOML in website and fix parser bug --- duper-js-wasm/CHANGELOG.md | 4 +++ duper-js-wasm/package-lock.json | 35 +++++++++++++----------- duper-js-wasm/package.json | 2 +- duper-js-wasm/src/temporal.rs | 2 +- duper-python/CHANGELOG.md | 4 +++ duper-vs-code/CHANGELOG.md | 6 +++++ duper/src/parser/mod.rs | 13 --------- duper/src/parser/temporal.rs | 20 +++++++++++++- duper_website/docs/index.md | 2 +- duper_website/package-lock.json | 37 +++++++++++++++++++++++++- duper_website/package.json | 4 ++- duper_website/src/lib.rs | 1 + duper_website/src/temporal.rs | 44 +++++++++++++++++++++++++++++++ duper_website/src/visitor/toml.rs | 30 ++++++++++++++++----- 14 files changed, 162 insertions(+), 42 deletions(-) create mode 100644 duper_website/src/temporal.rs diff --git a/duper-js-wasm/CHANGELOG.md b/duper-js-wasm/CHANGELOG.md index c7cad0a..0dc0ce8 100644 --- a/duper-js-wasm/CHANGELOG.md +++ b/duper-js-wasm/CHANGELOG.md @@ -6,6 +6,10 @@ - Add `minify` option to `stringify`. +### Changed + +- Update to comply with spec 0.4.0. + ## 0.2.1 (2025-11-01) ### Changed diff --git a/duper-js-wasm/package-lock.json b/duper-js-wasm/package-lock.json index f43181b..d42ccba 100644 --- a/duper-js-wasm/package-lock.json +++ b/duper-js-wasm/package-lock.json @@ -9,7 +9,7 @@ "version": "0.2.1", "license": "MIT", "dependencies": { - "temporal-polyfill": "^0.3.0" + "@js-temporal/polyfill": "^0.5.1" }, "devDependencies": { "vite": "^7.1.12", @@ -467,6 +467,18 @@ "dev": true, "license": "MIT" }, + "node_modules/@js-temporal/polyfill": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/@js-temporal/polyfill/-/polyfill-0.5.1.tgz", + "integrity": "sha512-hloP58zRVCRSpgDxmqCWJNlizAlUgJFqG2ypq79DCvyv9tHjRYMDOcPFjzfl/A1/YxDvRCZz8wvZvmapQnKwFQ==", + "license": "ISC", + "dependencies": { + "jsbi": "^4.3.0" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/@rollup/plugin-virtual": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/@rollup/plugin-virtual/-/plugin-virtual-3.0.2.tgz", @@ -1309,6 +1321,12 @@ "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } }, + "node_modules/jsbi": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/jsbi/-/jsbi-4.3.2.tgz", + "integrity": "sha512-9fqMSQbhJykSeii05nxKl4m6Eqn2P6rOlYiS+C5Dr/HPIU/7yZxu5qzbs40tgaFORiw2Amd0mirjxatXYMkIew==", + "license": "Apache-2.0" + }, "node_modules/magic-string": { "version": "0.30.21", "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz", @@ -1476,21 +1494,6 @@ "dev": true, "license": "MIT" }, - "node_modules/temporal-polyfill": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/temporal-polyfill/-/temporal-polyfill-0.3.0.tgz", - "integrity": "sha512-qNsTkX9K8hi+FHDfHmf22e/OGuXmfBm9RqNismxBrnSmZVJKegQ+HYYXT+R7Ha8F/YSm2Y34vmzD4cxMu2u95g==", - "license": "MIT", - "dependencies": { - "temporal-spec": "0.3.0" - } - }, - "node_modules/temporal-spec": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/temporal-spec/-/temporal-spec-0.3.0.tgz", - "integrity": "sha512-n+noVpIqz4hYgFSMOSiINNOUOMFtV5cZQNCmmszA6GiVFVRt3G7AqVyhXjhCSmowvQn+NsGn+jMDMKJYHd3bSQ==", - "license": "ISC" - }, "node_modules/tinybench": { "version": "2.9.0", "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.9.0.tgz", diff --git a/duper-js-wasm/package.json b/duper-js-wasm/package.json index b5f62ab..e76df76 100644 --- a/duper-js-wasm/package.json +++ b/duper-js-wasm/package.json @@ -47,6 +47,6 @@ "vitest": "^4.0.6" }, "dependencies": { - "temporal-polyfill": "^0.3.0" + "@js-temporal/polyfill": "^0.5.1" } } diff --git a/duper-js-wasm/src/temporal.rs b/duper-js-wasm/src/temporal.rs index 54afa67..bf76eec 100644 --- a/duper-js-wasm/src/temporal.rs +++ b/duper-js-wasm/src/temporal.rs @@ -1,6 +1,6 @@ use wasm_bindgen::prelude::*; -#[wasm_bindgen(module = "temporal-polyfill")] +#[wasm_bindgen(module = "@js-temporal/polyfill")] unsafe extern "C" { // Instant #[wasm_bindgen(js_namespace = ["Temporal"])] diff --git a/duper-python/CHANGELOG.md b/duper-python/CHANGELOG.md index c5e3425..798d0cf 100644 --- a/duper-python/CHANGELOG.md +++ b/duper-python/CHANGELOG.md @@ -6,6 +6,10 @@ - Add `minify` option to `dump`/`dumps`. +### Changed + +- Update to comply with spec 0.4.0. + ## 0.2.2 (2025-10-28) ### Changed diff --git a/duper-vs-code/CHANGELOG.md b/duper-vs-code/CHANGELOG.md index ae5b99c..34a3d86 100644 --- a/duper-vs-code/CHANGELOG.md +++ b/duper-vs-code/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Changed + +- Update to comply with spec 0.4.0. + ## 0.3.2 (2025-11-01) ### Added diff --git a/duper/src/parser/mod.rs b/duper/src/parser/mod.rs index 5c54ab1..69e6551 100644 --- a/duper/src/parser/mod.rs +++ b/duper/src/parser/mod.rs @@ -185,19 +185,6 @@ pub(crate) fn identified_value<'a>() } }), identifier() - .and_is( - choice(( - just("Instant"), - just("ZonedDateTime"), - just("PlainDate"), - just("PlainTime"), - just("PlainDateTime"), - just("PlainYearMonth"), - just("PlainMonthDay"), - just("Duration"), - )) - .not(), - ) .then(temporal_unspecified().delimited_by(just('('), just(')'))) .map(|(identifier, temporal)| DuperValue { identifier: Some(identifier), diff --git a/duper/src/parser/temporal.rs b/duper/src/parser/temporal.rs index 015dbba..809c045 100644 --- a/duper/src/parser/temporal.rs +++ b/duper/src/parser/temporal.rs @@ -167,6 +167,16 @@ pub(crate) fn zoned_date_time<'a>() .ignored() } +pub(crate) fn non_z_zoned_date_time<'a>() +-> impl Parser<'a, &'a str, (), extra::Err>> + Clone { + date_time() + .then(time_num_offset()) + .then(timezone()) + .then(suffix_tag().repeated()) + .padded() + .ignored() +} + pub(crate) fn instant<'a>() -> impl Parser<'a, &'a str, (), extra::Err>> + Clone { choice(( zoned_date_time(), @@ -174,6 +184,14 @@ pub(crate) fn instant<'a>() -> impl Parser<'a, &'a str, (), extra::Err() -> impl Parser<'a, &'a str, (), extra::Err>> + Clone +{ + choice(( + non_z_zoned_date_time(), + date_time().then(time_num_offset()).padded().ignored(), + )) +} + pub(crate) fn plain_date<'a>() -> impl Parser<'a, &'a str, (), extra::Err>> + Clone { choice(( plain_date_time(), @@ -191,7 +209,7 @@ pub(crate) fn plain_time<'a>() -> impl Parser<'a, &'a str, (), extra::Err() -> impl Parser<'a, &'a str, (), extra::Err>> + Clone { choice(( - instant(), + non_z_instant(), date_time().then(suffix_tag().repeated()).padded().ignored(), )) } diff --git a/duper_website/docs/index.md b/duper_website/docs/index.md index 8beb42c..fc5ffef 100644 --- a/duper_website/docs/index.md +++ b/duper_website/docs/index.md @@ -52,7 +52,7 @@ const initial = `UserProfile({ avatar: Png(b64"iVBORw0KGgoAAAANSUhEUgAAAGQ"), bio: r#"Hello! I'm a super "duper" user!"#, last_logins: [ - (IPv4Address("192.168.1.100"), Instant('2024-03-20T14:30:00Z')), + (IPv4Address("192.168.1.100"), Instant('2024-03-20T14:30:00+00:00')), ], })`; diff --git a/duper_website/package-lock.json b/duper_website/package-lock.json index 1659940..ac67b3f 100644 --- a/duper_website/package-lock.json +++ b/duper_website/package-lock.json @@ -8,11 +8,13 @@ "license": "MIT", "dependencies": { "@floating-ui/vue": "^1.1.9", + "@js-temporal/polyfill": "^0.5.1", "@shikijs/langs": "^3.14.0", "@shikijs/monaco": "^3.14.0", "@shikijs/themes": "^3.14.0", "monaco-editor": "^0.54.0", - "shiki": "^3.14.0" + "shiki": "^3.14.0", + "temporal-polyfill": "^0.3.0" }, "devDependencies": { "@biomejs/biome": "2.3.1", @@ -231,6 +233,18 @@ "version": "1.5.5", "license": "MIT" }, + "node_modules/@js-temporal/polyfill": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/@js-temporal/polyfill/-/polyfill-0.5.1.tgz", + "integrity": "sha512-hloP58zRVCRSpgDxmqCWJNlizAlUgJFqG2ypq79DCvyv9tHjRYMDOcPFjzfl/A1/YxDvRCZz8wvZvmapQnKwFQ==", + "license": "ISC", + "dependencies": { + "jsbi": "^4.3.0" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/@rolldown/pluginutils": { "version": "1.0.0-beta.29", "dev": true, @@ -991,6 +1005,12 @@ "url": "https://github.com/sponsors/mesqueeb" } }, + "node_modules/jsbi": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/jsbi/-/jsbi-4.3.2.tgz", + "integrity": "sha512-9fqMSQbhJykSeii05nxKl4m6Eqn2P6rOlYiS+C5Dr/HPIU/7yZxu5qzbs40tgaFORiw2Amd0mirjxatXYMkIew==", + "license": "Apache-2.0" + }, "node_modules/magic-string": { "version": "0.30.21", "license": "MIT", @@ -1360,6 +1380,21 @@ "dev": true, "license": "MIT" }, + "node_modules/temporal-polyfill": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/temporal-polyfill/-/temporal-polyfill-0.3.0.tgz", + "integrity": "sha512-qNsTkX9K8hi+FHDfHmf22e/OGuXmfBm9RqNismxBrnSmZVJKegQ+HYYXT+R7Ha8F/YSm2Y34vmzD4cxMu2u95g==", + "license": "MIT", + "dependencies": { + "temporal-spec": "0.3.0" + } + }, + "node_modules/temporal-spec": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/temporal-spec/-/temporal-spec-0.3.0.tgz", + "integrity": "sha512-n+noVpIqz4hYgFSMOSiINNOUOMFtV5cZQNCmmszA6GiVFVRt3G7AqVyhXjhCSmowvQn+NsGn+jMDMKJYHd3bSQ==", + "license": "ISC" + }, "node_modules/tinyglobby": { "version": "0.2.15", "dev": true, diff --git a/duper_website/package.json b/duper_website/package.json index 524d040..a7fb1e5 100644 --- a/duper_website/package.json +++ b/duper_website/package.json @@ -21,11 +21,13 @@ }, "dependencies": { "@floating-ui/vue": "^1.1.9", + "@js-temporal/polyfill": "^0.5.1", "@shikijs/langs": "^3.14.0", "@shikijs/monaco": "^3.14.0", "@shikijs/themes": "^3.14.0", "monaco-editor": "^0.54.0", - "shiki": "^3.14.0" + "shiki": "^3.14.0", + "temporal-polyfill": "^0.3.0" }, "devDependencies": { "@biomejs/biome": "2.3.1", diff --git a/duper_website/src/lib.rs b/duper_website/src/lib.rs index 57b588a..2b232ba 100644 --- a/duper_website/src/lib.rs +++ b/duper_website/src/lib.rs @@ -2,6 +2,7 @@ use duper::DuperParser; use serde::Serialize; use wasm_bindgen::prelude::*; +mod temporal; mod visitor; #[wasm_bindgen(typescript_custom_section)] diff --git a/duper_website/src/temporal.rs b/duper_website/src/temporal.rs new file mode 100644 index 0000000..4b7137d --- /dev/null +++ b/duper_website/src/temporal.rs @@ -0,0 +1,44 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen(module = "@js-temporal/polyfill")] +unsafe extern "C" { + // Instant + #[wasm_bindgen(js_namespace = ["Temporal"])] + pub(crate) type Instant; + + #[wasm_bindgen(method, js_name = toString)] + pub(crate) fn to_string(this: &Instant) -> String; + + #[wasm_bindgen(js_namespace = ["Temporal", "Instant"])] + pub(crate) fn from(value: &str) -> Instant; + + // PlainDate + #[wasm_bindgen(js_namespace = ["Temporal"])] + pub(crate) type PlainDate; + + #[wasm_bindgen(method, js_name = toString)] + pub(crate) fn to_string(this: &PlainDate) -> String; + + #[wasm_bindgen(js_namespace = ["Temporal", "PlainDate"])] + pub(crate) fn from(value: &str) -> PlainDate; + + // PlainTime + #[wasm_bindgen(js_namespace = ["Temporal"])] + pub(crate) type PlainTime; + + #[wasm_bindgen(method, js_name = toString)] + pub(crate) fn to_string(this: &PlainTime) -> String; + + #[wasm_bindgen(js_namespace = ["Temporal", "PlainTime"])] + pub(crate) fn from(value: &str) -> PlainTime; + + // PlainDateTime + #[wasm_bindgen(js_namespace = ["Temporal"])] + pub(crate) type PlainDateTime; + + #[wasm_bindgen(method, js_name = toString)] + pub(crate) fn to_string(this: &PlainDateTime) -> String; + + #[wasm_bindgen(js_namespace = ["Temporal", "PlainDateTime"])] + pub(crate) fn from(value: &str) -> PlainDateTime; +} diff --git a/duper_website/src/visitor/toml.rs b/duper_website/src/visitor/toml.rs index 9183d1b..0328950 100644 --- a/duper_website/src/visitor/toml.rs +++ b/duper_website/src/visitor/toml.rs @@ -7,6 +7,8 @@ use duper::{ }; use toml::{Value, value::Datetime}; +use crate::temporal::{Instant, PlainDate, PlainDateTime, PlainTime}; + // A visitor that serializes Duper into a TOML value. pub(crate) struct TomlVisitor {} @@ -76,14 +78,28 @@ impl DuperVisitor for TomlVisitor { temporal: &DuperTemporal<'a>, ) -> Self::Value { match temporal { - DuperTemporal::Instant(inner) - | DuperTemporal::PlainDateTime(inner) - | DuperTemporal::PlainDate(inner) - | DuperTemporal::PlainTime(inner) => { - let inner = inner.as_ref(); - let datetime_str = inner.split_once('[').map(|(left, _)| left).unwrap_or(inner); + DuperTemporal::Instant(inner) => { + let datetime_str = Instant::from(inner.as_ref()).to_string(); + Ok(Some(Value::Datetime( + Datetime::from_str(&datetime_str).map_err(|err| err.to_string())?, + ))) + } + DuperTemporal::PlainDateTime(inner) => { + let datetime_str = PlainDateTime::from(inner.as_ref()).to_string(); + Ok(Some(Value::Datetime( + Datetime::from_str(&datetime_str).map_err(|err| err.to_string())?, + ))) + } + DuperTemporal::PlainDate(inner) => { + let datetime_str = PlainDate::from(inner.as_ref()).to_string(); + Ok(Some(Value::Datetime( + Datetime::from_str(&datetime_str).map_err(|err| err.to_string())?, + ))) + } + DuperTemporal::PlainTime(inner) => { + let datetime_str = PlainTime::from(inner.as_ref()).to_string(); Ok(Some(Value::Datetime( - Datetime::from_str(datetime_str).map_err(|err| err.to_string())?, + Datetime::from_str(&datetime_str).map_err(|err| err.to_string())?, ))) } DuperTemporal::ZonedDateTime(inner) -- knotmirror 0.0.1