gooey/interface/controller/component/
mod.rs

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