Skip to content

Commit

Permalink
saving work, learning a lot, very cool
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgraham4401 committed Nov 15, 2023
1 parent ab478dd commit 4a6abe1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/gather.rs
Original file line number Diff line number Diff line change
@@ -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<Gather, io::Error>;
}

impl Loader for Gather {
fn load(&self, file_name: &str) -> Result<Gather, Error> {
todo!()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Precision {
Expand All @@ -8,16 +18,16 @@ pub enum Precision {
}

#[derive(Debug)]
pub struct Gather<T> {
pub struct Gather {
pub id: u16,
pub nt: u16,
pub nx: u16,
pub dt: f32,
pub data: Option<Box<T>>,
pub data: Option<Vec<f64>>,
precision: Option<Precision>,
}

#[allow(dead_code)]

fn read_binary_file(file_name: &str) -> Result<Vec<u8>, String>{
let raw_data = fs::read(file_name);
let data = match raw_data {
Expand All @@ -34,7 +44,7 @@ fn write_binary_to_file(file_path: &str, data: Vec<u8>) -> std::io::Result<()> {
}

#[allow(dead_code)]
fn group_u8_to_f64(bytes: &[u8]) -> Vec<f64> {
fn bytes_to_f64(bytes: &[u8]) -> Vec<f64> {
bytes
.chunks_exact(8) // Take chunks of 8 bytes
.map(|chunk| {
Expand All @@ -45,9 +55,15 @@ fn group_u8_to_f64(bytes: &[u8]) -> Vec<f64> {
.collect()
}

/// Convert a slice of f64 values to a Vec<u8> for writing to a binary file
///
/// # Arguments
/// * `data`: &[f64]
///
/// returns: Vec<u8, Global>
#[allow(dead_code)]
fn split_f64_to_u8(values: &[f64]) -> Vec<u8> {
values
fn data_to_bytes(data: &[f64]) -> Vec<u8> {
data
.iter()
.flat_map(|&f| {
let bits = f.to_bits();
Expand All @@ -63,7 +79,7 @@ mod tests {
#[test]
fn writes_binary_file() {
let double_data: Vec<f64> = 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)
Expand All @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod gather;
pub use gather::Gather;

0 comments on commit 4a6abe1

Please sign in to comment.