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, Default, From, TryInto)]
18// TODO: consider boxing large components to reduce enum size
19#[expect(clippy::large_enum_variant)]
20pub enum Component {
21  #[default]
22  Empty,
23  Cursor    (Cursor),
24  Layout    (Layout),
25  Numeral   (Numeral),
26  Scroll    (Scroll),
27  Selection (Selection),
28  Switch    (Switch)
29}
30
31pub trait Kind : Into <Component> + TryFrom <Component> {
32  fn try_ref     (component : &Component)     -> Option <&Self>;
33  fn try_ref_mut (component : &mut Component) -> Option <&mut Self>;
34}
35
36impl Component {
37  pub fn clear (&mut self) {
38    *self = Component::Empty
39  }
40}
41
42
43impl Kind for Component {
44  fn try_ref (component : &Component) -> Option <&Self> {
45    Some (component)
46  }
47  fn try_ref_mut (component : &mut Component) -> Option <&mut Self> {
48    Some (component)
49  }
50}
51
52macro impl_kind {
53  ($kind:ident) => {
54    impl Kind for $kind {
55      fn try_ref (component : &Component) -> Option <&Self> {
56        match component {
57          Component::$kind (t) => Some (t),
58          _ => None
59        }
60      }
61      fn try_ref_mut (component : &mut Component) -> Option <&mut Self> {
62        match component {
63          Component::$kind (t) => Some (t),
64          _ => None
65        }
66      }
67    }
68  }
69}