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