Skip to content

Commit

Permalink
JsJson implementation for unit type
Browse files Browse the repository at this point in the history
  • Loading branch information
sfisol committed Jul 11, 2024
1 parent 0943788 commit 8b0e9b0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2024-02-28
toolchain: nightly-2024-07-01
target: wasm32-unknown-unknown
components: clippy
override: true
Expand All @@ -74,7 +74,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2024-02-28
toolchain: nightly-2024-07-01
target: wasm32-unknown-unknown
components: clippy
override: true
Expand All @@ -85,7 +85,7 @@ jobs:
- name: vertigo-cli
run: |
cargo build --release -p vertigo-cli
rustup default nightly-2024-02-28
rustup default nightly-2024-07-01
mkdir ../vertigo-cli-test
mv target/release/vertigo ../vertigo-cli-test
cd ../vertigo-cli-test
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* `Reactive` trait that allows generic components to be more flexible with props
* `BTreeMap` and `chrono::DateTime<Utc>` support in `AutoJsJson`
* `#[js_json(default = "None")]` attribute to `AutoJsJson`
* `JsJson` implementation for unit type `()`
* All http methods in `FetchMethod`
* `history_replace` method in `Driver`
* vertigo-cli: `add-watch-path` to `watch` command
Expand Down
5 changes: 1 addition & 4 deletions crates/vertigo/src/computed/reactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@ use crate::{Context, Value};
/// inner: Value::new("".to_string())
/// };
///
/// // Specialize Print component
/// type PrintL = Print::<Lisper>;
///
/// // Use reactive object in generic component
/// let _ = dom! {
/// <div>
/// <PrintL saying={lisper.clone()} />
/// <Print saying={&lisper} />
/// </div>
/// };
///
Expand Down
32 changes: 32 additions & 0 deletions crates/vertigo/src/driver_module/js_value/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ impl JsJsonDeserialize for bool {
}
}

impl JsJsonSerialize for () {
fn to_json(self) -> JsJson {
JsJson::Object(HashMap::default())
}
}

impl JsJsonDeserialize for () {
fn from_json(context: JsJsonContext, json: JsJson) -> Result<Self, JsJsonContext> {
let map = json.get_hashmap(&context)?;

if !map.is_empty() {
let message = "Empty {} expected, inner content received".to_string();
return Err(context.add(message))
}

Ok(())
}
}

impl JsJsonSerialize for &str {
fn to_json(self) -> JsJson {
JsJson::String(self.into())
Expand Down Expand Up @@ -384,4 +403,17 @@ mod tests {

assert_eq!(ccc, eee);
}

#[test]
fn test_unit() {
let unit = JsJson::Object(HashMap::default());

let Ok(()) = from_json::<()>(unit.clone()) else {
unreachable!();
};

let unit2 = to_json(());

assert_eq!(unit2, unit)
}
}

0 comments on commit 8b0e9b0

Please sign in to comment.