Skip to main content

completers/
types.rs

1//! Possible types of completions.
2
3use std::fmt::Display;
4
5/// The type of completion attempted. [ref](https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-COMP_005fTYPE).
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[repr(u8)]
8pub enum CompletionType {
9    /// Normal completion.
10    Normal = b'\t',
11    /// Listing completions after successive tabs.
12    List = b'?',
13    /// Listing alternatives on partial word completion.
14    ListAlternatives = b'!',
15    /// List completions if the word is not unmodified.
16    ListUnmodified = b'@',
17    /// Menu completion.
18    Menu = b'%',
19}
20
21impl From<&CompletionType> for u8 {
22    fn from(completion_type: &CompletionType) -> Self {
23        *completion_type as Self
24    }
25}
26
27impl From<&CompletionType> for char {
28    fn from(completion_type: &CompletionType) -> Self {
29        *completion_type as u8 as Self
30    }
31}
32
33impl TryFrom<u8> for CompletionType {
34    type Error = ();
35
36    fn try_from(value: u8) -> Result<Self, ()> {
37        match value {
38            b'\t' => Ok(Self::Normal),
39            b'?' => Ok(Self::List),
40            b'!' => Ok(Self::ListAlternatives),
41            b'@' => Ok(Self::ListUnmodified),
42            b'%' => Ok(Self::Menu),
43            _ => Err(()),
44        }
45    }
46}
47
48impl TryFrom<char> for CompletionType {
49    type Error = ();
50
51    fn try_from(value: char) -> Result<Self, ()> {
52        match value as u8 {
53            b'\t' => Ok(Self::Normal),
54            b'?' => Ok(Self::List),
55            b'!' => Ok(Self::ListAlternatives),
56            b'@' => Ok(Self::ListUnmodified),
57            b'%' => Ok(Self::Menu),
58            _ => Err(()),
59        }
60    }
61}
62
63impl Display for CompletionType {
64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        let c: char = self.into();
66        write!(f, "{c}")
67    }
68}