yew_nested_router/
state.rs1use serde::Serialize;
2use wasm_bindgen::JsValue;
3use yew::html::IntoPropValue;
4
5#[derive(PartialEq, Debug, Clone)]
10pub struct State(pub(crate) JsValue);
11
12impl State {
13 pub const fn null() -> Self {
15 State(JsValue::null())
16 }
17
18 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}