Merge pull request #643 from matthiasbeyer/libimagentryedit/init

Libimagentryedit/init
This commit is contained in:
Matthias Beyer 2016-08-09 18:34:48 +02:00 committed by GitHub
commit 0cb09a627d
21 changed files with 90 additions and 30 deletions

View file

@ -15,6 +15,9 @@ path = "../libimagrt"
[dependencies.libimagdiary]
path = "../libimagdiary"
[dependencies.libimagentryedit]
path = "../libimagentryedit"
[dependencies.libimagentrylist]
path = "../libimagentrylist"

View file

@ -4,7 +4,7 @@ use libimagdiary::diary::Diary;
use libimagdiary::diaryid::DiaryId;
use libimagdiary::error::DiaryError as DE;
use libimagdiary::error::DiaryErrorKind as DEK;
use libimagrt::edit::Edit;
use libimagentryedit::edit::Edit;
use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error;
use libimagdiary::entry::Entry;

View file

@ -5,7 +5,7 @@ use libimagdiary::diary::Diary;
use libimagdiary::diaryid::DiaryId;
use libimagdiary::error::DiaryError as DE;
use libimagdiary::error::DiaryErrorKind as DEK;
use libimagrt::edit::Edit;
use libimagentryedit::edit::Edit;
use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error;
use libimagtimeui::datetime::DateTime;

View file

@ -19,6 +19,7 @@ extern crate clap;
extern crate chrono;
extern crate libimagdiary;
extern crate libimagentryedit;
extern crate libimagentrylist;
extern crate libimagentryview;
extern crate libimaginteraction;

View file

@ -15,6 +15,9 @@ path = "../libimagrt"
[dependencies.libimagnotes]
path = "../libimagnotes"
[dependencies.libimagentryedit]
path = "../libimagentryedit"
[dependencies.libimagentrytag]
path = "../libimagentrytag"

View file

@ -5,12 +5,13 @@ extern crate semver;
extern crate libimagnotes;
extern crate libimagrt;
extern crate libimagentryedit;
extern crate libimagentrytag;
extern crate libimagerror;
use std::process::exit;
use libimagrt::edit::Edit;
use libimagentryedit::edit::Edit;
use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup;
use libimagnotes::note::Note;

View file

@ -24,3 +24,6 @@ path = "../libimagutil"
[dependencies.libimagrt]
path = "../libimagrt"
[dependencies.libimagentryedit]
path = "../libimagentryedit"

View file

@ -2,8 +2,8 @@ use std::ops::Deref;
use std::ops::DerefMut;
use libimagstore::store::FileLockEntry;
use libimagrt::edit::Edit;
use libimagrt::edit::EditResult;
use libimagentryedit::edit::Edit;
use libimagentryedit::result::Result as EditResult;
use libimagrt::runtime::Runtime;
use diaryid::DiaryId;

View file

@ -25,6 +25,7 @@ extern crate itertools;
#[macro_use] extern crate libimagstore;
#[macro_use] extern crate libimagutil;
#[macro_use] extern crate libimagerror;
extern crate libimagentryedit;
extern crate libimagrt;
module_entry_path_mod!("diary", "0.2.0");

View file

@ -0,0 +1,17 @@
[package]
name = "libimagentryedit"
version = "0.2.0"
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies]
tempfile = "2.1.1"
[dependencies.libimagerror]
path = "../libimagerror"
[dependencies.libimagrt]
path = "../libimagrt"
[dependencies.libimagstore]
path = "../libimagstore"

View file

