gooey/interface/model/component/
mod.rs

1use std::convert::TryFrom;
2use derive_more::{From, TryInto};
3
4pub mod pixmap;
5pub use self::pixmap::Pixmap;
6
7#[derive(Clone, Debug, PartialEq, From, TryInto)]
8pub enum Component {
9  Empty,
10  Flag     (bool),
11  Unsigned (u64),
12  Signed   (i64),
13  Float    (f64),
14  IntVec   (Vec <i64>),
15  FloatVec (Vec <f64>),
16  Text     (String),
17  Pixmap   (Pixmap)
18}
19
20pub trait Kind : Into <Component> + TryFrom <Component> {
21  fn try_ref     (component : &Component)     -> Option <&Self>;
22  fn try_ref_mut (component : &mut Component) -> Option <&mut Self>;
23}
24
25impl Component {
26  pub fn clear (&mut self) {
27    *self = Component::Empty
28  }
29}
30
31impl Default for Component {
32  fn default() -> Self {
33    Component::Empty
34  }
35}
36
37impl Kind for Component {
38  fn try_ref (component : &Component) -> Option <&Self> {
39    Some (component)
40  }
41  fn try_ref_mut (component : &mut Component) -> Option <&mut Self> {
42    Some (component)
43  }
44}
45
46macro impl_kind {
47  ($component:ident, $kind:ty) => {
48    impl Kind for $kind {
49      fn try_ref (component : &Component) -> Option <&Self> {
50        match component {
51          Component::$component (t) => Some (t),
52          _ => None
53        }
54      }
55      fn try_ref_mut (component : &mut Component) -> Option <&mut Self> {
56        match component {
57          Component::$component (t) => Some (t),
58          _ => None
59        }
60      }
61    }
62  }
63}
64
65impl_kind!(Flag,     bool);
66impl_kind!(Unsigned, u64);
67impl_kind!(Signed,   i64);
68impl_kind!(Float,    f64);
69impl_kind!(IntVec,   Vec <i64>);
70impl_kind!(FloatVec, Vec <f64>);
71impl_kind!(Text,     String);