-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove unused imports * Improve DataWriter
- Loading branch information
1 parent
fb781be
commit 334fd43
Showing
3 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
Cargo.lock | ||
.idea/* | ||
.DS_Store | ||
/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |