1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use std::{
convert::TryInto,
fmt,
io::{self, BufRead, Write},
};
#[allow(dead_code)]
mod shellac_capnp {
include!(concat!(env!("OUT_DIR"), "/shellac_capnp.rs"));
}
use capnp::message::ReaderOptions;
use serde::{Deserialize, Serialize};
#[derive(Debug)]
pub enum Error {
WordOutOfRange { word: u16, argv_len: u32 },
Capnp(capnp::Error),
NotInSchema(capnp::NotInSchema),
}
impl From<capnp::NotInSchema> for Error {
fn from(cause: capnp::NotInSchema) -> Self { Error::NotInSchema(cause) }
}
impl From<capnp::Error> for Error {
fn from(cause: capnp::Error) -> Self { Error::Capnp(cause) }
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::WordOutOfRange { word, argv_len } => write!(
f,
"the word {} can't be autocompleted because it is out of bound for argc = {}",
word, argv_len
),
Error::Capnp(e) => write!(f, "{}", e),
Error::NotInSchema(e) => write!(f, "{}", e),
}
}
}
#[derive(Default, Clone, Debug, Hash, PartialEq, Eq, Deserialize)]
pub struct AutocompRequest {
argv: Vec<String>,
word: u16,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize)]
pub struct Reply<T> {
pub choices: Vec<Suggestion<T>>,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize)]
pub enum SuggestionType<T> {
Literal(T),
Command { command: Vec<T>, prefix: T },
}
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize)]
pub struct Suggestion<T> {
suggestion: SuggestionType<T>,
description: T,
}
impl AutocompRequest {
pub fn new(argv: Vec<String>, word: u16) -> Self {
if word as usize >= argv.len() {
eprintln!(
"Word {} is out of bound for argv '{:?}' in ShellAC autocompletion request",
word, argv
);
panic!();
}
Self { argv, word }
}
}
impl<T> Suggestion<T> {
pub const fn new(suggestion: SuggestionType<T>, description: T) -> Self {
Self { suggestion, description }
}
pub const fn description(&self) -> &T { &self.description }
pub const fn suggestion(&self) -> &SuggestionType<T> { &self.suggestion }
}
type Out<'a, E> = std::iter::Map<
capnp::traits::ListIter<
capnp::struct_list::Reader<'a, shellac_capnp::suggestion::Owned>,
shellac_capnp::suggestion::Reader<'a>,
>,
fn(shellac_capnp::suggestion::Reader<'a>) -> Result<(SuggestionType<&'a str>, &'a str), E>,
>;
fn convert<T: From<capnp::Error> + From<capnp::NotInSchema>>(
choice: shellac_capnp::suggestion::Reader,
) -> Result<(SuggestionType<&str>, &str), T> {
Ok((
match choice.get_arg().which()? {
shellac_capnp::suggestion::arg::Which::Literal(lit) => SuggestionType::Literal(lit?),
shellac_capnp::suggestion::arg::Which::Command(cmd) => {
let cmd = cmd?;
let prefix = cmd.get_prefix()?;
let command = cmd.get_args()?.iter().collect::<Result<Vec<_>, _>>()?;
SuggestionType::Command { command, prefix }
}
},
choice.get_description()?,
))
}
pub fn read_reply<R, T, E, F>(reader: &mut R, f: F) -> Result<T, E>
where
E: From<capnp::Error> + From<capnp::NotInSchema>,
R: BufRead,
F: FnOnce(Out<'_, E>) -> Result<T, E>,
{
let request = capnp::serialize_packed::read_message(reader, ReaderOptions::default())?;
let choices = request.get_root::<shellac_capnp::response::Reader>()?.get_choices()?;
f(choices.iter().map(convert))
}
pub fn write_request<W: Write>(writer: &mut W, input: &AutocompRequest) -> Result<(), io::Error> {
let mut message = capnp::message::Builder::new_default();
let mut output = message.init_root::<shellac_capnp::request::Builder>();
output.set_word(input.word);
let len = input.argv.len().try_into().expect("Too many output choices");
let mut reply_argv = output.init_argv(len);
for (i, arg) in input.argv.iter().enumerate() {
reply_argv.reborrow().set(i as u32, arg);
}
capnp::serialize_packed::write_message(writer, &message)
}