Skip to content

Commit 74946f3

Browse files
committed
Make everything tabs
Indenting with spaces is silly and I will not be taking feedback on this
1 parent d951e7e commit 74946f3

15 files changed

+1169
-1153
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 4
6+
tab_width = 4
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
# Tragically, yaml gonna yaml
13+
[*.{yml,yaml}]
14+
indent_style = space
15+
indent_size = 2

.rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hard_tabs = true

src/bin/test_parse.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@ use std::error;
77
use winnow::Parser;
88

99
fn do_file(fname: &str) -> Result<(), Box<dyn error::Error>> {
10-
let data = std::fs::read(fname).map_err(Box::new)?;
10+
let data = std::fs::read(fname).map_err(Box::new)?;
1111

12-
let packet: Packet = parse_packet.parse(&data[..]).map_err(|e| e.to_string())?;
13-
println!("{packet:?}");
12+
let packet: Packet = parse_packet.parse(&data[..]).map_err(|e| e.to_string())?;
13+
println!("{packet:?}");
1414

15-
match packet {
16-
Packet::Long { mut data, .. } => {
17-
let ci = CICode::parse.parse_next(&mut data);
18-
println!("{ci:?}");
19-
}
20-
_ => todo!(),
21-
}
22-
Ok(())
15+
match packet {
16+
Packet::Long { mut data, .. } => {
17+
let ci = CICode::parse.parse_next(&mut data);
18+
println!("{ci:?}");
19+
}
20+
_ => todo!(),
21+
}
22+
Ok(())
2323
}
2424

2525
fn main() {
26-
for fname in std::env::args().skip(1) {
27-
println!("Trying to load file {}", fname);
28-
let res = do_file(&fname);
29-
match res {
30-
Ok(_) => println!("Yay"),
31-
Err(e) => eprintln!("Oh no: {}", e),
32-
}
33-
}
34-
// honk
26+
for fname in std::env::args().skip(1) {
27+
println!("Trying to load file {}", fname);
28+
let res = do_file(&fname);
29+
match res {
30+
Ok(_) => println!("Yay"),
31+
Err(e) => eprintln!("Oh no: {}", e),
32+
}
33+
}
34+
// honk
3535
}

src/parse.rs

+95-95
Original file line numberDiff line numberDiff line change
@@ -12,109 +12,109 @@ use crate::parse::error::{ParseError, Result};
1212

1313
#[allow(dead_code)]
1414
pub struct Datagram {
15-
data: Vec<u8>,
16-
index: usize,
17-
/// IEC 60780 control field
18-
packet_control: u8,
19-
/// IEC 60780 address field
20-
address: u8,
21-
/// IEC 13757 (mbus) control information (CI) field
22-
mbus_control: u8,
15+
data: Vec<u8>,
16+
index: usize,
17+
/// IEC 60780 control field
18+
packet_control: u8,
19+
/// IEC 60780 address field
20+
address: u8,
21+
/// IEC 13757 (mbus) control information (CI) field
22+
mbus_control: u8,
2323
}
2424

