diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2017-05-13 19:19:06 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2017-05-13 19:19:06 +0200 |
commit | 36b72bfef7b7b8dea94546d11704ec529091bce1 (patch) | |
tree | 4a8cdae81db3ab44aa946046bbfbb87f96c360c5 /src/files.rs | |
parent | 27682d0ab246af8d0375853fdea44c38de2c2db4 (diff) |
Split into smaller portions
Diffstat (limited to 'src/files.rs')
-rw-r--r-- | src/files.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/files.rs b/src/files.rs new file mode 100644 index 0000000..87d295f --- /dev/null +++ b/src/files.rs @@ -0,0 +1,32 @@ +use json; +use std::io::prelude::*; +use std::fs::File; +use std::path::PathBuf; + +use actions::*; + +const COMMAND_FILE: &'static str = "command.txt"; +const PLACE_FILE: &'static str = "place.txt"; +const STATE_FILE: &'static str = "state.json"; + +pub fn read_file(working_dir: &PathBuf) -> Result<json::JsonValue, String> { + let state_path = working_dir.join(STATE_FILE); + let mut file = File::open(state_path.as_path()).map_err(|e| e.to_string())?; + let mut content = String::new(); + file.read_to_string(&mut content).map_err(|e| e.to_string())?; + json::parse(content.as_ref()).map_err(|e| e.to_string()) +} + +pub fn write_action(working_dir: &PathBuf, is_place_phase: bool, action: Action) -> Result<(), String> { + let filename = if is_place_phase { + PLACE_FILE + } + else { + COMMAND_FILE + }; + + let full_filename = working_dir.join(filename); + let mut file = File::create(full_filename.as_path()).map_err(|e| e.to_string())?; + writeln!(file, "{}", action).map_err(|e| e.to_string())?; + Ok(()) +} |