Skip to content

Commit

Permalink
fix: won't panic, just tell about errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeromos Kovacs committed Jul 4, 2024
1 parent 3dadbd2 commit e8755c4
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use gpx::{Gpx, GpxVersion, Track, TrackSegment, Waypoint};
use std::{fs, fs::File, io::BufWriter};
use time::OffsetDateTime;

type Res<T> = Result<T, Box<dyn std::error::Error>>;

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
Expand Down Expand Up @@ -121,20 +123,24 @@ fn main() {
}
let file = file.clone();
let jh = std::thread::spawn(move || {
fit2gpx(&file);
let _ = fit2gpx(&file).inspect_err(|e| eprintln!("error: {e:#?}"));
});
handles.push(jh);
}
for handle in handles {
handle.join().unwrap();
let res = handle.join();
if let Err(e) = res {
eprintln!("error: {e:#?}");
continue;
}
}
}

fn fit2gpx(f_in: &String) {
let file = fs::read(f_in).unwrap();
let fit: Fit = Fit::read(file).unwrap();
eprintln!("-----------------------------------\n\n\nfile: {f_in}");
// let mut log_file = File::create([f_in, ".log"].concat()).unwrap();
fn fit2gpx(f_in: &String) -> Res<()> {
let file = fs::read(f_in)?;
let fit: Fit = Fit::read(file)?;
eprintln!("file: {f_in}");
// let mut log_file = File::create([f_in, ".log"].concat())?;

// println!("\n\nHEADER:");
// println!("\theader size: {}", &fit.header.header_size);
Expand All @@ -154,12 +160,12 @@ fn fit2gpx(f_in: &String) {
// println!("\nDefinition: {:#?}", msg.data);
}
FitMessage::Data(msg) => {
// writeln!(log_file, "\nData: {msg:#?}").unwrap();
// writeln!(log_file, "\nData: {msg:#?}")?;
// println!("\nData: {:#?}", msg);
if let MessageType::Record = msg.data.message_type {
let rec_dat: RecordData = msg.data.clone().into();
if rec_dat.invalid() {
eprintln!("warn: guess it's invalid, as it doesn't contain lat/lon data: {msg:#?}");
eprintln!("doesn't contain lat/lon data: {:?}", msg.data);
continue;
}
// eprintln!("{rec_dat:#?}");
Expand All @@ -173,7 +179,7 @@ fn fit2gpx(f_in: &String) {
track_segment.points.push(wp);
} else {
eprintln!("warn: NOT in an activity right now");
// std::io::stdin().read_line(&mut String::new()).unwrap();
// std::io::stdin().read_line(&mut String::new())?;
}
} else if let MessageType::Activity = msg.data.message_type {
let start_stop = df_at(&msg.data, 4);
Expand Down Expand Up @@ -211,11 +217,13 @@ fn fit2gpx(f_in: &String) {

let f_out = f_in.replace(".fit", ".gpx");
// Create file at path
let gpx_file = File::create(f_out).unwrap();
let gpx_file = File::create(f_out)?;
let buf = BufWriter::new(gpx_file);

// Write to file
gpx::write(&gpx, buf).unwrap();
gpx::write(&gpx, buf)?;

Ok(())
}

fn value_to_float(val: Option<&Value>) -> Option<f64> {
Expand Down

0 comments on commit e8755c4

Please sign in to comment.