Skip to content

Commit

Permalink
Fix/data writer improvements (#8)
Browse files Browse the repository at this point in the history
* Remove unused imports

* Improve DataWriter
  • Loading branch information
johnxnguyen authored Dec 12, 2018
1 parent fb781be commit 334fd43
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
Cargo.lock
.idea/*
.DS_Store
/data
2 changes: 0 additions & 2 deletions src/geometry/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::ops::{Add, AddAssign, Div, Mul};
use std::cmp::PartialEq;
use std::fs::File;
use std::io::prelude::*;

// Point /////////////////////////////////////////////////////////////////////
//
Expand Down
83 changes: 77 additions & 6 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,80 @@
use geometry::types::Point;
use std::fs::File;
use std::fs;
use std::io::Write;
use std::path::Path;

use geometry::types::Point;

// DataWriter ////////////////////////////////////////////////////////////////
//
// A utility object to simplify persistence of point data. Each call to
// write will generate a new file in the specified directory. Files are
// sequentially numbered.

pub struct DataWriter {
directory: String,
counter: u32,
}

impl DataWriter {
/// Creates a new directory in the current working path.
pub fn new(directory: &str) -> DataWriter {
if !Path::new(directory).exists() {
fs::create_dir(directory)
.expect("Couldn't create dir.");
}
DataWriter {
directory: directory.to_owned(),
counter: 0
}
}

/// Creates a new file in the writers directory with each point written
/// on a separate line.
pub fn write(&mut self, points: Vec<Point>) {
let path = format!("{}/frame-{}.txt", self.directory, self.counter);
match self.write_points(points, path) {
Err(e) => panic!("Error writing data. {}", e),
Ok(_) => (),
}
self.counter += 1;
}

fn write_points(&self, points: Vec<Point>, path: String) -> std::io::Result<()> {
let mut file = fs::File::create(path)?;
for point in points { write!(file, "{},{}\n", point.x, point.y)?; }
Ok(())
}
}

// Tests /////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
use std::io::Read;

use super::*;

#[test]
fn data_writer_writes() {
// given
let mut writer = DataWriter::new("temp");

// when
writer.write(vec![Point::new(3.4, 6.7)]);
writer.write(vec![Point::new(6.4, 6.785)]);

// then
let mut file = fs::File::open("temp/frame-0.txt").expect("Error opening file.");
let mut contents = String::new();
file.read_to_string(&mut contents);
assert_eq!(contents, "3.4,6.7\n".to_owned());

let mut file = fs::File::open("temp/frame-1.txt").expect("Error opening file.");
let mut contents = String::new();
file.read_to_string(&mut contents);
assert_eq!(contents, "6.4,6.785\n".to_owned());

pub fn write_points(points: Vec<Point>, file_name: &str) -> std::io::Result<()> {
let mut file = File::create(file_name)?;
for point in points { write!(file, "{},{}\n", point.x, point.y)?; }
Ok(())
// after
fs::remove_dir_all("temp").expect("Error cleaning up test.");
}
}

0 comments on commit 334fd43

Please sign in to comment.