Merge pull request #263 from matthiasbeyer/libimagrt/edit

Libimagrt/edit
This commit is contained in:
Matthias Beyer 2016-03-24 14:23:55 +01:00
commit 8ed1ad6785
4 changed files with 83 additions and 2 deletions

View File

@ -9,6 +9,7 @@ toml = "0.1.27"
log = "0.3.4"
xdg-basedir = "0.2.2"
itertools = "0.4.6"
tempfile = "2.1.1"
[dependencies.libimagstore]
path = "../libimagstore"

65
libimagrt/src/edit.rs Normal file
View File

@ -0,0 +1,65 @@
use std::ops::DerefMut;
use runtime::Runtime;
use error::RuntimeError;
use error::RuntimeErrorKind;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Entry;
pub type EditResult<T> = Result<T, RuntimeError>;
pub trait Edit {
fn edit_content(&mut self, rt: &Runtime) -> EditResult<()>;
}
impl Edit for Entry {
fn edit_content(&mut self, rt: &Runtime) -> EditResult<()> {
edit_in_tmpfile(rt, self.get_content_mut())
.map(|_| ())
}
}
impl<'a> Edit for FileLockEntry<'a> {
fn edit_content(&mut self, rt: &Runtime) -> EditResult<()> {
self.deref_mut().edit_content(rt)
}
}
pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> EditResult<()> {
use tempfile::NamedTempFile;
use std::io::Seek;
use std::io::Read;
use std::io::SeekFrom;
use std::io::Write;
let file = try!(NamedTempFile::new());
let file_path = file.path();
let mut file = try!(file.reopen());
file.write_all(&s.clone().into_bytes()[..]);
try!(file.sync_data());
if let Some(mut editor) = rt.editor() {
let exit_status = editor.arg(file_path).status();
match exit_status.map(|s| s.success()) {
Ok(true) => {
file.sync_data()
.and_then(|_| file.seek(SeekFrom::Start(0)))
.and_then(|_| file.read_to_string(s))
.map(|_| ())
.map_err(|e| RuntimeError::new(RuntimeErrorKind::IOError, Some(Box::new(e))))
},
Ok(false) => Err(RuntimeError::new(RuntimeErrorKind::ProcessExitFailure, None)),
Err(e) => Err(RuntimeError::new(RuntimeErrorKind::IOError, Some(Box::new(e)))),
}
} else {
Err(RuntimeError::new(RuntimeErrorKind::Instantiate, None))
}
}

View File

@ -2,10 +2,13 @@ use std::error::Error;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Error as FmtError;
use std::io::Error as IOError;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum RuntimeErrorKind {
Instantiate,
IOError,
ProcessExitFailure,
// more?
}
@ -29,7 +32,9 @@ impl RuntimeError {
fn runtime_error_kind_as_str(e: &RuntimeErrorKind) -> &'static str {
match e {
&RuntimeErrorKind::Instantiate => "Could not instantiate",
&RuntimeErrorKind::Instantiate => "Could not instantiate",
&RuntimeErrorKind::IOError => "IO Error",
&RuntimeErrorKind::ProcessExitFailure => "Process exited with failure",
}
}
@ -54,3 +59,11 @@ impl Error for RuntimeError {
}
impl From<IOError> for RuntimeError {
fn from(ioe: IOError) -> RuntimeError {
RuntimeError::new(RuntimeErrorKind::IOError, Some(Box::new(ioe)))
}
}

View File

@ -1,6 +1,7 @@
#[macro_use] extern crate log;
#[macro_use] extern crate itertools;
#[cfg(unix)] extern crate xdg_basedir;
extern crate tempfile;
extern crate clap;
extern crate toml;
@ -9,8 +10,9 @@ extern crate libimagstore;
extern crate libimagutil;
mod configuration;
mod error;
mod logger;
pub mod edit;
pub mod error;
pub mod runtime;