diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-09-08 17:44:21 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-09-09 16:33:14 +0200 |
commit | 027fffb5b51d9c957741eab07889bc5aed50189b (patch) | |
tree | dd35ed1bf00bd42f1f2bef954752831eadd76a4e /libimagutil | |
parent | 501d1f38a2099efcb9580ede596fdb74cb9f23a4 (diff) |
Move edit_entry_with_cmd() to libimagutil
Diffstat (limited to 'libimagutil')
-rw-r--r-- | libimagutil/Cargo.toml | 1 | ||||
-rw-r--r-- | libimagutil/src/edit.rs | 36 | ||||
-rw-r--r-- | libimagutil/src/lib.rs | 2 |
3 files changed, 39 insertions, 0 deletions
diff --git a/libimagutil/Cargo.toml b/libimagutil/Cargo.toml index 53e0e0ee..87be5306 100644 --- a/libimagutil/Cargo.toml +++ b/libimagutil/Cargo.toml @@ -7,4 +7,5 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"] lazy_static = "0.1.15" log = "0.3" regex = "0.1" +tempfile = "2.1.1" diff --git a/libimagutil/src/edit.rs b/libimagutil/src/edit.rs new file mode 100644 index 00000000..ad23b6ed --- /dev/null +++ b/libimagutil/src/edit.rs @@ -0,0 +1,36 @@ +use std::io::Read; +use std::io::Seek; +use std::io::SeekFrom; +use std::io::Write; +use std::process::Command; +use std::io::Error as IOError; + +use tempfile::NamedTempFile; + +pub fn edit_in_tmpfile_with_command(mut cmd: Command, s: &mut String) -> Result<bool, IOError> { + let file = try!(NamedTempFile::new()); + let file_path = file.path(); + let mut file = try!(file.reopen()); + + try!(file.write_all(&s.clone().into_bytes()[..])); + try!(file.sync_data()); + + cmd.arg(file_path) + .status() + .and_then(|status| { + if status.success() { + file.sync_data() + .and_then(|_| file.seek(SeekFrom::Start(0))) + .and_then(|_| { + let mut new_s = String::new(); + let res = file.read_to_string(&mut new_s); + *s = new_s; + res + }) + .map(|_| true) + } else { + Ok(false) + } + }) +} + diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index 16183e7b..c0026c6f 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -16,9 +16,11 @@ #[macro_use] extern crate lazy_static; #[macro_use] extern crate log; extern crate regex; +extern crate tempfile; #[macro_use] mod log_result; pub mod debug_result; +pub mod edit; pub mod info_result; pub mod ismatch; pub mod iter; |