thedes_tui_core/runtime/
device.rs

1use std::fmt;
2
3use thiserror::Error;
4use tokio::io;
5
6use crate::{
7    input::device::InputDevice,
8    panic::restore::PanicRestoreGuard,
9    screen::device::ScreenDevice,
10};
11
12pub mod native;
13pub mod null;
14
15#[cfg(feature = "testing")]
16pub mod mock;
17
18#[derive(Debug, Error)]
19pub enum Error {
20    #[error("Already initialized")]
21    AlreadyInit,
22    #[error("Not initialized yet or already shut down")]
23    NotInit,
24    #[error(transparent)]
25    Io(#[from] io::Error),
26}
27
28pub trait RuntimeDevice: fmt::Debug + Send + Sync {
29    fn blocking_init(&mut self) -> Result<(), Error>;
30
31    fn blocking_shutdown(&mut self) -> Result<(), Error>;
32
33    fn open_screen_device(&mut self) -> Box<dyn ScreenDevice>;
34
35    fn open_input_device(&mut self) -> Box<dyn InputDevice>;
36
37    fn open_panic_restore_guard(&mut self) -> Box<dyn PanicRestoreGuard>;
38}
39
40impl<'a, T> RuntimeDevice for &'a mut T
41where
42    T: RuntimeDevice + ?Sized,
43{
44    fn blocking_init(&mut self) -> Result<(), Error> {
45        (**self).blocking_init()
46    }
47
48    fn blocking_shutdown(&mut self) -> Result<(), Error> {
49        (**self).blocking_shutdown()
50    }
51
52    fn open_screen_device(&mut self) -> Box<dyn ScreenDevice> {
53        (**self).open_screen_device()
54    }
55
56    fn open_input_device(&mut self) -> Box<dyn InputDevice> {
57        (**self).open_input_device()
58    }
59
60    fn open_panic_restore_guard(&mut self) -> Box<dyn PanicRestoreGuard> {
61        (**self).open_panic_restore_guard()
62    }
63}
64
65impl<T> RuntimeDevice for Box<T>
66where
67    T: RuntimeDevice + ?Sized,
68{
69    fn blocking_init(&mut self) -> Result<(), Error> {
70        (**self).blocking_init()
71    }
72
73    fn blocking_shutdown(&mut self) -> Result<(), Error> {
74        (**self).blocking_shutdown()
75    }
76
77    fn open_screen_device(&mut self) -> Box<dyn ScreenDevice> {
78        (**self).open_screen_device()
79    }
80
81    fn open_input_device(&mut self) -> Box<dyn InputDevice> {
82        (**self).open_input_device()
83    }
84
85    fn open_panic_restore_guard(&mut self) -> Box<dyn PanicRestoreGuard> {
86        (**self).open_panic_restore_guard()
87    }
88}