Skip to main content

glory_web/view/
flags.rs

1//! Boolean class sources retain a bool, not a formatted whole class string.
2use super::*;
3pub trait BoolValue: sealed::Flag + 'static {
4    fn validate_bool(&self, runtime: RuntimeId) -> Result<()>;
5    fn reactive(&self) -> bool {
6        true
7    }
8    fn read_bool(&mut self) -> Result<bool>;
9}
10impl sealed::Flag for bool {}
11impl BoolValue for bool {
12    fn validate_bool(&self, _: RuntimeId) -> Result<()> {
13        Ok(())
14    }
15    fn reactive(&self) -> bool {
16        false
17    }
18    fn read_bool(&mut self) -> Result<bool> {
19        Ok(*self)
20    }
21}
22macro_rules! flag_handle {
23    ($ty:ident) => {
24        impl sealed::Flag for $ty<bool> {}
25        impl BoolValue for $ty<bool> {
26            fn validate_bool(&self, r: RuntimeId) -> Result<()> {
27                TextValue::validate(self, r)
28            }
29            fn read_bool(&mut self) -> Result<bool> {
30                self.try_get().map_err(error)
31            }
32        }
33    };
34}
35flag_handle!(Cage);
36flag_handle!(Bond);
37impl sealed::Flag for Vary<bool> {}
38impl BoolValue for Vary<bool> {
39    fn validate_bool(&self, r: RuntimeId) -> Result<()> {
40        TextValue::validate(self, r)
41    }
42    fn reactive(&self) -> bool {
43        self.is_reactive()
44    }
45    fn read_bool(&mut self) -> Result<bool> {
46        self.try_get().map_err(error)
47    }
48}
49impl<F: FnMut() -> bool + 'static> sealed::Flag for Computed<F> {}
50impl<F: FnMut() -> bool + 'static> BoolValue for Computed<F> {
51    fn validate_bool(&self, _: RuntimeId) -> Result<()> {
52        Ok(())
53    }
54    fn read_bool(&mut self) -> Result<bool> {
55        Ok((self.0)())
56    }
57}