From 027fffb5b51d9c957741eab07889bc5aed50189b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 8 Sep 2016 17:44:21 +0200 Subject: [PATCH] Move edit_entry_with_cmd() to libimagutil --- libimagentryedit/Cargo.toml | 4 ++- libimagentryedit/src/edit.rs | 54 ++++++++++------------------------- libimagentryedit/src/error.rs | 1 + libimagentryedit/src/lib.rs | 2 +- libimagutil/Cargo.toml | 1 + libimagutil/src/edit.rs | 36 +++++++++++++++++++++++ libimagutil/src/lib.rs | 2 ++ 7 files changed, 59 insertions(+), 41 deletions(-) create mode 100644 libimagutil/src/edit.rs diff --git a/libimagentryedit/Cargo.toml b/libimagentryedit/Cargo.toml index 0c79700e..076519fe 100644 --- a/libimagentryedit/Cargo.toml +++ b/libimagentryedit/Cargo.toml @@ -4,7 +4,6 @@ version = "0.2.0" authors = ["Matthias Beyer "] [dependencies] -tempfile = "2.1.1" [dependencies.libimagerror] path = "../libimagerror" @@ -15,3 +14,6 @@ path = "../libimagrt" [dependencies.libimagstore] path = "../libimagstore" +[dependencies.libimagutil] +path = "../libimagutil" + diff --git a/libimagentryedit/src/edit.rs b/libimagentryedit/src/edit.rs index cc9bbc64..08e2bfb4 100644 --- a/libimagentryedit/src/edit.rs +++ b/libimagentryedit/src/edit.rs @@ -39,44 +39,20 @@ impl<'a> Edit for FileLockEntry<'a> { } pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> Result<()> { - edit_in_tmpfile_with_command(rt.editor(), s) -} - -pub fn edit_in_tmpfile_with_command(cmd: Command, s: &mut String) -> Result<()> { - use tempfile::NamedTempFile; - use std::io::Seek; - use std::io::Read; - use std::io::SeekFrom; - use std::io::Write; - - let file = try!(NamedTempFile::new().map_err_into(EditErrorKind::IOError)); - let file_path = file.path(); - let mut file = try!(file.reopen().map_err_into(EditErrorKind::IOError)); - - try!(file.write_all(&s.clone().into_bytes()[..]).map_err_into(EditErrorKind::IOError)); - try!(file.sync_data().map_err_into(EditErrorKind::IOError)); - - if let Some(mut editor) = rt.editor() { - let exit_status = editor.arg(file_path).status(); - - match exit_status.map(|s| s.success()).map_err(Box::new) { - Ok(true) => { - 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(|_| ()) - .map_err_into(EditErrorKind::IOError) - }, - Ok(false) => Err(EditErrorKind::ProcessExitFailure.into()), - Err(e) => Err(EditErrorKind::IOError.into_error_with_cause(e)), - } - } else { - Err(EditErrorKind::InstantiateError.into()) - } + use libimagutil::edit::edit_in_tmpfile_with_command; + + rt.editor() + .ok_or(EditErrorKind::NoEditor.into_error()) + .and_then(|editor| { + edit_in_tmpfile_with_command(editor, s) + .map_err_into(EditErrorKind::IOError) + .and_then(|worked| { + if !worked { + Err(EditErrorKind::ProcessExitFailure.into()) + } else { + Ok(()) + } + }) + }) } diff --git a/libimagentryedit/src/error.rs b/libimagentryedit/src/error.rs index 0cd675e0..776a51fb 100644 --- a/libimagentryedit/src/error.rs +++ b/libimagentryedit/src/error.rs @@ -1,6 +1,7 @@ generate_error_module!( generate_error_types!(EditError, EditErrorKind, IOError => "IO Error", + NoEditor => "No editor set", ProcessExitFailure => "Process did not exit properly", InstantiateError => "Instantation error" ); diff --git a/libimagentryedit/src/lib.rs b/libimagentryedit/src/lib.rs index 62cff1e4..d0a26038 100644 --- a/libimagentryedit/src/lib.rs +++ b/libimagentryedit/src/lib.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate libimagerror; extern crate libimagstore; extern crate libimagrt; -extern crate tempfile; +extern crate libimagutil; pub mod edit; pub mod error; 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 "] 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 { + 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;