From 15eac852e70a6061b2742567a0a87a03ea698a85 Mon Sep 17 00:00:00 2001 From: Zetok Zalbavar Date: Fri, 10 Jun 2016 12:02:37 +0100 Subject: [PATCH] fix: file didn't get truncated before writing replaced version --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 12 +++++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f767735..942716a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [root] name = "sterminator" -version = "0.0.0" +version = "0.0.1" dependencies = [ "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index d1cfeae..06cddee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sterminator" -version = "0.0.0" +version = "0.0.1" authors = ["Zetok Zalbavar "] [dependencies] diff --git a/src/main.rs b/src/main.rs index 124e0b9..9e23a14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,15 +102,15 @@ fn main() { let mut www = String::new(); drop(web_file.read_to_string(&mut www)); - + for (original, translation) in json.as_object() .expect(&format!("JSON {} is not an object!", json)) { - let tr = translation.as_string() - .expect(&format!("{:?} is not a string!", translation)); + // use original if not string + let tr = translation.as_string().unwrap_or(original); // Replaces `[[[String]]]` with supplied `String`. - // + // // If there is no matching `[[[String]]]`, there's no change. if tr.is_empty() { www = www.replace(&format!("[[[{}]]]", original), original); @@ -120,7 +120,9 @@ fn main() { } // write the file down + drop(web_file.set_len(0).expect( + &format!("Failed to truncate file {:?}", web_file))); web_file.write_all(www.as_bytes()) .expect(&format!("Failed to write to file {:?}", web_file)); - println!("{} done.", &args[1]); + println!("Done: {}", &args[1]); }