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
use std::sync::mpsc;
use failure::{self, Error};
use neovim_lib::{neovim_api::Buffer, Handler, Value};
use event::Event;
pub struct NeovimHandler(pub mpsc::Sender<Event>);
impl NeovimHandler {
pub fn parse_lines_event(
&mut self,
mut args: Vec<Value>,
) -> Result<Event, Error> {
let nea = "Not enough arguments in nvim_buf_lines_event!";
let more = parse_bool(&last_arg(&mut args, nea)?)?;
let linedata = parse_vecstr(last_arg(&mut args, nea)?)?;
let lastline = parse_i64(&last_arg(&mut args, nea)?)?;
let firstline = parse_i64(&last_arg(&mut args, nea)?)?;
let changedtick = parse_u64(&last_arg(&mut args, nea)?)?;
let buf = parse_buf(last_arg(&mut args, nea)?);
Ok(Event::LinesEvent {
buf,
changedtick,
firstline,
lastline,
linedata,
more,
})
}
pub fn parse_changedtick_event(
&mut self,
mut args: Vec<Value>,
) -> Result<Event, Error> {
let nea = "Not enough arguments in nvim_buf_changedtick_event!";
let changedtick = parse_u64(&last_arg(&mut args, nea)?)?;
let buf = parse_buf(last_arg(&mut args, nea)?);
Ok(Event::ChangedTickEvent { buf, changedtick })
}
pub fn parse_detach_event(
&mut self,
mut args: Vec<Value>,
) -> Result<Event, Error> {
let nea = "Not enough arguments in nvim_buf_detach_event!";
let buf = parse_buf(last_arg(&mut args, nea)?);
Ok(Event::DetachEvent { buf })
}
}
impl Handler for NeovimHandler {
fn handle_notify(&mut self, name: &str, args: Vec<Value>) {
match name {
"nvim_buf_lines_event" => {
if let Ok(event) = self.parse_lines_event(args) {
info!("{:?}", event);
if let Err(reason) = self.0.send(event) {
error!("{}", reason);
}
}
}
"nvim_buf_changedtick_event" => {
if let Ok(event) = self.parse_changedtick_event(args) {
info!("{:?}", event);
if let Err(reason) = self.0.send(event) {
error!("{}", reason);
}
}
}
"nvim_buf_detach_event" => {
if let Ok(event) = self.parse_detach_event(args) {
info!("{:?}", event);
if let Err(reason) = self.0.send(event) {
error!("{}", reason);
}
}
}
"RefreshFolds" => {
if let Err(reason) = self.0.send(Event::RefreshFolds) {
error!("{}", reason);
}
}
"quit" => {
if let Err(reason) = self.0.send(Event::Quit) {
error!("{}", reason);
}
}
unknown => {
error!("Receveid unknown event: {}!", unknown);
}
}
}
fn handle_request(
&mut self,
_name: &str,
_args: Vec<Value>,
) -> Result<Value, Value> {
Err(Value::from("not implemented"))
}
}
pub fn last_arg(
v: &mut Vec<Value>,
errmsg: &'static str,
) -> Result<Value, Error> {
v.pop().ok_or_else(|| failure::err_msg(errmsg))
}
pub fn parse_u64(value: &Value) -> Result<u64, Error> {
value
.as_u64()
.ok_or_else(|| failure::err_msg("cannot parse as u64"))
}
pub fn parse_i64(value: &Value) -> Result<i64, Error> {
value
.as_i64()
.ok_or_else(|| failure::err_msg("cannot parse as i64"))
}
pub fn parse_bool(value: &Value) -> Result<bool, Error> {
value
.as_bool()
.ok_or_else(|| failure::err_msg("cannot parse as bool"))
}
pub fn parse_vecstr(value: Value) -> Result<Vec<String>, Error> {
let mut res: Vec<String>;
if let Value::Array(v) = value {
res = Vec::with_capacity(v.len());
for val in v {
if let Value::String(s) = val {
match s.into_str() {
Some(string) => res.push(string),
None => return Err(failure::err_msg("non-utf8 values in array")),
}
} else {
return Err(failure::err_msg("non-String value in array"));
}
}
} else {
return Err(failure::err_msg("cannot parse as array"));
}
Ok(res)
}
pub fn parse_buf(value: Value) -> Buffer {
Buffer::new(value)
}