Skip to content

Commit 4006925

Browse files
committed
JsJson implementation for unit type
1 parent 0943788 commit 4006925

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

.github/workflows/pipeline.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- uses: actions/checkout@v2
5353
- uses: actions-rs/toolchain@v1
5454
with:
55-
toolchain: nightly-2024-02-28
55+
toolchain: nightly-2024-07-01
5656
target: wasm32-unknown-unknown
5757
components: clippy
5858
override: true
@@ -74,7 +74,7 @@ jobs:
7474
- uses: actions/checkout@v2
7575
- uses: actions-rs/toolchain@v1
7676
with:
77-
toolchain: nightly-2024-02-28
77+
toolchain: nightly-2024-07-01
7878
target: wasm32-unknown-unknown
7979
components: clippy
8080
override: true
@@ -85,7 +85,7 @@ jobs:
8585
- name: vertigo-cli
8686
run: |
8787
cargo build --release -p vertigo-cli
88-
rustup default nightly-2024-02-28
88+
rustup default nightly-2024-07-01
8989
mkdir ../vertigo-cli-test
9090
mv target/release/vertigo ../vertigo-cli-test
9191
cd ../vertigo-cli-test

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* `Reactive` trait that allows generic components to be more flexible with props
99
* `BTreeMap` and `chrono::DateTime<Utc>` support in `AutoJsJson`
1010
* `#[js_json(default = "None")]` attribute to `AutoJsJson`
11+
* `JsJson` implementation for unit type `()`
1112
* All http methods in `FetchMethod`
1213
* `history_replace` method in `Driver`
1314
* vertigo-cli: `add-watch-path` to `watch` command

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,25 @@ impl JsJsonDeserialize for bool {
171171
}
172172
}
173173

174+
impl JsJsonSerialize for () {
175+
fn to_json(self) -> JsJson {
176+
JsJson::Object(HashMap::default())
177+
}
178+
}
179+
180+
impl JsJsonDeserialize for () {
181+
fn from_json(context: JsJsonContext, json: JsJson) -> Result<Self, JsJsonContext> {
182+
let map = json.get_hashmap(&context)?;
183+
184+
if !map.is_empty() {
185+
let message = "Empty {} expected, inner content received".to_string();
186+
return Err(context.add(message))
187+
}
188+
189+
Ok(())
190+
}
191+
}
192+
174193
impl JsJsonSerialize for &str {
175194
fn to_json(self) -> JsJson {
176195
JsJson::String(self.into())
@@ -384,4 +403,17 @@ mod tests {
384403

385404
assert_eq!(ccc, eee);
386405
}
406+
407+
#[test]
408+
fn test_unit() {
409+
let unit = JsJson::Object(HashMap::default());
410+
411+
let Ok(()) = from_json::<()>(unit.clone()) else {
412+
unreachable!();
413+
};
414+
415+
let unit2 = to_json(());
416+
417+
assert_eq!(unit2, unit)
418+
}
387419
}

0 commit comments

Comments
 (0)