#1 Finished FileLinked implementation.

This commit is contained in:
Jacob VanDomelen 2019-05-31 23:07:13 -07:00
parent 4252851b81
commit feadba1a65
4 changed files with 63 additions and 28 deletions

View file

@ -1,6 +1,7 @@
mod state;
use super::tree;
use super::file_linked::FileLinked;
use uuid::Uuid;
use std::str::FromStr;
@ -40,15 +41,18 @@ fn build_tree(h: u32) -> Option<Box<tree::Tree<Uuid>>> {
/// TODO: Explain reasoning for bracket system against genetic algorithm.
pub fn run_bracket() {
let mut height = 1;
let mut tree: tree::Tree<Uuid> = *build_tree(height).expect("Error getting result from build_tree.");
let mut tree = FileLinked::new(*build_tree(height).expect("Error getting result from build_tree"), "temp")
.expect("Unable to create file linked object from tree");
// Building tree one node at a time, appending to the top.
loop {
println!("=========================================");
println!("Running bracket...");
height += 1;
tree = tree::Tree::new(Uuid::new_v4(), Some(Box::new(tree)), build_tree(height));
tree.run_simulation();
tree.replace(tree::Tree::new(Uuid::new_v4(), Some(Box::new(tree.readonly().clone())), build_tree(height)))
.expect("Error building up tree node");
tree.readonly().run_simulation();
if height == 3 {
println!("{}\n\n", tree);

View file

@ -1,6 +1,6 @@
use std::fs;
use std::str::FromStr;
use std::fmt::Display;
use std::fmt;
use std::string::String;
use std::io::Read;
use std::io::Write;
@ -10,44 +10,57 @@ pub struct FileLinked<T> {
path: String
}
impl<T: FromStr + Display> FileLinked<T> {
impl<T> FileLinked<T> where T: FromStr + fmt::Display + Default {
pub fn from_file(path: &str) -> Result<FileLinked<T>, String> {
let meta = fs::metadata(path)
.or(Err(format!("Path {} does not exist.", path)))?;
if meta.is_file() {
let mut file = fs::OpenOptions::new().read(true).open(path)
.or(Err(format!("Unable to open file {}", path)))?;
let mut s = String::new();
file.read_to_string(&mut s)
.or(Err(String::from("Unable to read from file.")))?;
let meta = fs::metadata(path);
let val = T::from_str(&s).or(Err(String::from("Unable to parse value from file.")))?;
match &meta {
Ok(m) if m.is_file() => {
let mut file = fs::OpenOptions::new().read(true).open(path)
.or(Err(format!("Unable to open file {}", path)))?;
let mut s = String::new();
file.read_to_string(&mut s)
.or(Err(String::from("Unable to read from file.")))?;
Ok(FileLinked {
val,
path: String::from(path)
})
} else {
Err(format!("{} is not a file.", path))
let val = T::from_str(&s).or(Err(String::from("Unable to parse value from file.")))?;
Ok(FileLinked {
val,
path: String::from(path)
})
},
Ok(_) => {
Err(format!("{} is not a file.", path))
},
_ => {
let result = FileLinked {
val: T::default(),
path: String::from(path)
};
result.write_data()?;
Ok(result)
}
}
}
pub fn new(val: T, path: &str) -> FileLinked<T> {
pub fn new(val: T, path: &str) -> Result<FileLinked<T>, String> {
let result = FileLinked {
val,
path: String::from(path)
};
result.write_data();
result.write_data()?;
result
Ok(result)
}
pub fn write_data(&self) -> Result<(), String> {
let mut file = fs::OpenOptions::new()
.write(true)
.create_new(true)
.create(true)
.truncate(true)
.open(&self.path)
.or(Err(format!("Unable to open path {}", self.path)))?;
@ -61,11 +74,23 @@ impl<T: FromStr + Display> FileLinked<T> {
&self.val
}
pub fn mutate<U, F: FnOnce(&mut T) -> U>(&mut self, op: F) -> U {
pub fn mutate<U, F: FnOnce(&mut T) -> U>(&mut self, op: F) -> Result<U, String> {
let result = op(&mut self.val);
self.write_data();
self.write_data()?;
result
Ok(result)
}
pub fn replace(&mut self, val: T) -> Result<(), String>{
self.val = val;
self.write_data()
}
}
impl<T: fmt::Display> fmt::Display for FileLinked<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.val)
}
}

View file

@ -28,6 +28,11 @@ fn main() {
println!("{} is a valid directory!", directory);
println!("Building tree for {}.", directory);
bracket::run_bracket();
println!("\n\nReading tree from temp file.");
let tree: file_linked::FileLinked<tree::Tree<uuid::Uuid>> = file_linked::FileLinked::from_file("temp")
.expect("Unable to read tree from existing file");
println!("Value read from file:\n{}", tree);
},
Ok(_) => println!("{} is not a valid directory!", directory),
_ => println!("{} does not exist!", directory)

View file

@ -2,6 +2,7 @@ use std::fmt;
use std::str::FromStr;
use regex::Regex;
#[derive(Default, Clone)]
pub struct Tree<T> {
pub val: T,
pub left: Option<Box<Tree<T>>>,