@ -1,23 +1,21 @@
use std::ops::DerefMut;
use runtime::Runtime;
use error::RuntimeError;
use error::RuntimeErrorKind;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Entry;
use libimagerror::into::IntoError;
use libimagrt::runtime::Runtime;
use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry;
pub type EditResult<T> = Result<T, RuntimeError>;
use result::Result;
use error::EditErrorKind;
use error::MapErrInto;
pub trait Edit {
fn edit_content(&mut self, rt: &Runtime) -> EditResult<()>;
fn edit_content(&mut self, rt: &Runtime) -> Result<()>;
}
impl Edit for String {
fn edit_content(&mut self, rt: &Runtime) -> EditResult<()> {
fn edit_content(&mut self, rt: &Runtime) -> Result<()> {
edit_in_tmpfile(rt, self).map(|_| ())
}
@ -25,7 +23,7 @@ impl Edit for String {
impl Edit for Entry {
fn edit_content(&mut self, rt: &Runtime) -> EditResult<()> {
fn edit_content(&mut self, rt: &Runtime) -> Result<()> {
edit_in_tmpfile(rt, self.get_content_mut())
.map(|_| ())
}
@ -34,25 +32,25 @@ impl Edit for Entry {
impl<'a> Edit for FileLockEntry<'a> {
fn edit_content(&mut self, rt: &Runtime) -> EditResult<()> {
fn edit_content(&mut self, rt: &Runtime) -> Result<()> {
self.deref_mut().edit_content(rt)
}
}
pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> EditResult<()> {
pub fn edit_in_tmpfile(rt: &Runtime, 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());
let file = try!(NamedTempFile::new().map_err_into(EditErrorKind::IOError));
let file_path = file.path();
let mut file = try!(file.reopen());
let mut file = try!(file.reopen().map_err_into(EditErrorKind::IOError));
try!(file.write_all(&s.clone().into_bytes()[..]));
try!(file.sync_data());
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();
@ -68,13 +66,12 @@ pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> EditResult<()> {
res
})
.map(|_| ())
.map_err(Box::new)
.map_err(|e| RuntimeErrorKind::IOError.into_error_with_cause(e))
.map_err_into(EditErrorKind::IOError)
},
Ok(false) => Err(RuntimeErrorKind::ProcessExitFailure.into()),
Err(e) => Err(RuntimeErrorKind::IOError.into_error_with_cause(e)),
Ok(false) => Err(EditErrorKind::ProcessExitFailure.into()),
Err(e) => Err(EditErrorKind::IOError.into_error_with_cause(e)),
}
} else {
Err(RuntimeErrorKind::Instantiate.into())
Err(EditErrorKind::InstantiateError.into())
}
}

View file

@ -0,0 +1,12 @@
generate_error_module!(
generate_error_types!(EditError, EditErrorKind,
IOError => "IO Error",
ProcessExitFailure => "Process did not exit properly",
InstantiateError => "Instantation error"
);
);
pub use self::error::EditError;
pub use self::error::EditErrorKind;
pub use self::error::MapErrInto;

View file

@ -0,0 +1,8 @@
#[macro_use] extern crate libimagerror;
extern crate libimagstore;
extern crate libimagrt;
extern crate tempfile;
pub mod edit;
pub mod error;
pub mod result;

View file

@ -0,0 +1,6 @@
use std::result::Result as RResult;
use error::EditError;
pub type Result<T> = RResult<T, EditError>;

View file

@ -17,3 +17,6 @@ path = "../libimagstore"
[dependencies.libimagerror]
path = "../libimagerror"
[dependencies.libimagentryedit]
path = "../libimagentryedit"

View file

@ -1,6 +1,6 @@
use libimagstore::store::Entry;
use libimagrt::runtime::Runtime;
use libimagrt::edit::edit_in_tmpfile;
use libimagentryedit::edit::edit_in_tmpfile;
use viewer::Viewer;
use result::Result;

View file

@ -21,6 +21,7 @@ extern crate toml;
extern crate libimagstore;
extern crate libimagrt;
#[macro_use] extern crate libimagerror;
extern crate libimagentryedit;
pub mod error;
pub mod builtin;

View file

@ -17,6 +17,9 @@ path = "../libimagerror"
[dependencies.libimagrt]
path = "../libimagrt"
[dependencies.libimagentryedit]
path = "../libimagentryedit"
[dependencies.libimagentrytag]
path = "../libimagentrytag"

View file

@ -21,6 +21,7 @@ extern crate toml;
extern crate libimagrt;
#[macro_use] extern crate libimagstore;
#[macro_use] extern crate libimagerror;
extern crate libimagentryedit;
extern crate libimagentrytag;
module_entry_path_mod!("notes", "0.2.0");

View file

@ -4,7 +4,8 @@ use std::ops::Deref;
use toml::Value;
use libimagrt::runtime::Runtime;
use libimagrt::edit::{Edit, EditResult};
use libimagentryedit::edit::Edit;
use libimagentryedit::result::Result as EditResult;
use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreId;
use libimagstore::storeid::StoreIdIterator;

View file

@ -32,7 +32,6 @@ extern crate libimagutil;
mod configuration;
mod logger;
pub mod edit;
pub mod error;
pub mod runtime;
pub mod setup;