appease clippy/fmt

This commit is contained in:
Will Greenberg
2026-07-24 13:36:18 -07:00
parent bef92d4558
commit 6b1cda2325
2 changed files with 55 additions and 16 deletions
+24 -7
View File
@@ -1,5 +1,5 @@
use rayhunter::analysis::analyzer::ReportMetadata;
use tokio::{io::{AsyncWrite, AsyncWriteExt, BufWriter}};
use tokio::io::{AsyncWrite, AsyncWriteExt, BufWriter};
use crate::Report;
@@ -9,11 +9,18 @@ pub struct IncrementalJsonWriter<T> {
}
impl<T: AsyncWrite + Unpin> IncrementalJsonWriter<T> {
pub async fn new(file: T, check_path: &str, metadata: &ReportMetadata) -> std::io::Result<Self> {
pub async fn new(
file: T,
check_path: &str,
metadata: &ReportMetadata,
) -> std::io::Result<Self> {
let mut writer = BufWriter::new(file);
let path_str = serde_json::to_string(check_path)?;
let metadata_str = serde_json::to_string(metadata)?;
let s = format!(r#"{{ "path": {}, "metadata": {}, "reports": ["#, path_str, metadata_str);
let s = format!(
r#"{{ "path": {}, "metadata": {}, "reports": ["#,
path_str, metadata_str
);
writer.write_all(s.as_bytes()).await?;
Ok(Self {
writer,
@@ -23,7 +30,7 @@ impl<T: AsyncWrite + Unpin> IncrementalJsonWriter<T> {
pub async fn write_report(&mut self, report: &Report) -> std::io::Result<()> {
if self.reports_written > 0 {
self.writer.write_u8(',' as u8).await?;
self.writer.write_u8(b',').await?;
}
let report_str = serde_json::to_string(report)?;
self.writer.write_all(report_str.as_bytes()).await?;
@@ -40,7 +47,9 @@ impl<T: AsyncWrite + Unpin> IncrementalJsonWriter<T> {
#[cfg(test)]
mod test {
use rayhunter::analysis::analyzer::{AnalyzerConfig, Event, EventType, Harness, ReportMetadata};
use rayhunter::analysis::analyzer::{
AnalyzerConfig, Event, EventType, Harness, ReportMetadata,
};
use serde::Deserialize;
use tokio::io::{AsyncReadExt, DuplexStream, duplex};
@@ -58,7 +67,10 @@ mod test {
report.skipped = 13;
report.warnings = 12;
report.events.push(EventWithTimestamp {
event: Event { event_type: EventType::High, message: "hi".into() },
event: Event {
event_type: EventType::High,
message: "hi".into(),
},
timestamp: chrono::DateTime::default(),
});
report
@@ -67,7 +79,12 @@ mod test {
async fn create_test_writer(path: &str) -> (IncrementalJsonWriter<DuplexStream>, DuplexStream) {
let (writer, reader) = duplex(1_000_000);
let harness = Harness::new_with_config(&AnalyzerConfig::default());
(IncrementalJsonWriter::new(writer, path, &harness.get_metadata()).await.unwrap(), reader)
(
IncrementalJsonWriter::new(writer, path, &harness.get_metadata())
.await
.unwrap(),
reader,
)
}
async fn parse_json(mut reader: DuplexStream) -> ExpectedJsonLayout {
+31 -9
View File
@@ -3,7 +3,10 @@ use clap::Parser;
use log::{debug, error, info, warn};
use pcap_file_tokio::pcapng::{Block, PcapNgReader};
use rayhunter::{
analysis::analyzer::{AnalysisRow, AnalyzerConfig, Event, EventType, Harness}, gsmtap::parser as gsmtap_parser, pcap::GsmtapPcapWriter, qmdl::QmdlMessageReader,
analysis::analyzer::{AnalysisRow, AnalyzerConfig, Event, EventType, Harness},
gsmtap::parser as gsmtap_parser,
pcap::GsmtapPcapWriter,
qmdl::QmdlMessageReader,
};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf};
@@ -102,7 +105,11 @@ impl Report {
}
}
async fn analyze_pcap(pcap_path: &str, show_skipped: bool, json_writer: Option<&mut IncrementalJsonWriter<File>>) {
async fn analyze_pcap(
pcap_path: &str,
show_skipped: bool,
json_writer: Option<&mut IncrementalJsonWriter<File>>,
) {
let mut harness = Harness::new_with_config(&AnalyzerConfig::default());
let pcap_file = &mut File::open(&pcap_path).await.expect("failed to open file");
let mut pcap_reader = PcapNgReader::new(pcap_file)
@@ -121,11 +128,18 @@ async fn analyze_pcap(pcap_path: &str, show_skipped: bool, json_writer: Option<&
}
report.print_summary(show_skipped);
if let Some(writer) = json_writer {
writer.write_report(&report).await.expect("failed to write report to JSON");
writer
.write_report(&report)
.await
.expect("failed to write report to JSON");
}
}
async fn analyze_qmdl(qmdl_path: &str, show_skipped: bool, json_writer: Option<&mut IncrementalJsonWriter<File>>) {
async fn analyze_qmdl(
qmdl_path: &str,
show_skipped: bool,
json_writer: Option<&mut IncrementalJsonWriter<File>>,
) {
let mut harness = Harness::new_with_config(&AnalyzerConfig::default());
let qmdl_file = &mut File::open(&qmdl_path).await.expect("failed to open file");
let mut qmdl_reader = QmdlMessageReader::new(qmdl_file)
@@ -141,7 +155,10 @@ async fn analyze_qmdl(qmdl_path: &str, show_skipped: bool, json_writer: Option<&
}
report.print_summary(show_skipped);
if let Some(writer) = json_writer {
writer.write_report(&report).await.expect("failed to write report to JSON");
writer
.write_report(&report)
.await
.expect("failed to write report to JSON");
}
}
@@ -208,9 +225,11 @@ async fn main() {
.await
.expect("failed to create JSON output file");
let check_path_str = args.path.to_string_lossy();
json_writer = Some(IncrementalJsonWriter::new(json_file, &check_path_str, &metadata)
.await
.expect("failed to create JSON writer"));
json_writer = Some(
IncrementalJsonWriter::new(json_file, &check_path_str, &metadata)
.await
.expect("failed to create JSON writer"),
);
}
for maybe_entry in WalkDir::new(&args.path) {
@@ -238,6 +257,9 @@ async fn main() {
if let Some(writer) = json_writer {
// if we have a json_writer, we also have args.json
info!("Writing report to {:?}", args.json.unwrap());
writer.finish().await.expect("failed to finish writing to JSON file");
writer
.finish()
.await
.expect("failed to finish writing to JSON file");
}
}