imag/libimagentryedit/src/edit.rs

59 lines
1.3 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<()> {
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(())
}
})
})
2016-03-24 10:50:37 +00:00
}