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
#![deny(missing_docs)]
#![deny(warnings)]
use failure::{bail, Error, Fail};
use nom::IResult;
use std::cell::RefCell;
#[derive(Debug, Fail)]
pub enum RadioError {
#[fail(display = "Parse error: {}", message)]
ParseError {
message: String,
},
#[fail(display = "No data available")]
NoData,
}
fn nom_to_radio_error<T>(err: nom::Err<&[u8]>) -> Result<(&[u8], T), RadioError> {
Err(match err {
nom::Err::Error(nom::simple_errors::Context::Code(_, e)) => RadioError::ParseError {
message: e.description().to_string(),
},
nom::Err::Failure(nom::simple_errors::Context::Code(_, e)) => RadioError::ParseError {
message: e.description().to_string(),
},
nom::Err::Incomplete(_) => RadioError::ParseError {
message: "Incomplete data".to_string(),
},
})
}
pub type RadioResult<T> = Result<T, Error>;
pub type ParseFn<T> = fn(input: &[u8]) -> IResult<&[u8], T>;
pub trait Stream {
fn write(&self, data: &[u8]) -> RadioResult<()>;
fn read(&self) -> RadioResult<Vec<u8>>;
}
pub struct Connection {
stream: Box<Stream + Send>,
buffer: RefCell<Vec<u8>>,
}
impl Connection {
pub fn new(stream: Box<Stream + Send>) -> Connection {
Connection {
stream,
buffer: RefCell::new(Vec::new()),
}
}
pub fn write(&self, data: &[u8]) -> RadioResult<()> {
self.stream.write(data)
}
pub fn read<T>(&self, parse: ParseFn<T>) -> RadioResult<T> {
let mut buffer = self.buffer.borrow_mut();
for _ in 0..5 {
let copy = buffer.clone();
let res = parse(©);
if let Err(nom::Err::Incomplete(_)) = res {
let more = self.stream.read()?;
buffer.extend_from_slice(&more);
continue;
}
let (extra, value) = res.or_else(nom_to_radio_error)?;
buffer.clear();
buffer.extend_from_slice(extra);
return Ok(value);
}
bail!("Incomplete parsing".to_owned())
}
}