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