covey_plugin/
input.rs

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