Skip to content

Commit 5f7edc2

Browse files
committed
Fix clippy
1 parent eb84ef9 commit 5f7edc2

File tree

3 files changed

+63
-49
lines changed

3 files changed

+63
-49
lines changed

src/packet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl From<PacketType> for u16 {
8787
}
8888

8989
impl<'a> Packet<'a> {
90-
pub(crate) fn decode(data: &[u8]) -> Result<Packet> {
90+
pub(crate) fn decode(data: &[u8]) -> Result<Packet<'_>> {
9191
parse_packet(data)
9292
}
9393

src/parse.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::packet::{
66
Error as PacketError, Mode, Opts, Packet, PacketType, RwReq,
77
};
88

9-
pub(crate) fn parse_packet(input: &[u8]) -> Result<Packet> {
9+
pub(crate) fn parse_packet(input: &[u8]) -> Result<Packet<'_>> {
1010
parse_packet_type(input)
1111
.and_then(|(packet_type, data)| match packet_type {
1212
PacketType::Rrq => parse_rrq(data),
@@ -90,7 +90,7 @@ pub(crate) fn parse_opts(mut input: &[u8]) -> Option<Opts> {
9090
Some(opts)
9191
}
9292

93-
fn parse_rrq(input: &[u8]) -> Option<Packet> {
93+
fn parse_rrq(input: &[u8]) -> Option<Packet<'_>> {
9494
let (filename, rest) = parse_nul_str(input)?;
9595
let (mode, rest) = parse_mode(rest)?;
9696
let opts = parse_opts(rest)?;
@@ -102,7 +102,7 @@ fn parse_rrq(input: &[u8]) -> Option<Packet> {
102102
}))
103103
}
104104

105-
fn parse_wrq(input: &[u8]) -> Option<Packet> {
105+
fn parse_wrq(input: &[u8]) -> Option<Packet<'_>> {
106106
let (filename, rest) = parse_nul_str(input)?;
107107
let (mode, rest) = parse_mode(rest)?;
108108
let opts = parse_opts(rest)?;
@@ -114,12 +114,12 @@ fn parse_wrq(input: &[u8]) -> Option<Packet> {
114114
}))
115115
}
116116

117-
fn parse_data(input: &[u8]) -> Option<Packet> {
117+
fn parse_data(input: &[u8]) -> Option<Packet<'_>> {
118118
let (block_nr, rest) = parse_u16_be(input)?;
119119
Some(Packet::Data(block_nr, rest))
120120
}
121121

