Crates.io | fit_file |
lib.rs | fit_file |
version | 0.6.0 |
source | src |
created_at | 2021-04-24 22:04:36.513014 |
updated_at | 2023-12-05 21:35:01.240558 |
description | Reads FIT (Flexible and Interoperable Data Transfer) files |
homepage | |
repository | https://github.com/msimms/rust_fit_file |
max_upload_size | |
id | 389088 |
size | 161,870 |
FIT file parser written in Rust. FIT (Flexible and Interoperable Data Transfer) is a binary file format that is commonly used to exchange fitness data, such as that from a sports watch or bike computer.
This implementation uses a callback, as shown in the example below. The callback is an efficient method for dealing with large files as it allows us to avoid returning a large array containing each of the records that was processed.
use fit_file as fit;
use fit_file::fit_file;
/// Called for each record message as it is processed.
fn callback(timestamp: u32, global_message_num: u16, _local_msg_type: u8, _message_index: u16, fields: Vec<crate::fit_file::FitFieldValue>, data: &mut Context) {
if global_message_num == crate::fit::GLOBAL_MSG_NUM_SESSION {
let msg = crate::fit::FitSessionMsg::new(fields);
let sport_names = crate::fit::init_sport_name_map();
let sport_id = msg.sport.unwrap();
println!("Sport: {}", sport_names.get(&sport_id).unwrap());
}
else if global_message_num == crate::fit::GLOBAL_MSG_NUM_RECORD {
let msg = crate::fit::FitRecordMsg::new(fields);
data.num_records_processed += 1;
println!("Timestamp: {} Latitude: {} Longitude: {}", timestamp, crate::fit::semicircles_to_degrees(msg.position_lat.unwrap()), crate::fit::semicircles_to_degrees(msg.position_long.unwrap()));
}
}
/// Context structure. An instance of this will be passed to the parser and ultimately to the callback function so we can use it for whatever.
struct Context {
num_records_processed: u16,
}
impl Context {
pub fn new() -> Self {
let context = Context{ num_records_processed: 0 };
context
}
}
fn main() {
let file = std::fs::File::open("tests/20210218_zwift.fit").unwrap();
let mut reader = std::io::BufReader::new(file);
let mut context = Context::new();
crate::fit::read(&mut reader, callback, &mut context).unwrap();
println!("{} records processed", context.num_records_processed);
}
Work in progress.
This project is licensed under the MIT license.