yew_nested_router/
state.rs

1use serde::Serialize;
2use wasm_bindgen::JsValue;
3use yew::html::IntoPropValue;
4
5/// A page state value
6///
7/// This is a thing wrapper around [`JsValue`], allowing for an easier interaction with the API,
8/// especially in the context of `yew`.
9#[derive(PartialEq, Debug, Clone)]
10pub struct State(pub(crate) JsValue);
11
12impl State {
13    /// A `null` value
14    pub const fn null() -> Self {
15        State(JsValue::null())
16    }
17
18    /// Serialize a value into [`JsValue`].
19    pub fn json<S: Serialize>(value: &S) -> Result<Self, serde_json::Error> {
20        use gloo_utils::format::JsValueSerdeExt;
21        Ok(State(JsValue::from_serde(value)?))
22    }
23}
24
25impl From<JsValue> for State {
26    fn from(value: JsValue) -> Self {
27        Self(value)
28    }
29}
30
31impl Default for State {
32    fn default() -> Self {
33        Self::null()
34    }
35}
36
37impl IntoPropValue<State> for &str {
38    fn into_prop_value(self) -> State {
39        State(JsValue::from_str(self))
40    }
41}
42
43impl IntoPropValue<State> for String {
44    fn into_prop_value(self) -> State {
45        State(JsValue::from_str(&self))
46    }
47}
48
49impl IntoPropValue<State> for &String {
50    fn into_prop_value(self) -> State {
51        State(JsValue::from_str(self))
52    }
53}
54
55impl IntoPropValue<State> for JsValue {
56    fn into_prop_value(self) -> State {
57        State(self)
58    }
59}