imag/libimagentryedit/src/edit.rs

78 lines
2.2 KiB
Rust
Raw Normal View History

2016-03-24 10:50:37 +00:00
use std::ops::DerefMut;
2016-05-27 08:27:06 +00:00
use libimagerror::into::IntoError;
2016-08-09 10:35:16 +00:00
use libimagrt::runtime::Runtime;
use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry;
2016-05-27 08:27:06 +00:00
2016-08-09 10:35:16 +00:00
use result::Result;
use error::EditErrorKind;
use error::MapErrInto;
2016-03-24 10:50:37 +00:00
pub trait Edit {
2016-08-09 10:35:16 +00:00
fn edit_content(&mut self, rt: &Runtime) -> Result<()>;
2016-03-24 10:50:37 +00:00
}
2016-04-06 12:40:36 +00:00
impl Edit for String {
2016-08-09 10:35:16 +00:00
fn edit_content(&mut self, rt: &Runtime) -> Result<()> {
2016-04-06 12:40:36 +00:00
edit_in_tmpfile(rt, self).map(|_| ())
}
}
2016-03-24 10:50:37 +00:00
impl Edit for Entry {
2016-08-09 10:35:16 +00:00
fn edit_content(&mut self, rt: &Runtime) -> Result<()> {
2016-03-24 10:50:37 +00:00
edit_in_tmpfile(rt, self.get_content_mut())
.map(|_| ())
}
}
impl<'a> Edit for FileLockEntry<'a> {
2016-08-09 10:35:16 +00:00
fn edit_content(&mut self, rt: &Runtime) -> Result<()> {
2016-03-24 10:50:37 +00:00
self.deref_mut().edit_content(rt)
}
}
2016-08-09 10:35:16 +00:00
pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> Result<()> {
2016-03-24 10:50:37 +00:00
use tempfile::NamedTempFile;
use std::io::Seek;
use std::io::Read;
use std::io::SeekFrom;
use std::io::Write;
2016-08-09 10:35:16 +00:00
let file = try!(NamedTempFile::new().map_err_into(EditErrorKind::IOError));
2016-03-24 10:50:37 +00:00
let file_path = file.path();
2016-08-09 10:35:16 +00:00
let mut file = try!(file.reopen().map_err_into(EditErrorKind::IOError));
2016-03-24 10:50:37 +00:00
2016-08-09 10:35:16 +00:00
try!(file.write_all(&s.clone().into_bytes()[..]).map_err_into(EditErrorKind::IOError));
try!(file.sync_data().map_err_into(EditErrorKind::IOError));
2016-03-24 10:50:37 +00:00
if let Some(mut editor) = rt.editor() {
let exit_status = editor.arg(file_path).status();
2016-05-27 08:27:06 +00:00
match exit_status.map(|s| s.success()).map_err(Box::new) {
Ok(true) => {
2016-03-24 10:50:37 +00:00
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
})
2016-03-24 10:50:37 +00:00
.map(|_| ())
2016-08-09 10:35:16 +00:00
.map_err_into(EditErrorKind::IOError)
2016-03-24 10:50:37 +00:00
},
2016-08-09 10:35:16 +00:00
Ok(false) => Err(EditErrorKind::ProcessExitFailure.into()),
Err(e) => Err(EditErrorKind::IOError.into_error_with_cause(e)),
2016-03-24 10:50:37 +00:00
}
} else {
2016-08-09 10:35:16 +00:00
Err(EditErrorKind::InstantiateError.into())
2016-03-24 10:50:37 +00:00
}
}