1use crate::protocol::Error;
2use embedded_io::Error as _;
3
4pub trait ReadBytesExt {
9 fn read_bytes(&mut self, buf: &mut [u8]) -> Result<(), Error>;
14
15 fn read_u8(&mut self) -> Result<u8, Error> {
20 let mut buf = [0u8; 1];
21 self.read_bytes(&mut buf)?;
22 Ok(buf[0])
23 }
24
25 fn read_i8(&mut self) -> Result<i8, Error> {
30 self.read_u8().map(u8::cast_signed)
31 }
32
33 fn read_u16_be(&mut self) -> Result<u16, Error> {
38 let mut buf = [0u8; 2];
39 self.read_bytes(&mut buf)?;
40 Ok(u16::from_be_bytes(buf))
41 }
42
43 fn read_i16_be(&mut self) -> Result<i16, Error> {
48 let mut buf = [0u8; 2];
49 self.read_bytes(&mut buf)?;
50 Ok(i16::from_be_bytes(buf))
51 }
52
53 fn read_u24_be(&mut self) -> Result<u32, Error> {
58 let mut buf = [0u8; 3];
59 self.read_bytes(&mut buf)?;
60 Ok(u32::from_be_bytes([0, buf[0], buf[1], buf[2]]))
61 }
62
63 fn read_u32_be(&mut self) -> Result<u32, Error> {
68 let mut buf = [0u8; 4];
69 self.read_bytes(&mut buf)?;
70 Ok(u32::from_be_bytes(buf))
71 }
72
73 fn read_i32_be(&mut self) -> Result<i32, Error> {
78 let mut buf = [0u8; 4];
79 self.read_bytes(&mut buf)?;
80 Ok(i32::from_be_bytes(buf))
81 }
82
83 fn read_u64_be(&mut self) -> Result<u64, Error> {
88 let mut buf = [0u8; 8];
89 self.read_bytes(&mut buf)?;
90 Ok(u64::from_be_bytes(buf))
91 }
92
93 fn read_i64_be(&mut self) -> Result<i64, Error> {
98 let mut buf = [0u8; 8];
99 self.read_bytes(&mut buf)?;
100 Ok(i64::from_be_bytes(buf))
101 }
102
103 fn read_u128_be(&mut self) -> Result<u128, Error> {
108 let mut buf = [0u8; 16];
109 self.read_bytes(&mut buf)?;
110 Ok(u128::from_be_bytes(buf))
111 }
112
113 fn read_i128_be(&mut self) -> Result<i128, Error> {
118 let mut buf = [0u8; 16];
119 self.read_bytes(&mut buf)?;
120 Ok(i128::from_be_bytes(buf))
121 }
122
123 fn read_f32_be(&mut self) -> Result<f32, Error> {
128 let mut buf = [0u8; 4];
129 self.read_bytes(&mut buf)?;
130 Ok(f32::from_be_bytes(buf))
131 }
132
133 fn read_f64_be(&mut self) -> Result<f64, Error> {
138 let mut buf = [0u8; 8];
139 self.read_bytes(&mut buf)?;
140 Ok(f64::from_be_bytes(buf))
141 }
142}
143
144impl<T: embedded_io::Read> ReadBytesExt for T {
145 fn read_bytes(&mut self, buf: &mut [u8]) -> Result<(), Error> {
146 self.read_exact(buf).map_err(|e| match e {
147 embedded_io::ReadExactError::UnexpectedEof => Error::Io(embedded_io::ErrorKind::Other),
148 embedded_io::ReadExactError::Other(e) => Error::Io(e.kind()),
149 })
150 }
151}
152
153pub trait WriteBytesExt {
158 fn write_bytes(&mut self, buf: &[u8]) -> Result<(), Error>;
163
164 fn write_u8(&mut self, val: u8) -> Result<(), Error> {
169 self.write_bytes(&[val])
170 }
171
172 fn write_i8(&mut self, val: i8) -> Result<(), Error> {
177 self.write_bytes(&[val.cast_unsigned()])
178 }
179
180 fn write_u16_be(&mut self, val: u16) -> Result<(), Error> {
185 self.write_bytes(&val.to_be_bytes())
186 }
187
188 fn write_i16_be(&mut self, val: i16) -> Result<(), Error> {
193 self.write_bytes(&val.to_be_bytes())
194 }
195
196 fn write_u24_be(&mut self, val: u32) -> Result<(), Error> {
201 self.write_bytes(&val.to_be_bytes()[1..])
202 }
203
204 fn write_u32_be(&mut self, val: u32) -> Result<(), Error> {
209 self.write_bytes(&val.to_be_bytes())
210 }
211
212 fn write_i32_be(&mut self, val: i32) -> Result<(), Error> {
217 self.write_bytes(&val.to_be_bytes())
218 }
219
220 fn write_u64_be(&mut self, val: u64) -> Result<(), Error> {
225 self.write_bytes(&val.to_be_bytes())
226 }
227
228 fn write_i64_be(&mut self, val: i64) -> Result<(), Error> {
233 self.write_bytes(&val.to_be_bytes())
234 }
235
236 fn write_u128_be(&mut self, val: u128) -> Result<(), Error> {
241 self.write_bytes(&val.to_be_bytes())
242 }
243
244 fn write_i128_be(&mut self, val: i128) -> Result<(), Error> {
249 self.write_bytes(&val.to_be_bytes())
250 }
251
252 fn write_f32_be(&mut self, val: f32) -> Result<(), Error> {
257 self.write_bytes(&val.to_be_bytes())
258 }
259
260 fn write_f64_be(&mut self, val: f64) -> Result<(), Error> {
265 self.write_bytes(&val.to_be_bytes())
266 }
267}
268
269impl<T: embedded_io::Write> WriteBytesExt for T {
270 fn write_bytes(&mut self, buf: &[u8]) -> Result<(), Error> {
271 self.write_all(buf).map_err(|e| Error::Io(e.kind()))
272 }
273}
274
275#[cfg(test)]
276mod tests {
277 use super::*;
278
279 struct FailingWriter;
280
281 impl embedded_io::ErrorType for FailingWriter {
282 type Error = embedded_io::ErrorKind;
283 }
284
285 impl embedded_io::Write for FailingWriter {
286 fn write(&mut self, _buf: &[u8]) -> Result<usize, Self::Error> {
287 Err(embedded_io::ErrorKind::BrokenPipe)
288 }
289
290 fn flush(&mut self) -> Result<(), Self::Error> {
291 Ok(())
292 }
293 }
294
295 struct FailingReader;
296
297 impl embedded_io::ErrorType for FailingReader {
298 type Error = embedded_io::ErrorKind;
299 }
300
301 impl embedded_io::Read for FailingReader {
302 fn read(&mut self, _buf: &mut [u8]) -> Result<usize, Self::Error> {
303 Err(embedded_io::ErrorKind::BrokenPipe)
304 }
305 }
306
307 #[test]
310 fn write_io_error_maps_to_error_io() {
311 assert!(matches!(
312 FailingWriter.write_u8(0),
313 Err(Error::Io(embedded_io::ErrorKind::BrokenPipe))
314 ));
315 }
316
317 #[test]
318 fn read_io_error_maps_to_error_io() {
319 assert!(matches!(
320 FailingReader.read_u8(),
321 Err(Error::Io(embedded_io::ErrorKind::BrokenPipe))
322 ));
323 }
324
325 #[test]
328 fn read_u8_decodes_correctly() {
329 let buf: &[u8] = &[0xAB];
330 assert_eq!((&mut &*buf).read_u8().unwrap(), 0xAB);
331 }
332
333 #[test]
334 fn read_i8_decodes_correctly() {
335 let buf: &[u8] = &[0xFF];
336 assert_eq!((&mut &*buf).read_i8().unwrap(), -1);
337 }
338
339 #[test]
340 fn read_u16_be_decodes_correctly() {
341 let buf: &[u8] = &[0x01, 0x02];
342 assert_eq!((&mut &*buf).read_u16_be().unwrap(), 0x0102);
343 }
344
345 #[test]
346 fn read_i16_be_decodes_correctly() {
347 let buf: &[u8] = &[0xFF, 0xFE];
348 assert_eq!((&mut &*buf).read_i16_be().unwrap(), -2);
349 }
350
351 #[test]
352 fn read_u24_be_decodes_correctly() {
353 let buf: &[u8] = &[0x01, 0x02, 0x03];
354 assert_eq!((&mut &*buf).read_u24_be().unwrap(), 0x0001_0203);
355 }
356
357 #[test]
358 fn read_u32_be_decodes_correctly() {
359 let buf: &[u8] = &[0x01, 0x02, 0x03, 0x04];
360 assert_eq!((&mut &*buf).read_u32_be().unwrap(), 0x0102_0304);
361 }
362
363 #[test]
364 fn read_i32_be_decodes_correctly() {
365 let buf: &[u8] = &[0xFF, 0xFF, 0xFF, 0xFE];
366 assert_eq!((&mut &*buf).read_i32_be().unwrap(), -2);
367 }
368
369 #[test]
370 fn read_u64_be_decodes_correctly() {
371 let buf: &[u8] = &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
372 assert_eq!((&mut &*buf).read_u64_be().unwrap(), 0x0102_0304_0506_0708);
373 }
374
375 #[test]
376 fn read_i64_be_decodes_correctly() {
377 let buf: &[u8] = &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE];
378 assert_eq!((&mut &*buf).read_i64_be().unwrap(), -2);
379 }
380
381 #[test]
382 fn read_u128_be_decodes_correctly() {
383 let buf: &[u8] = &[
384 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
385 0x00, 0x01,
386 ];
387 assert_eq!((&mut &*buf).read_u128_be().unwrap(), 1);
388 }
389
390 #[test]
391 fn read_i128_be_decodes_correctly() {
392 let buf: &[u8] = &[
393 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
394 0xFF, 0xFE,
395 ];
396 assert_eq!((&mut &*buf).read_i128_be().unwrap(), -2);
397 }
398
399 #[test]
400 fn read_f32_be_decodes_correctly() {
401 let expected: f32 = 1.0;
402 let buf = expected.to_be_bytes();
403 assert_eq!((&mut buf.as_slice()).read_f32_be().unwrap(), expected);
404 }
405
406 #[test]
407 fn read_f64_be_decodes_correctly() {
408 let expected: f64 = 1.0;
409 let buf = expected.to_be_bytes();
410 assert_eq!((&mut buf.as_slice()).read_f64_be().unwrap(), expected);
411 }
412
413 #[test]
416 fn write_u8_encodes_correctly() {
417 let mut buf = [0u8; 1];
418 buf.as_mut_slice().write_u8(0xAB).unwrap();
419 assert_eq!(buf, [0xAB]);
420 }
421
422 #[test]
423 fn write_i8_encodes_correctly() {
424 let mut buf = [0u8; 1];
425 buf.as_mut_slice().write_i8(-1).unwrap();
426 assert_eq!(buf, [0xFF]);
427 }
428
429 #[test]
430 fn write_u16_be_encodes_correctly() {
431 let mut buf = [0u8; 2];
432 buf.as_mut_slice().write_u16_be(0x0102).unwrap();
433 assert_eq!(buf, [0x01, 0x02]);
434 }
435
436 #[test]
437 fn write_i16_be_encodes_correctly() {
438 let mut buf = [0u8; 2];
439 buf.as_mut_slice().write_i16_be(-2).unwrap();
440 assert_eq!(buf, [0xFF, 0xFE]);
441 }
442
443 #[test]
444 fn write_u24_be_encodes_correctly() {
445 let mut buf = [0u8; 3];
446 buf.as_mut_slice().write_u24_be(0x0001_0203).unwrap();
447 assert_eq!(buf, [0x01, 0x02, 0x03]);
448 }
449
450 #[test]
451 fn write_u32_be_encodes_correctly() {
452 let mut buf = [0u8; 4];
453 buf.as_mut_slice().write_u32_be(0x0102_0304).unwrap();
454 assert_eq!(buf, [0x01, 0x02, 0x03, 0x04]);
455 }
456
457 #[test]
458 fn write_i32_be_encodes_correctly() {
459 let mut buf = [0u8; 4];
460 buf.as_mut_slice().write_i32_be(-2).unwrap();
461 assert_eq!(buf, [0xFF, 0xFF, 0xFF, 0xFE]);
462 }
463
464 #[test]
465 fn write_u64_be_encodes_correctly() {
466 let mut buf = [0u8; 8];
467 buf.as_mut_slice()
468 .write_u64_be(0x0102_0304_0506_0708)
469 .unwrap();
470 assert_eq!(buf, [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
471 }
472
473 #[test]
474 fn write_i64_be_encodes_correctly() {
475 let mut buf = [0u8; 8];
476 buf.as_mut_slice().write_i64_be(-2).unwrap();
477 assert_eq!(buf, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE]);
478 }
479
480 #[test]
481 fn write_u128_be_encodes_correctly() {
482 let mut buf = [0u8; 16];
483 buf.as_mut_slice().write_u128_be(1).unwrap();
484 assert_eq!(buf, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01]);
485 }
486
487 #[test]
488 fn write_i128_be_encodes_correctly() {
489 let mut buf = [0u8; 16];
490 buf.as_mut_slice().write_i128_be(-2).unwrap();
491 let expected = (-2_i128).to_be_bytes();
492 assert_eq!(buf, expected);
493 }
494
495 #[test]
496 fn write_f32_be_encodes_correctly() {
497 let val: f32 = 1.0;
498 let mut buf = [0u8; 4];
499 buf.as_mut_slice().write_f32_be(val).unwrap();
500 assert_eq!(buf, val.to_be_bytes());
501 }
502
503 #[test]
504 fn write_f64_be_encodes_correctly() {
505 let val: f64 = 1.0;
506 let mut buf = [0u8; 8];
507 buf.as_mut_slice().write_f64_be(val).unwrap();
508 assert_eq!(buf, val.to_be_bytes());
509 }
510
511 #[test]
514 fn round_trip_f32() {
515 let val: f32 = core::f32::consts::PI;
516 let mut buf = [0u8; 4];
517 buf.as_mut_slice().write_f32_be(val).unwrap();
518 assert_eq!((&mut buf.as_slice()).read_f32_be().unwrap(), val);
519 }
520
521 #[test]
522 fn round_trip_f64() {
523 let val: f64 = core::f64::consts::PI;
524 let mut buf = [0u8; 8];
525 buf.as_mut_slice().write_f64_be(val).unwrap();
526 assert_eq!((&mut buf.as_slice()).read_f64_be().unwrap(), val);
527 }
528}