sycamore_web/
bind.rs

1//! Definition for bind-able attributes/properties.
2
3use crate::events::EventDescriptor;
4use crate::*;
5
6/// Description for a bind-able attribute/property.
7pub trait BindDescriptor {
8    /// The event which we listen to to update the value.
9    type Event: EventDescriptor;
10    /// The value of the signal that drives the attribute/property.
11    type ValueTy: Into<JsValue> + Clone;
12    /// The name of the property to which we are binding.
13    const TARGET_PROPERTY: &'static str;
14    /// Function for converting from JS to Rust type.
15    const CONVERT_FROM_JS: for<'a> fn(&'a JsValue) -> Option<Self::ValueTy>;
16}
17
18macro_rules! impl_bind {
19    ($name:ident: $event:ty, $value:ty, $target:expr, $fn:expr) => {
20        #[allow(non_camel_case_types)]
21        pub struct $name;
22        impl BindDescriptor for $name {
23            type Event = $event;
24            type ValueTy = $value;
25            const TARGET_PROPERTY: &'static str = $target;
26            const CONVERT_FROM_JS: for<'a> fn(&'a JsValue) -> Option<Self::ValueTy> = $fn;
27        }
28    };
29}
30
31macro_rules! impl_binds {
32    ($($name:ident: $event:ty, $value:ty, $target:expr, $fn:expr,)*) => {
33        $(impl_bind!($name: $event, $value, $target, $fn);)*
34    };
35}
36
37impl_binds! {
38    value: events::input, String, "value", JsValue::as_string,
39    valueAsNumber: events::input, f64, "valueAsNumber", JsValue::as_f64,
40    checked: events::change, bool, "checked", JsValue::as_bool,
41}