Skip to main content

thedes_tui_core/runtime/device/
null.rs

1use crate::{
2    audio::{self, device::AudioDevice},
3    input::{self, device::InputDevice},
4    panic::{self, restore::PanicRestoreGuard},
5    screen::{self, device::ScreenDevice},
6};
7
8use super::{Error, RuntimeDevice};
9
10pub fn open() -> Box<dyn RuntimeDevice> {
11    Box::new(NullRuntimeDevice::new())
12}
13
14#[derive(Debug)]
15struct NullRuntimeDevice {
16    initialized: bool,
17}
18
19impl NullRuntimeDevice {
20    pub fn new() -> Self {
21        Self { initialized: false }
22    }
23}
24
25impl RuntimeDevice for NullRuntimeDevice {
26    fn blocking_init(&mut self) -> Result<(), Error> {
27        if self.initialized {
28            Err(Error::AlreadyInit)?
29        }
30        self.initialized = true;
31        Ok(())
32    }
33
34    fn blocking_shutdown(&mut self) -> Result<(), Error> {
35        if !self.initialized {
36            Err(Error::NotInit)?
37        }
38        self.initialized = false;
39        Ok(())
40    }
41
42    fn open_input_device(&mut self) -> Box<dyn InputDevice> {
43        input::device::null::open()
44    }
45
46    fn open_screen_device(&mut self) -> Box<dyn ScreenDevice> {
47        screen::device::null::open()
48    }
49
50    fn open_audio_device(&mut self) -> Box<dyn AudioDevice> {
51        audio::device::null::open()
52    }
53
54    fn open_panic_restore_guard(&mut self) -> Box<dyn PanicRestoreGuard> {
55        panic::restore::null::open()
56    }
57}