Refactor main

This commit is contained in:
2026-03-07 10:11:15 -05:00
parent a16fbd3a8e
commit e801da4beb

View File

@@ -85,7 +85,7 @@ struct Arg {
fn main() -> Result<()> {
let arg = Arg::parse();
let mut file = arg.output.map(|path| {
let mut output = arg.output.map(|path| {
//TODO automatically add file extension
let file_out = File::create(path).expect("Error opening file for writing");
let header = PcapHeader {
@@ -101,8 +101,8 @@ fn main() -> Result<()> {
let file = io::BufReader::new(io::stdin());
thread::spawn(move || tnc_loop(tx, file).unwrap());
}
s if Path::new(s).exists() => {
let file = io::BufReader::new(File::open(s)?);
path if Path::new(path).exists() => {
let file = io::BufReader::new(File::open(path)?);
thread::spawn(move || tnc_loop(tx, file).unwrap());
}
addr => {
@@ -113,37 +113,28 @@ fn main() -> Result<()> {
}
while let Ok(pkt) = rx.recv() {
//let time = SystemTime::now();
//let timestamp = time.duration_since(SystemTime::UNIX_EPOCH);
if let Ok(frame) = Ax25Frame::from_bytes(&pkt.data[1..]) {
if arg.verbose {
frame.print_long();
} else {
frame.print_short();
}
} else {
let mut s = String::new();
for byte in pkt.data.iter() {
//if byte >= ' '.into() && byte <= '~'.into() {
if *byte >= 32 && *byte <= 126 {
s.push((*byte).into());
} else {
s.push_str(&format!("\\{:x?} ", *byte));
}
}
if arg.verbose {
if let Ok(frame) = Ax25Frame::from_bytes(&pkt.data[1..]) {
frame.print_long();
} else {
//TODO print time stamp
println!("{}", s);
}
} else {
if let Ok(frame) = Ax25Frame::from_bytes(&pkt.data[1..]) {
frame.print_short();
} else {
//TODO print time stamp
println!("{}", s);
}
}
if let Some(ref mut writer) = file {
let pkt = PcapPacket::new_owned(pkt.timestamp, pkt.orig_len, pkt.data);
writer.write_packet(&pkt).unwrap();
if let Some(ref mut writer) = output {
let pcpkt = PcapPacket::new_owned(pkt.timestamp, pkt.orig_len, pkt.data);
writer.write_packet(&pcpkt).unwrap();
}
}