notcurses/input/
received.rs

1// notcurses::input::received
2//
3//!
4//
5
6use super::Key;
7
8/// A received [`char`] or [`Key`].
9#[derive(Clone, Copy, PartialEq, Eq)]
10pub enum Received {
11    /// No input was received.
12    ///
13    /// A `0x00` (NUL) was received, meaning no input.
14    NoInput,
15
16    /// A synthesized event was received.
17    Key(Key),
18
19    /// A valid [`char`] was received.
20    Char(char),
21}
22
23impl Received {
24    /// Returns `true` if a specific `Key` has been received.
25    #[inline]
26    pub fn is_key(&self, key: Key) -> bool {
27        matches!(self, Self::Key(k) if *k == key)
28    }
29
30    /// Returns `true` if a specific `char` has been received.
31    #[inline]
32    pub const fn is_char(&self, c: char) -> bool {
33        matches!(self, Self::Char(ch) if *ch == c)
34    }
35
36    /// Returns the received `Key`, if any.
37    #[inline]
38    pub const fn key(&self) -> Option<Key> {
39        if let Self::Key(k) = self {
40            Some(*k)
41        } else {
42            None
43        }
44    }
45
46    /// Returns the received `char`, if any.
47    #[inline]
48    pub const fn char(&self) -> Option<char> {
49        if let Self::Char(c) = self {
50            Some(*c)
51        } else {
52            None
53        }
54    }
55}
56
57mod core_impls {
58    use super::Received;
59    use crate::sys::NcReceived;
60    use core::fmt;
61
62    impl Default for Received {
63        fn default() -> Self {
64            Self::NoInput
65        }
66    }
67
68    impl fmt::Display for Received {
69        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70            let string = match self {
71                Received::Key(k) => format!["{k}"],
72                Received::Char(c) => format!["{c:?}"],
73                Received::NoInput => "NoInput".to_string(),
74            };
75            write!(f, "{}", string)
76        }
77    }
78    impl fmt::Debug for Received {
79        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80            let string = match self {
81                Received::Key(k) => format!["Key({k})"],
82                Received::Char(c) => format!["Char({c:?})"],
83                Received::NoInput => "NoInput".to_string(),
84            };
85            write!(f, "Received::{}", string)
86        }
87    }
88
89    impl From<NcReceived> for Received {
90        fn from(nc: NcReceived) -> Self {
91            match nc {
92                NcReceived::NoInput => Received::NoInput,
93                NcReceived::Key(k) => Received::Key(k.into()),
94                NcReceived::Char(c) => Received::Char(c),
95            }
96        }
97    }
98    impl From<Received> for NcReceived {
99        fn from(r: Received) -> Self {
100            match r {
101                Received::NoInput => NcReceived::NoInput,
102                Received::Key(k) => NcReceived::Key(k.into()),
103                Received::Char(c) => NcReceived::Char(c),
104            }
105        }
106    }
107}