2525
impl Datagram {
26-
pub fn new(data: Vec<u8>, packet_control: u8, address: u8, mbus_control: u8) -> Self {
27-
Self {
28-
data,
29-
index: 0,
30-
packet_control,
31-
address,
32-
mbus_control,
33-
}
34-
}
26+
pub fn new(data: Vec<u8>, packet_control: u8, address: u8, mbus_control: u8) -> Self {
27+
Self {
28+
data,
29+
index: 0,
30+
packet_control,
31+
address,
32+
mbus_control,
33+
}
34+
}
3535

36-
pub fn parse(data: Vec<u8>) -> Result<Self> {
37-
let len = data.len();
38-
if len == 0 {
39-
return Err(ParseError::InvalidPacket("Packet is empty"));
40-
}
41-
let start1 = data[0];
42-
match start1 {
43-
0x68 => (),
44-
// TODO: Figure out where these are defined and why libmbus supports them,
45-
// because they're not in IEC 60870-5-2
46-
0xE5 => todo!("ACK packets aren't supported yet"),
47-
0x10 => todo!("Short packets aren't supported yet"),
48-
_ => return Err(ParseError::InvalidPacket("Start byte is invalid")),
49-
}
36+
pub fn parse(data: Vec<u8>) -> Result<Self> {
37+
let len = data.len();
38+
if len == 0 {
39+
return Err(ParseError::InvalidPacket("Packet is empty"));
40+
}
41+
let start1 = data[0];
42+
match start1 {
43+
0x68 => (),
44+
// TODO: Figure out where these are defined and why libmbus supports them,
45+
// because they're not in IEC 60870-5-2
46+
0xE5 => todo!("ACK packets aren't supported yet"),
47+
0x10 => todo!("Short packets aren't supported yet"),
48+
_ => return Err(ParseError::InvalidPacket("Start byte is invalid")),
49+
}
5050

51-
let [_, length1, length2, start2, packet_control, address, mbus_control, .., checksum, end] =
52-
data[..]
53-
else {
54-
return Err(ParseError::InvalidPacket("Packet is too short"));
55-
};
56-
if start1 != 0x68 {
57-
return Err(ParseError::InvalidPacket("Start byte is invalid"));
58-
} else if length1 != length2 {
59-
return Err(ParseError::InvalidPacket("Lengths don't match"));
60-
} else if length1 as usize != len - 6 {
61-
return Err(ParseError::InvalidPacket("Packet length incorrect"));
62-
} else if start2 != 0x68 {
63-
return Err(ParseError::InvalidPacket("Second start byte is invalid"));
64-
} else if checksum
65-
!= data
66-
.iter()
67-
.skip(4)
68-
.take(length1 as usize)
69-
.copied()
70-
.reduce(u8::wrapping_add)
71-
.unwrap_or(0)
72-
{
73-
return Err(ParseError::InvalidPacket("Checksum doesn't match"));
74-
} else if end != 0x16 {
75-
return Err(ParseError::InvalidPacket("End byte is invalid"));
76-
}
77-
Ok(Self::new(
78-
(data[6..len - 2]).into(),
79-
packet_control,
80-
address,
81-
mbus_control,
82-
))
83-
}
51+
let [_, length1, length2, start2, packet_control, address, mbus_control, .., checksum, end] =
52+
data[..]
53+
else {
54+
return Err(ParseError::InvalidPacket("Packet is too short"));
55+
};
56+
if start1 != 0x68 {
57+
return Err(ParseError::InvalidPacket("Start byte is invalid"));
58+
} else if length1 != length2 {
59+
return Err(ParseError::InvalidPacket("Lengths don't match"));
60+
} else if length1 as usize != len - 6 {
61+
return Err(ParseError::InvalidPacket("Packet length incorrect"));
62+
} else if start2 != 0x68 {
63+
return Err(ParseError::InvalidPacket("Second start byte is invalid"));
64+
} else if checksum
65+
!= data
66+
.iter()
67+
.skip(4)
68+
.take(length1 as usize)
69+
.copied()
70+
.reduce(u8::wrapping_add)
71+
.unwrap_or(0)
72+
{
73+
return Err(ParseError::InvalidPacket("Checksum doesn't match"));
74+
} else if end != 0x16 {
75+
return Err(ParseError::InvalidPacket("End byte is invalid"));
76+
}
77+
Ok(Self::new(
78+
(data[6..len - 2]).into(),
79+
packet_control,
80+
address,
81+
mbus_control,
82+
))
83+
}
8484

85-
pub fn get_byte(&self, index: usize) -> Result<u8> {
86-
self.data
87-
.get(index)
88-
.copied()
89-
.ok_or(ParseError::UnexpectedEOF)
90-
}
85+
pub fn get_byte(&self, index: usize) -> Result<u8> {
86+
self.data
87+
.get(index)
88+
.copied()
89+
.ok_or(ParseError::UnexpectedEOF)
90+
}
9191

92-
pub fn peek(&self) -> Result<u8> {
93-
self.get_byte(self.index)
94-
}
92+
pub fn peek(&self) -> Result<u8> {
93+
self.get_byte(self.index)
94+
}
9595

96-
pub fn last_byte(&self) -> Result<u8> {
97-
if self.index > 0 {
98-
self.get_byte(self.index - 1)
99-
} else {
100-
Err(ParseError::UnexpectedEOF)
101-
}
102-
}
96+
pub fn last_byte(&self) -> Result<u8> {
97+
if self.index > 0 {
98+
self.get_byte(self.index - 1)
99+
} else {
100+
Err(ParseError::UnexpectedEOF)
101+
}
102+
}
103103

104-
pub fn next_byte(&mut self) -> Result<u8> {
105-
let ret = self.get_byte(self.index);
106-
if ret.is_ok() {
107-
self.index += 1;
108-
}
109-
ret
110-
}
104+
pub fn next_byte(&mut self) -> Result<u8> {
105+
let ret = self.get_byte(self.index);
106+
if ret.is_ok() {
107+
self.index += 1;
108+
}
109+
ret
110+
}
111111

112-
pub fn take(&mut self, n: usize) -> Result<Vec<u8>> {
113-
if self.index + n >= self.data.len() {
114-
return Err(ParseError::UnexpectedEOF);
115-
}
116-
let start = self.index;
117-
self.index += n;
118-
Ok(self.data[start..self.index].into())
119-
}
112+
pub fn take(&mut self, n: usize) -> Result<Vec<u8>> {
113+
if self.index + n >= self.data.len() {
114+
return Err(ParseError::UnexpectedEOF);
115+
}
116+
let start = self.index;
117+
self.index += n;
118+
Ok(self.data[start..self.index].into())
119+
}
120120
}

0 commit comments

Comments
 (0)