Skip to content

Commit

Permalink
Configure & run rustfmt (#15)
Browse files Browse the repository at this point in the history
* Add rustfmt::skip attributes

* Add rustfmt config

* Run rustfmt

* Add formatting check to CI

* GH action fix attempt #1

* Indentation fix

* GH action fix attempt # 2

* Update Push action
  • Loading branch information
enteryournamehere authored Feb 9, 2023
1 parent 7005201 commit 3f752b2
Show file tree
Hide file tree
Showing 34 changed files with 414 additions and 396 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ jobs:
key: target-debug-${{ github.run_number }}
restore-keys: |
target-debug-
- name: Install rustfmt
uses: actions-rs/toolchain@v1
with:
profile: minimal
components: rustfmt

- name: Check formatting
uses: actions-rs/cargo@v1
with:
command: fmt
args: --check

- name: Compile
uses: actions-rs/cargo@v1
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ jobs:
key: target-debug-${{ github.run_number }}
restore-keys: |
target-debug-
- name: Install rustfmt
uses: actions-rs/toolchain@v1
with:
profile: minimal
components: rustfmt

- name: Check formatting
uses: actions-rs/cargo@v1
with:
command: fmt
args: --check

- name: Compile
uses: actions-rs/cargo@v1
Expand Down
228 changes: 112 additions & 116 deletions examples/capture_parser/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ fn visit_dirs(dir: &Path, cdclient: &mut Cdclient, level: usize) -> Res<usize> {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
packet_count += if path.is_dir() { visit_dirs(&path, cdclient, level+1) } else { parse(&path, cdclient) }?;
println!("packet count = {:>level$}", packet_count, level=level*6);
packet_count += if path.is_dir() { visit_dirs(&path, cdclient, level + 1) } else { parse(&path, cdclient) }?;
println!("packet count = {:>level$}", packet_count, level = level * 6);
}
}
Ok(packet_count)
Expand All @@ -48,7 +48,10 @@ fn visit_dirs(dir: &Path, cdclient: &mut Cdclient, level: usize) -> Res<usize> {
fn parse(path: &Path, cdclient: &mut Cdclient) -> Res<usize> {
use endio::LERead;

if path.extension().unwrap() != "zip" { return Ok(0); }
if path.extension().unwrap() != "zip" {
return Ok(0);
}

let src = BufReader::new(File::open(path).unwrap());
let mut zip = ZipArchive::new(src).unwrap();
let mut comps = HashMap::new();
Expand All @@ -57,112 +60,103 @@ fn parse(path: &Path, cdclient: &mut Cdclient) -> Res<usize> {
while i < zip.len() {
let mut file = zip.by_index(i).unwrap();
if file.name().contains("of") {
i += 1; continue;
i += 1;
continue;
}
if (file.name().contains("[53-01-")) || (file.name().contains("[53-05-00-00]")) || (file.name().contains("[53-04-")
&& !file.name().contains("[53-04-00-16]")
&& !file.name().contains("[e6-00]")
&& !file.name().contains("[6b-03]")
&& !file.name().contains("[16-04]")
&& !file.name().contains("[49-04]")
&& !file.name().contains("[ad-04]")
&& !file.name().contains("[1c-05]")
&& !file.name().contains("[230]")
&& !file.name().contains("[875]")
&& !file.name().contains("[1046]")
&& !file.name().contains("[1097]")
&& !file.name().contains("[1197]")
&& !file.name().contains("[1308]"))
|| (file.name().contains("[53-02-") || (file.name().contains("[53-05-")
&& !file.name().contains("[53-05-00-00]")
&& !file.name().contains("[53-05-00-31]")
&& !file.name().contains("[e6-00]")
&& !file.name().contains("[ff-00]")
&& !file.name().contains("[a1-01]")
&& !file.name().contains("[7f-02]")
&& !file.name().contains("[a3-02]")
&& !file.name().contains("[cc-02]")
&& !file.name().contains("[35-03]")
&& !file.name().contains("[36-03]")
&& !file.name().contains("[4d-03]")
&& !file.name().contains("[6d-03]")
&& !file.name().contains("[91-03]")
&& !file.name().contains("[1a-05]")
&& !file.name().contains("[e6-05]")
&& !file.name().contains("[16-06]")
&& !file.name().contains("[1c-06]")
&& !file.name().contains("[6f-06]")
&& !file.name().contains("[70-06]")
&& !file.name().contains("[118]")
&& !file.name().contains("[230]")
&& !file.name().contains("[255]")
&& !file.name().contains("[417]")
&& !file.name().contains("[639]")
&& !file.name().contains("[675]")
&& !file.name().contains("[716]")
&& !file.name().contains("[821]")
&& !file.name().contains("[822]")
&& !file.name().contains("[845]")
&& !file.name().contains("[877]")
&& !file.name().contains("[913]")
&& !file.name().contains("[1306]")
&& !file.name().contains("[1510]")
&& !file.name().contains("[1558]")
&& !file.name().contains("[1564]")
&& !file.name().contains("[1647]")
&& !file.name().contains("[1648]"))
|| (file.name().contains("[24]")
&& !file.name().contains("(2365)")
&& !file.name().contains("(4734)")
&& !file.name().contains("(4930)")
&& !file.name().contains("(4955)")
&& !file.name().contains("(4967)")
&& !file.name().contains("(4990)")
&& !file.name().contains("(5635)")
&& !file.name().contains("(5651)")
&& !file.name().contains("(5652)")
&& !file.name().contains("(5903)")
&& !file.name().contains("(5904)")
&& !file.name().contains("(5958)")
&& !file.name().contains("(6007)")
&& !file.name().contains("(6010)")
&& !file.name().contains("(6097)")
&& !file.name().contains("(6209)")
&& !file.name().contains("(6267)")
&& !file.name().contains("(6289)")
&& !file.name().contains("(6290)")
&& !file.name().contains("(6319)")
&& !file.name().contains("(7001)")
&& !file.name().contains("(7100)")
&& !file.name().contains("(7282)")
&& !file.name().contains("(7796)")
&& !file.name().contains("(8304)")
&& !file.name().contains("(8575)")
&& !file.name().contains("(9741)")
&& !file.name().contains("(10039)")
&& !file.name().contains("(10042)")
&& !file.name().contains("(10046)")
&& !file.name().contains("(10055)")
&& !file.name().contains("(10097)")
&& !file.name().contains("(12916)")
&& !file.name().contains("(13773)")
&& !file.name().contains("(14376)")
&& !file.name().contains("(14447)")
&& !file.name().contains("(14449)")
&& !file.name().contains("(14476)")
&& !file.name().contains("(14477)")
&& !file.name().contains("(14505)")
&& !file.name().contains("(14510)")
&& !file.name().contains("(14539)")
&& !file.name().contains("(14540)")
&& !file.name().contains("(14541)")
&& !file.name().contains("(14542)")
&& !file.name().contains("(14543)")
&& !file.name().contains("(14544)")
&& !file.name().contains("(14545)")
&& !file.name().contains("(14546)")
&& !file.name().contains("(14547)"))
|| file.name().contains("[27]"))
if (file.name().contains("[53-01-"))
|| (file.name().contains("[53-05-00-00]"))
|| (file.name().contains("[53-04-") && !file.name().contains("[53-04-00-16]") && !file.name().contains("[e6-00]") && !file.name().contains("[6b-03]") && !file.name().contains("[16-04]") && !file.name().contains("[49-04]") && !file.name().contains("[ad-04]") && !file.name().contains("[1c-05]") && !file.name().contains("[230]") && !file.name().contains("[875]") && !file.name().contains("[1046]") && !file.name().contains("[1097]") && !file.name().contains("[1197]") && !file.name().contains("[1308]"))
|| (file.name().contains("[53-02-")
|| (file.name().contains("[53-05-")
&& !file.name().contains("[53-05-00-00]")
&& !file.name().contains("[53-05-00-31]")
&& !file.name().contains("[e6-00]")
&& !file.name().contains("[ff-00]")
&& !file.name().contains("[a1-01]")
&& !file.name().contains("[7f-02]")
&& !file.name().contains("[a3-02]")
&& !file.name().contains("[cc-02]")
&& !file.name().contains("[35-03]")
&& !file.name().contains("[36-03]")
&& !file.name().contains("[4d-03]")
&& !file.name().contains("[6d-03]")
&& !file.name().contains("[91-03]")
&& !file.name().contains("[1a-05]")
&& !file.name().contains("[e6-05]")
&& !file.name().contains("[16-06]")
&& !file.name().contains("[1c-06]")
&& !file.name().contains("[6f-06]")
&& !file.name().contains("[70-06]")
&& !file.name().contains("[118]")
&& !file.name().contains("[230]")
&& !file.name().contains("[255]")
&& !file.name().contains("[417]")
&& !file.name().contains("[639]")
&& !file.name().contains("[675]")
&& !file.name().contains("[716]")
&& !file.name().contains("[821]")
&& !file.name().contains("[822]")
&& !file.name().contains("[845]")
&& !file.name().contains("[877]")
&& !file.name().contains("[913]")
&& !file.name().contains("[1306]")
&& !file.name().contains("[1510]")
&& !file.name().contains("[1558]")
&& !file.name().contains("[1564]")
&& !file.name().contains("[1647]")
&& !file.name().contains("[1648]"))
|| (file.name().contains("[24]")
&& !file.name().contains("(2365)")
&& !file.name().contains("(4734)")
&& !file.name().contains("(4930)")
&& !file.name().contains("(4955)")
&& !file.name().contains("(4967)")
&& !file.name().contains("(4990)")
&& !file.name().contains("(5635)")
&& !file.name().contains("(5651)")
&& !file.name().contains("(5652)")
&& !file.name().contains("(5903)")
&& !file.name().contains("(5904)")
&& !file.name().contains("(5958)")
&& !file.name().contains("(6007)")
&& !file.name().contains("(6010)")
&& !file.name().contains("(6097)")
&& !file.name().contains("(6209)")
&& !file.name().contains("(6267)")
&& !file.name().contains("(6289)")
&& !file.name().contains("(6290)")
&& !file.name().contains("(6319)")
&& !file.name().contains("(7001)")
&& !file.name().contains("(7100)")
&& !file.name().contains("(7282)")
&& !file.name().contains("(7796)")
&& !file.name().contains("(8304)")
&& !file.name().contains("(8575)")
&& !file.name().contains("(9741)")
&& !file.name().contains("(10039)")
&& !file.name().contains("(10042)")
&& !file.name().contains("(10046)")
&& !file.name().contains("(10055)")
&& !file.name().contains("(10097)")
&& !file.name().contains("(12916)")
&& !file.name().contains("(13773)")
&& !file.name().contains("(14376)")
&& !file.name().contains("(14447)")
&& !file.name().contains("(14449)")
&& !file.name().contains("(14476)")
&& !file.name().contains("(14477)")
&& !file.name().contains("(14505)")
&& !file.name().contains("(14510)")
&& !file.name().contains("(14539)")
&& !file.name().contains("(14540)")
&& !file.name().contains("(14541)")
&& !file.name().contains("(14542)")
&& !file.name().contains("(14543)")
&& !file.name().contains("(14544)")
&& !file.name().contains("(14545)")
&& !file.name().contains("(14546)")
&& !file.name().contains("(14547)"))
|| file.name().contains("[27]"))
{
let mut ctx = ZipContext { zip: file, comps: &mut comps, cdclient, assert_fully_read: true };
let msg: Message = ctx.read().expect(&format!("Zip: {}, Filename: {}, {} bytes", path.to_str().unwrap(), ctx.zip.name(), ctx.zip.size()));
Expand All @@ -178,8 +172,12 @@ fn parse(path: &Path, cdclient: &mut Cdclient) -> Res<usize> {
std::io::Read::read_to_end(&mut file, &mut rest).unwrap();
assert_eq!(rest, vec![], "Zip: {}, Filename: {}, {} bytes", path.to_str().unwrap(), file.name(), file.size());
}
i += 1; continue
} else { i += 1; continue }
i += 1;
continue;
} else {
i += 1;
continue;
}
}
Ok(packet_count)
}
Expand All @@ -192,14 +190,12 @@ fn main() {
}
let capture = fs::canonicalize(&args[1]).unwrap();
let mut cdclient = Cdclient { conn: Connection::open(&args[2]).unwrap(), comp_cache: HashMap::new() };
unsafe { PRINT_PACKETS = args.get(3).is_some(); }
unsafe {
PRINT_PACKETS = args.get(3).is_some();
}

let start = Instant::now();
let packet_count = if !capture.is_dir() && capture.extension().unwrap() == "zip" {
parse(&capture, &mut cdclient)
} else {
visit_dirs(&capture, &mut cdclient, 0)
}.unwrap();
let packet_count = if !capture.is_dir() && capture.extension().unwrap() == "zip" { parse(&capture, &mut cdclient) } else { visit_dirs(&capture, &mut cdclient, 0) }.unwrap();
println!();
println!("Number of parsed packets: {}", packet_count);
println!("Time taken: {:?}", start.elapsed());
Expand Down
28 changes: 18 additions & 10 deletions examples/capture_parser/zip_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ impl ZipContext<'_> {
fn apply_whitelist(comps: &mut Vec<u32>, config: &Option<LuNameValue>) {
if let Some(conf) = config {
if let Some(LnvValue::I32(1)) = conf.get(&lu!("componentWhitelist")) {
comps.retain(|&x|
match x {
1 | 2 | 3 | 7 | 10 | 11 | 24 | 42 => true,
_ => false,
}
);
comps.retain(|&x| match x {
1 | 2 | 3 | 7 | 10 | 11 | 24 | 42 => true,
_ => false,
});
}
}
}
Expand Down Expand Up @@ -95,15 +93,23 @@ impl ZipContext<'_> {
for comp in comps {
// special case: utter bodge
match comp {
2 => { final_comps.push(44); }
4 => { final_comps.push(110); final_comps.push(109); final_comps.push(106); }
7 => { final_comps.push(98); }
2 => {
final_comps.push(44);
}
4 => {
final_comps.push(110);
final_comps.push(109);
final_comps.push(106);
}
7 => {
final_comps.push(98);
}
23 | 48 => {
if !final_comps.contains(&7) {
final_comps.push(7);
}
}
_ => {},
_ => {}
}
final_comps.push(*comp);
}
Expand All @@ -114,6 +120,7 @@ impl ZipContext<'_> {
}
}

#[rustfmt::skip]
fn map_constrs<R: std::io::Read>(comps: &Vec<u32>) -> Vec<fn(&mut BEBitReader<R>) -> Res<Box<dyn ComponentConstruction>>> {
use endio::Deserialize;

Expand Down Expand Up @@ -186,6 +193,7 @@ impl ReplicaContext for ZipContext<'_> {
constrs
}

#[rustfmt::skip]
fn get_comp_serializations<R: std::io::Read>(&mut self, network_id: u16) -> Vec<fn(&mut BEBitReader<R>) -> Res<Box<dyn ComponentSerialization>>> {
use endio::Deserialize;

Expand Down
7 changes: 7 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
unstable_features = true
hard_tabs = true
reorder_imports = false
reorder_modules = false
# disable line wrapping
max_width = 1000
use_small_heuristics = "Max"
Loading

0 comments on commit 3f752b2

Please sign in to comment.