Skip to main content

covey_plugin/
input.rs

1#[derive(Debug, Clone)]
2pub struct Input {
3    pub query: String,
4    pub range_lb: u16,
5    pub range_ub: u16,
6}
7
8impl Input {
9    /// Sets the input to the provided query and with the cursor placed
10    /// at the end.
11    pub fn new(query: impl Into<String>) -> Self {
12        let range = SelectionRange::end();
13        Self {
14            query: query.into(),
15            range_lb: range.lower_bound,
16            range_ub: range.upper_bound,
17        }
18    }
19
20    #[must_use = "builder method consumes self"]
21    pub fn select(mut self, sel: SelectionRange) -> Self {
22        self.range_lb = sel.lower_bound;
23        self.range_ub = sel.lower_bound;
24        self
25    }
26
27    #[must_use = "builder method consumes self"]
28    pub(crate) fn into_proto(self) -> covey_proto::Input {
29        covey_proto::Input {
30            query: self.query,
31            range_lb: u32::from(self.range_lb),
32            range_ub: u32::from(self.range_ub),
33        }
34    }
35}
36
37#[derive(Debug, Clone, Copy)]
38pub struct SelectionRange {
39    lower_bound: u16,
40    upper_bound: u16,
41}
42
43impl SelectionRange {
44    /// Sets both the start and end bound to the provided index.
45    pub fn at(index: u16) -> Self {
46        Self {
47            lower_bound: index,
48            upper_bound: index,
49        }
50    }
51
52    /// Selects the entire query.
53    pub fn all() -> Self {
54        Self {
55            lower_bound: 0,
56            upper_bound: u16::MAX,
57        }
58    }
59
60    /// Sets the start and end to `0`.
61    pub fn start() -> Self {
62        Self::at(0)
63    }
64
65    pub fn end() -> Self {
66        Self::at(u16::MAX)
67    }
68}