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, Default, Eq, PartialEq, From, TryInto)]
14pub enum Component {
15  #[default]
16  Empty,
17  Body   (Body),
18  Canvas (Canvas),
19  Image  (Image),
20  Sfx    (Sfx)
21}
22
23pub trait Kind : Into <Component> + TryFrom <Component> {
24  fn try_ref     (component : &Component)     -> Option <&Self>;
25  fn try_ref_mut (component : &mut Component) -> Option <&mut Self>;
26}
27
28impl Component {
29  pub fn clear (&mut self) {
30    *self = Component::Empty
31  }
32}
33
34
35impl Kind for Component {
36  fn try_ref (component : &Component) -> Option <&Self> {
37    Some (component)
38  }
39  fn try_ref_mut (component : &mut Component) -> Option <&mut Self> {
40    Some (component)
41  }
42}
43
44macro impl_kind {
45  ($kind:ident) => {
46    impl Kind for $kind {
47      fn try_ref (component : &Component) -> Option <&Self> {
48        match component {
49          Component::$kind (t) => Some (t),
50          _ => None
51        }
52      }
53      fn try_ref_mut (component : &mut Component) -> Option <&mut Self> {
54        match component {
55          Component::$kind (t) => Some (t),
56          _ => None
57        }
58      }
59    }
60  }
61}