gooey/interface/view/component/
mod.rs

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