From 4a6abe17a3ed95635fe049509235e78b2f967e78 Mon Sep 17 00:00:00 2001 From: David Graham Date: Tue, 14 Nov 2023 20:31:14 -0500 Subject: [PATCH] saving work, learning a lot, very cool --- src/gather.rs | 36 ++++++++++++++++++++++++++---------- src/lib.rs | 1 + 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/gather.rs b/src/gather.rs index aa6a022..c861a2f 100644 --- a/src/gather.rs +++ b/src/gather.rs @@ -1,5 +1,15 @@ -use std::fs; -use std::io::Read; +use std::{fs, io}; +use std::io::{Error, Read}; + +pub trait Loader { + fn load(&self, file_name: &str) -> Result; +} + +impl Loader for Gather { + fn load(&self, file_name: &str) -> Result { + todo!() + } +} #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Precision { @@ -8,16 +18,16 @@ pub enum Precision { } #[derive(Debug)] -pub struct Gather { +pub struct Gather { pub id: u16, pub nt: u16, pub nx: u16, pub dt: f32, - pub data: Option>, + pub data: Option>, precision: Option, } -#[allow(dead_code)] + fn read_binary_file(file_name: &str) -> Result, String>{ let raw_data = fs::read(file_name); let data = match raw_data { @@ -34,7 +44,7 @@ fn write_binary_to_file(file_path: &str, data: Vec) -> std::io::Result<()> { } #[allow(dead_code)] -fn group_u8_to_f64(bytes: &[u8]) -> Vec { +fn bytes_to_f64(bytes: &[u8]) -> Vec { bytes .chunks_exact(8) // Take chunks of 8 bytes .map(|chunk| { @@ -45,9 +55,15 @@ fn group_u8_to_f64(bytes: &[u8]) -> Vec { .collect() } +/// Convert a slice of f64 values to a Vec for writing to a binary file +/// +/// # Arguments +/// * `data`: &[f64] +/// +/// returns: Vec #[allow(dead_code)] -fn split_f64_to_u8(values: &[f64]) -> Vec { - values +fn data_to_bytes(data: &[f64]) -> Vec { + data .iter() .flat_map(|&f| { let bits = f.to_bits(); @@ -63,7 +79,7 @@ mod tests { #[test] fn writes_binary_file() { let double_data: Vec = vec![10.352012, -5.0272, 3.0, -2.0899]; - let bytes = split_f64_to_u8(&double_data); + let bytes = data_to_bytes(&double_data); let file_path: &str = "./testTrace.bin"; let raw_data = write_binary_to_file(file_path, bytes); println!("Gather: {:?}", raw_data) @@ -72,7 +88,7 @@ mod tests { #[test] fn reads_binary_file() { - let file_path: &str = "./resources/shot6220.bin"; + let file_path: &str = "./testTrace.bin"; let raw_data = read_binary_file(file_path); println!("Gather: {:?}", raw_data) } diff --git a/src/lib.rs b/src/lib.rs index 19dc94c..9077467 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ mod gather; +pub use gather::Gather; \ No newline at end of file