Skip to content

JsJson implementation for unit type #300

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

Merged
merged 1 commit into from
Jul 11, 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
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)
}
}
Loading