Skip to content

Commit d0a5782

Browse files
committed
new improved syntax to describe ubx protocol packets
1 parent 0b27c35 commit d0a5782

File tree

13 files changed

+1263
-515
lines changed

13 files changed

+1263
-515
lines changed

ublox_derive/Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
[package]
22
name = "ublox_derive"
3-
version = "0.0.0"
3+
version = "0.0.1"
44
authors = ["Lane Kolbly <lane@rscheme.org>"]
55
edition = "2018"
66
publish = false
77

8-
[lib]
9-
proc-macro = true
10-
118
[dependencies]
12-
proc-macro2 = "1.0"
139
quote = "1.0"
14-
syn = { version = "1.0.14", features = ["extra-traits"] }
15-
heck = "0.3.1"
10+
syn = { version = "1.0.14", features = ["extra-traits", "parsing", "full"] }
11+
proc-macro2 = { version = "1.0", features = ["span-locations"] }
12+
log = "0.4"
13+
14+
[dev-dependencies]
15+
which = { version = "3.0", default-features = false }

ublox_derive/src/error.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::{fmt::Write, path::Path};
2+
use syn::Error;
3+
4+
pub fn panic_on_parse_error((src_path, src_cnt): (&Path, &str), err: &Error) -> ! {
5+
let span = err.span();
6+
let start = span.start();
7+
let end = span.end();
8+
9+
let mut code_problem = String::new();
10+
let nlines = end.line - start.line + 1;
11+
for (i, line) in src_cnt
12+
.lines()
13+
.skip(start.line - 1)
14+
.take(nlines)
15+
.enumerate()
16+
{
17+
code_problem.push_str(&line);
18+
code_problem.push('\n');
19+
if i == 0 && start.column > 0 {
20+
write!(&mut code_problem, "{:1$}", ' ', start.column).expect("write to String failed");
21+
}
22+
let code_problem_len = if i == 0 {
23+
if i == nlines - 1 {
24+
end.column - start.column
25+
} else {
26+
line.len() - start.column - 1
27+
}
28+
} else if i != nlines - 1 {
29+
line.len()
30+
} else {
31+
end.column
32+
};
33+
writeln!(&mut code_problem, "{:^^1$}", '^', code_problem_len).expect("Not enought memory");
34+
if i == end.line {
35+
break;
36+
}
37+
}
38+
39+
panic!(
40+
"parsing of {name} failed\nerror: {err}\n{code_problem}\nAt {name}:{line_s}:{col_s}",
41+
name = src_path.display(),
42+
err = err,
43+
code_problem = code_problem,
44+
line_s = start.line,
45+
col_s = start.column,
46+
);
47+
}

ublox_derive/src/file_cache.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/// To prevent modification time changing
2+
use std::{
3+
fs::File,
4+
io,
5+
io::{Read, Write},
6+
path::PathBuf,
7+
};
8+
9+
/// Implement write cache in memory, and update file only if necessary
10+
pub struct FileWriteCache {
11+
cnt: Vec<u8>,
12+
path: PathBuf,
13+
}
14+
15+
impl FileWriteCache {
16+
pub fn new<P: Into<PathBuf>>(p: P) -> FileWriteCache {
17+
let path = p.into();
18+
FileWriteCache { cnt: vec![], path }
19+
}
20+
21+
pub fn update_file_if_necessary(self) -> Result<(), io::Error> {
22+
if let Ok(mut f) = File::open(&self.path) {
23+
let mut cur_cnt = vec![];
24+
f.read_to_end(&mut cur_cnt)?;
25+
if cur_cnt == self.cnt {
26+
return Ok(());
27+
}
28+
}
29+
let mut f = File::create(&self.path)?;
30+
f.write_all(&self.cnt)?;
31+
Ok(())
32+
}
33+
34+
pub fn replace_content(&mut self, bytes: Vec<u8>) {
35+
self.cnt = bytes;
36+
}
37+
}
38+
39+
impl io::Write for FileWriteCache {
40+
fn write(&mut self, data: &[u8]) -> Result<usize, io::Error> {
41+
self.cnt.extend_from_slice(data);
42+
Ok(data.len())
43+
}
44+
fn flush(&mut self) -> Result<(), io::Error> {
45+
Ok(())
46+
}
47+
}

0 commit comments

Comments
 (0)