summaryrefslogtreecommitdiffstats
path: root/libimagentryedit
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-09-08 17:44:21 +0200
committerMatthias Beyer <mail@beyermatthias.de>2016-09-09 16:33:14 +0200
commit027fffb5b51d9c957741eab07889bc5aed50189b (patch)
treedd35ed1bf00bd42f1f2bef954752831eadd76a4e /libimagentryedit
parent501d1f38a2099efcb9580ede596fdb74cb9f23a4 (diff)
Move edit_entry_with_cmd() to libimagutil
Diffstat (limited to 'libimagentryedit')
-rw-r--r--libimagentryedit/Cargo.toml4
-rw-r--r--libimagentryedit/src/edit.rs54
-rw-r--r--libimagentryedit/src/error.rs1
-rw-r--r--libimagentryedit/src/lib.rs2
4 files changed, 20 insertions, 41 deletions
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 <mail@beyermatthias.de>"]
[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;