浏览代码

Formatting

Kevin Lee 1 年之前
父节点
当前提交
c849d83928
共有 6 个文件被更改,包括 57 次插入25 次删除
  1. 3 2
      src/ezcad/array_of.rs
  2. 6 3
      src/ezcad/field_of.rs
  3. 1 1
      src/ezcad/mod.rs
  4. 1 1
      src/lib.rs
  5. 4 6
      src/main.rs
  6. 42 12
      tests/array_of.rs

+ 3 - 2
src/ezcad/array_of.rs

@@ -1,4 +1,4 @@
-use std::ops::{DerefMut, Deref};
+use std::ops::{Deref, DerefMut};
 
 use binrw::{binrw, BinRead, BinWrite};
 
@@ -7,6 +7,7 @@ use binrw::{binrw, BinRead, BinWrite};
 #[derive(Debug, PartialEq)]
 pub struct ArrayOf<T>
 where
+    // Static bounds due to https://github.com/jam1garner/binrw/issues/199
     T: for<'a> BinRead<Args<'a> = ()> + 'static,
     T: for<'a> BinWrite<Args<'a> = ()> + 'static,
 {
@@ -19,7 +20,7 @@ where
     pub value: Vec<T>,
 }
 
-impl<T> Deref for ArrayOf<T> 
+impl<T> Deref for ArrayOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()> + 'static,
     T: for<'a> BinWrite<Args<'a> = ()> + 'static,

+ 6 - 3
src/ezcad/field_of.rs

@@ -1,5 +1,8 @@
-use binrw::{BinRead, BinWrite, binrw};
-use std::{mem::size_of, ops::{DerefMut, Deref}};
+use binrw::{binrw, BinRead, BinWrite};
+use std::{
+    mem::size_of,
+    ops::{Deref, DerefMut},
+};
 
 /// Wrapper to prefix arbitrary structs with size of struct as u32
 #[binrw]
@@ -17,7 +20,7 @@ where
     pub value: T,
 }
 
-impl<T> Deref for FieldOf<T> 
+impl<T> Deref for FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()>,
     T: for<'a> BinWrite<Args<'a> = ()>,

+ 1 - 1
src/ezcad/mod.rs

@@ -1,3 +1,3 @@
 pub mod array_of;
 pub mod basic_field;
-pub mod field_of;
+pub mod field_of;

+ 1 - 1
src/lib.rs

@@ -1,3 +1,3 @@
 pub mod ezcad;
 
-pub use ezcad::*;
+pub use ezcad::*;

+ 4 - 6
src/main.rs

@@ -1,4 +1,4 @@
-use std::{path::PathBuf, fs::read};
+use std::{fs::read, path::PathBuf};
 
 use clap::Parser;
 
@@ -22,16 +22,14 @@ struct Cli {
 
 fn main() {
     env_logger::builder()
-    .format_timestamp(None)
-    .filter_level(log::LevelFilter::Info)
-    .init();
+        .format_timestamp(None)
+        .filter_level(log::LevelFilter::Info)
+        .init();
 
     let args: Cli = Cli::parse();
 
     let _input = read(args.input).unwrap();
     let _confing = read(args.config).unwrap();
 
-
     println!("Hello, world!");
-    
 }

+ 42 - 12
tests/array_of.rs

@@ -5,16 +5,15 @@ use ezcad::array_of::ArrayOf;
 
 #[test]
 fn array_of_be() {
-    let encoded: Vec<u8> = vec![0x00, 0x00, 0x00, 0x02, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04];
+    let encoded: Vec<u8> = vec![
+        0x00, 0x00, 0x00, 0x02, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04,
+    ];
     let decoded: ArrayOf<u32> = ArrayOf {
-        value: vec![0x0A0B0C0D, 0x01020304]
+        value: vec![0x0A0B0C0D, 0x01020304],
     };
 
     let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
-    assert_eq!(
-        ArrayOf::<u32>::read_be(&mut stream).unwrap(),
-        decoded
-    );
+    assert_eq!(ArrayOf::<u32>::read_be(&mut stream).unwrap(), decoded);
 
     let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
     decoded.write_be(&mut buffer).unwrap();
@@ -23,18 +22,49 @@ fn array_of_be() {
 
 #[test]
 fn array_of_le() {
-    let encoded: Vec<u8> = vec![0x02, 0x00, 0x00, 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04];
+    let encoded: Vec<u8> = vec![
+        0x02, 0x00, 0x00, 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04,
+    ];
     let decoded: ArrayOf<u32> = ArrayOf {
-        value: vec![0x0D0C0B0A, 0x04030201]
+        value: vec![0x0D0C0B0A, 0x04030201],
     };
 
     let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
-    assert_eq!(
-        ArrayOf::<u32>::read_le(&mut stream).unwrap(),
-        decoded
-    );
+    assert_eq!(ArrayOf::<u32>::read_le(&mut stream).unwrap(), decoded);
 
     let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
     decoded.write_le(&mut buffer).unwrap();
     assert_eq!(buffer.into_inner(), encoded);
 }
+
+#[test]
+fn array_of_struct() {
+    #[derive(BinRead, BinWrite, Debug, PartialEq, Default)]
+    struct Test {
+        a: u16,
+        b: u16,
+    }
+
+    let encoded: Vec<u8> = vec![
+        0x00, 0x00, 0x00, 0x02, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x02, 0x03, 0x04,
+    ];
+    let decoded: ArrayOf<Test> = ArrayOf {
+        value: vec![
+            Test {
+                a: 0x0A0B,
+                b: 0x0C0D,
+            },
+            Test {
+                a: 0x0102,
+                b: 0x0304,
+            },
+        ],
+    };
+
+    let mut stream: Cursor<Vec<u8>> = Cursor::new(encoded.clone());
+    assert_eq!(ArrayOf::<Test>::read_be(&mut stream).unwrap(), decoded);
+
+    let mut buffer: Cursor<Vec<u8>> = Cursor::new(vec![]);
+    decoded.write_be(&mut buffer).unwrap();
+    assert_eq!(buffer.into_inner(), encoded);
+}