122-
fn parse_ack(input: &[u8]) -> Option<Packet> {
122+
fn parse_ack(input: &[u8]) -> Option<Packet<'_>> {
123123
let (block_nr, rest) = parse_u16_be(input)?;
124124

125125
if !rest.is_empty() {
@@ -129,7 +129,7 @@ fn parse_ack(input: &[u8]) -> Option<Packet> {
129129
Some(Packet::Ack(block_nr))
130130
}
131131

132-
fn parse_error(input: &[u8]) -> Option<Packet> {
132+
fn parse_error(input: &[u8]) -> Option<Packet<'_>> {
133133
let (code, rest) = parse_u16_be(input)?;
134134
let (msg, rest) = parse_nul_str(rest)?;
135135

@@ -140,7 +140,7 @@ fn parse_error(input: &[u8]) -> Option<Packet> {
140140
Some(Packet::Error(PacketError::from_code(code, Some(msg))))
141141
}
142142

143-
fn parse_oack(input: &[u8]) -> Option<Packet> {
143+
fn parse_oack(input: &[u8]) -> Option<Packet<'_>> {
144144
let opts = parse_opts(input)?;
145145
Some(Packet::OAck(opts))
146146
}

src/tests/packet.rs

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn packet_to_bytes(packet: &Packet) -> Bytes {
1212

1313
#[test]
1414
fn check_rrq() {
15-
let packet = Packet::decode(b"\x00\x01abc\0netascii\0");
15+
let packet = Packet::decode(b"\x00\x01abc\x00netascii\x00");
1616

1717
assert!(matches!(packet, Ok(Packet::Rrq(ref req))
1818
if req == &RwReq {
@@ -24,10 +24,10 @@ fn check_rrq() {
2424

2525
assert_eq!(
2626
packet_to_bytes(&packet.unwrap()),
27-
b"\x00\x01abc\0netascii\0"[..]
27+
b"\x00\x01abc\x00netascii\x00"[..]
2828
);
2929

30-
let packet = Packet::decode(b"\x00\x01abc\0netascII\0");
30+
let packet = Packet::decode(b"\x00\x01abc\x00netascII\x00");
3131

3232
assert!(matches!(packet, Ok(Packet::Rrq(ref req))
3333
if req == &RwReq {
@@ -39,20 +39,20 @@ fn check_rrq() {
3939

4040
assert_eq!(
4141
packet_to_bytes(&packet.unwrap()),
42-
b"\x00\x01abc\0netascii\0"[..]
42+
b"\x00\x01abc\x00netascii\x00"[..]
4343
);
4444

45-
let packet = Packet::decode(b"\x00\x01abc\0netascii\0more");
45+
let packet = Packet::decode(b"\x00\x01abc\x00netascii\x00more");
4646
assert!(matches!(packet, Err(ref e) if matches!(e, Error::InvalidPacket)));
4747

48-
let packet = Packet::decode(b"\x00\x01abc\0netascii");
48+
let packet = Packet::decode(b"\x00\x01abc\x00netascii");
4949
assert!(matches!(packet, Err(ref e) if matches!(e, Error::InvalidPacket)));
5050

51-
let packet = Packet::decode(b"\x00\x01abc\0netascXX\0");
51+
let packet = Packet::decode(b"\x00\x01abc\x00netascXX\x00");
5252
assert!(matches!(packet, Err(ref e) if matches!(e, Error::InvalidPacket)));
5353

5454
let packet = Packet::decode(
55-
b"\x00\x01abc\0netascii\0blksize\0123\0timeout\03\0tsize\05556\0",
55+
b"\x00\x01abc\x00netascii\x00blksize\x00123\x00timeout\x003\x00tsize\x005556\x00",
5656
);
5757

5858
assert!(matches!(packet, Ok(Packet::Rrq(ref req))
@@ -70,15 +70,16 @@ fn check_rrq() {
7070

7171
assert_eq!(
7272
packet_to_bytes(&packet.unwrap()),
73-
b"\x00\x01abc\0netascii\0blksize\0123\0timeout\03\0tsize\05556\0"[..]
73+
b"\x00\x01abc\x00netascii\x00blksize\x00123\x00timeout\x003\x00tsize\x005556\x00"[..]
7474
);
7575

7676
let packet = Packet::decode(
77-
b"\x00\x01abc\0netascii\0blksize\0123\0timeout\03\0tsize\0",
77+
b"\x00\x01abc\x00netascii\x00blksize\x00123\x00timeout\x003\x00tsize\x00",
7878
);
7979
assert!(matches!(packet, Err(ref e) if matches!(e, Error::InvalidPacket)));
8080

81-
let packet = Packet::decode(b"\x00\x01abc\0netascii\0blksizeX\0123\0");
81+
let packet =
82+
Packet::decode(b"\x00\x01abc\x00netascii\x00blksizeX\x00123\x00");
8283
assert!(matches!(packet, Ok(Packet::Rrq(ref req))
8384
if req == &RwReq {
8485
filename: "abc".to_string(),
@@ -90,7 +91,7 @@ fn check_rrq() {
9091

9192
#[test]
9293
fn check_wrq() {
93-
let packet = Packet::decode(b"\x00\x02abc\0octet\0");
94+
let packet = Packet::decode(b"\x00\x02abc\x00octet\x00");
9495

9596
assert!(matches!(packet, Ok(Packet::Wrq(ref req))
9697
if req == &RwReq {
@@ -100,9 +101,12 @@ fn check_wrq() {
100101
}
101102
));
102103

103-
assert_eq!(packet_to_bytes(&packet.unwrap()), b"\x00\x02abc\0octet\0"[..]);
104+
assert_eq!(
105+
packet_to_bytes(&packet.unwrap()),
106+
b"\x00\x02abc\x00octet\x00"[..]
107+
);
104108

105-
let packet = Packet::decode(b"\x00\x02abc\0OCTet\0");
109+
let packet = Packet::decode(b"\x00\x02abc\x00OCTet\x00");
106110

107111
assert!(matches!(packet, Ok(Packet::Wrq(ref req))
108112
if req == &RwReq {
@@ -112,19 +116,22 @@ fn check_wrq() {
112116
}
113117
));
114118

115-
assert_eq!(packet_to_bytes(&packet.unwrap()), b"\x00\x02abc\0octet\0"[..]);
119+
assert_eq!(
120+
packet_to_bytes(&packet.unwrap()),
121+
b"\x00\x02abc\x00octet\x00"[..]
122+
);
116123

117-
let packet = Packet::decode(b"\x00\x02abc\0octet\0more");
124+
let packet = Packet::decode(b"\x00\x02abc\x00octet\x00more");
118125
assert!(matches!(packet, Err(ref e) if matches!(e, Error::InvalidPacket)));
119126

120-
let packet = Packet::decode(b"\x00\x02abc\0octet");
127+
let packet = Packet::decode(b"\x00\x02abc\x00octet");
121128
assert!(matches!(packet, Err(ref e) if matches!(e, Error::InvalidPacket)));
122129

123-
let packet = Packet::decode(b"\x00\x02abc\0octex\0");
130+
let packet = Packet::decode(b"\x00\x02abc\x00octex\x00");
124131
assert!(matches!(packet, Err(ref e) if matches!(e, Error::InvalidPacket)));
125132

126133
let packet = Packet::decode(
127-
b"\x00\x02abc\0octet\0blksize\0123\0timeout\03\0tsize\05556\0windowsize\04\0",
134+
b"\x00\x02abc\x00octet\x00blksize\x00123\x00timeout\x003\x00tsize\x005556\x00windowsize\x004\x00",
128135
);
129136

130137
assert!(matches!(packet, Ok(Packet::Wrq(ref req))
@@ -142,10 +149,10 @@ fn check_wrq() {
142149

143150
assert_eq!(
144151
packet_to_bytes(&packet.unwrap()),
145-
b"\x00\x02abc\0octet\0blksize\0123\0timeout\03\0tsize\05556\0windowsize\04\0"[..]
152+
b"\x00\x02abc\x00octet\x00blksize\x00123\x00timeout\x003\x00tsize\x005556\x00windowsize\x004\x00"[..]
146153
);
147154

148-
let packet = Packet::decode(b"\x00\x02abc\0octet\0blksizeX\0123\0");
155+
let packet = Packet::decode(b"\x00\x02abc\x00octet\x00blksizeX\x00123\x00");
149156
assert!(matches!(packet, Ok(Packet::Wrq(ref req))
150157
if req == &RwReq {
151158
filename: "abc".to_string(),
@@ -184,25 +191,31 @@ fn check_ack() {
184191

185192
#[test]
186193
fn check_error() {
187-
let packet = Packet::decode(b"\x00\x05\x00\x01msg\0");
194+
let packet = Packet::decode(b"\x00\x05\x00\x01msg\x00");
188195
assert!(matches!(packet, Ok(Packet::Error(packet::Error::FileNotFound))));
189196
assert_eq!(
190197
packet_to_bytes(&packet.unwrap()),
191-
b"\x00\x05\x00\x01File not found\0"[..]
198+
b"\x00\x05\x00\x01File not found\x00"[..]
192199
);
193200

194201
// 0x10 is unknown error code an will be treated as 0
195-
let packet = Packet::decode(b"\x00\x05\x00\x10msg\0");
202+
let packet = Packet::decode(b"\x00\x05\x00\x10msg\x00");
196203
assert!(matches!(packet, Ok(Packet::Error(packet::Error::Msg(ref errmsg)))
197204
if errmsg == "msg"));
198-
assert_eq!(packet_to_bytes(&packet.unwrap()), b"\x00\x05\x00\x00msg\0"[..]);
205+
assert_eq!(
206+
packet_to_bytes(&packet.unwrap()),
207+
b"\x00\x05\x00\x00msg\x00"[..]
208+
);
199209

200-
let packet = Packet::decode(b"\x00\x05\x00\x00msg\0");
210+
let packet = Packet::decode(b"\x00\x05\x00\x00msg\x00");
201211
assert!(matches!(packet, Ok(Packet::Error(packet::Error::Msg(ref errmsg)))
202212
if errmsg == "msg"));
203-
assert_eq!(packet_to_bytes(&packet.unwrap()), b"\x00\x05\x00\x00msg\0"[..]);
213+
assert_eq!(
214+
packet_to_bytes(&packet.unwrap()),
215+
b"\x00\x05\x00\x00msg\x00"[..]
216+
);
204217

205-
let packet = Packet::decode(b"\x00\x05\x00\x00msg\0more");
218+
let packet = Packet::decode(b"\x00\x05\x00\x00msg\x00more");
206219
assert!(matches!(packet, Err(ref e) if matches!(e, Error::InvalidPacket)));
207220

208221
let packet = Packet::decode(b"\x00\x05\x00\x00msg");
@@ -219,32 +232,33 @@ fn check_oack() {
219232
matches!(packet, Ok(Packet::OAck(ref opts)) if opts == &Opts::default())
220233
);
221234

222-
let packet = Packet::decode(b"\x00\x06blksize\0123\0");
235+
let packet = Packet::decode(b"\x00\x06blksize\x00123\x00");
223236
assert!(matches!(packet, Ok(Packet::OAck(ref opts))
224237
if opts == &Opts {
225238
block_size: Some(123),
226239
..Default::default()
227240
}
228241
));
229242

230-
let packet = Packet::decode(b"\x00\x06timeout\03\0");
243+
let packet = Packet::decode(b"\x00\x06timeout\x003\x00");
231244
assert!(matches!(packet, Ok(Packet::OAck(ref opts))
232245
if opts == &Opts {
233246
timeout: Some(3),
234247
..Default::default()
235248
}
236249
));
237250

238-
let packet = Packet::decode(b"\x00\x06tsize\05556\0");
251+
let packet = Packet::decode(b"\x00\x06tsize\x005556\x00");
239252
assert!(matches!(packet, Ok(Packet::OAck(ref opts))
240253
if opts == &Opts {
241254
transfer_size: Some(5556),
242255
..Default::default()
243256
}
244257
));
245258

246-
let packet =
247-
Packet::decode(b"\x00\x06tsize\05556\0blksize\0123\0timeout\03\0");
259+
let packet = Packet::decode(
260+
b"\x00\x06tsize\x005556\x00blksize\x00123\x00timeout\x003\x00",
261+
);
248262
assert!(matches!(packet, Ok(Packet::OAck(ref opts))
249263
if opts == &Opts {
250264
block_size: Some(123),
@@ -266,7 +280,7 @@ fn check_packet() {
266280

267281
#[test]
268282
fn check_blksize_boundaries() {
269-
let opts = parse_opts(b"blksize\07\0").unwrap();
283+
let opts = parse_opts(b"blksize\x007\x00").unwrap();
270284
assert_eq!(
271285
opts,
272286
Opts {
@@ -275,7 +289,7 @@ fn check_blksize_boundaries() {
275289
}
276290
);
277291

278-
let opts = parse_opts(b"blksize\08\0").unwrap();
292+
let opts = parse_opts(b"blksize\x008\x00").unwrap();
279293
assert_eq!(
280294
opts,
281295
Opts {
@@ -284,7 +298,7 @@ fn check_blksize_boundaries() {
284298
}
285299
);
286300

287-
let opts = parse_opts(b"blksize\065464\0").unwrap();
301+
let opts = parse_opts(b"blksize\x0065464\x00").unwrap();
288302
assert_eq!(
289303
opts,
290304
Opts {
@@ -293,7 +307,7 @@ fn check_blksize_boundaries() {
293307
}
294308
);
295309

296-
let opts = parse_opts(b"blksize\065465\0").unwrap();
310+
let opts = parse_opts(b"blksize\x0065465\x00").unwrap();
297311
assert_eq!(
298312
opts,
299313
Opts {
@@ -305,7 +319,7 @@ fn check_blksize_boundaries() {
305319

306320
#[test]
307321
fn check_timeout_boundaries() {
308-
let opts = parse_opts(b"timeout\00\0").unwrap();
322+
let opts = parse_opts(b"timeout\x000\x00").unwrap();
309323
assert_eq!(
310324
opts,
311325
Opts {
@@ -314,7 +328,7 @@ fn check_timeout_boundaries() {
314328
}
315329
);
316330

317-
let opts = parse_opts(b"timeout\01\0").unwrap();
331+
let opts = parse_opts(b"timeout\x001\x00").unwrap();
318332
assert_eq!(
319333
opts,
320334
Opts {
@@ -323,7 +337,7 @@ fn check_timeout_boundaries() {
323337
}
324338
);
325339

326-
let opts = parse_opts(b"timeout\0255\0").unwrap();
340+
let opts = parse_opts(b"timeout\x00255\x00").unwrap();
327341
assert_eq!(
328342
opts,
329343
Opts {
@@ -332,7 +346,7 @@ fn check_timeout_boundaries() {
332346
}
333347
);
334348

335-
let opts = parse_opts(b"timeout\0256\0").unwrap();
349+
let opts = parse_opts(b"timeout\x00256\x00").unwrap();
336350
assert_eq!(
337351
opts,
338352
Opts {

0 commit comments

Comments
 (0)