summaryrefslogtreecommitdiff
path: root/src/files.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/files.rs')
-rw-r--r--src/files.rs32
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(())
+}