Merge pull request #643 from matthiasbeyer/libimagentryedit/init
Libimagentryedit/init
This commit is contained in:
commit
0cb09a627d
21 changed files with 90 additions and 30 deletions
|
@ -15,6 +15,9 @@ path = "../libimagrt"
|
||||||
[dependencies.libimagdiary]
|
[dependencies.libimagdiary]
|
||||||
path = "../libimagdiary"
|
path = "../libimagdiary"
|
||||||
|
|
||||||
|
[dependencies.libimagentryedit]
|
||||||
|
path = "../libimagentryedit"
|
||||||
|
|
||||||
[dependencies.libimagentrylist]
|
[dependencies.libimagentrylist]
|
||||||
path = "../libimagentrylist"
|
path = "../libimagentrylist"
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use libimagdiary::diary::Diary;
|
||||||
use libimagdiary::diaryid::DiaryId;
|
use libimagdiary::diaryid::DiaryId;
|
||||||
use libimagdiary::error::DiaryError as DE;
|
use libimagdiary::error::DiaryError as DE;
|
||||||
use libimagdiary::error::DiaryErrorKind as DEK;
|
use libimagdiary::error::DiaryErrorKind as DEK;
|
||||||
use libimagrt::edit::Edit;
|
use libimagentryedit::edit::Edit;
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagerror::trace::trace_error;
|
use libimagerror::trace::trace_error;
|
||||||
use libimagdiary::entry::Entry;
|
use libimagdiary::entry::Entry;
|
||||||
|
|
|
@ -5,7 +5,7 @@ use libimagdiary::diary::Diary;
|
||||||
use libimagdiary::diaryid::DiaryId;
|
use libimagdiary::diaryid::DiaryId;
|
||||||
use libimagdiary::error::DiaryError as DE;
|
use libimagdiary::error::DiaryError as DE;
|
||||||
use libimagdiary::error::DiaryErrorKind as DEK;
|
use libimagdiary::error::DiaryErrorKind as DEK;
|
||||||
use libimagrt::edit::Edit;
|
use libimagentryedit::edit::Edit;
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagerror::trace::trace_error;
|
use libimagerror::trace::trace_error;
|
||||||
use libimagtimeui::datetime::DateTime;
|
use libimagtimeui::datetime::DateTime;
|
||||||
|
|
|
@ -19,6 +19,7 @@ extern crate clap;
|
||||||
extern crate chrono;
|
extern crate chrono;
|
||||||
|
|
||||||
extern crate libimagdiary;
|
extern crate libimagdiary;
|
||||||
|
extern crate libimagentryedit;
|
||||||
extern crate libimagentrylist;
|
extern crate libimagentrylist;
|
||||||
extern crate libimagentryview;
|
extern crate libimagentryview;
|
||||||
extern crate libimaginteraction;
|
extern crate libimaginteraction;
|
||||||
|
|
|
@ -15,6 +15,9 @@ path = "../libimagrt"
|
||||||
[dependencies.libimagnotes]
|
[dependencies.libimagnotes]
|
||||||
path = "../libimagnotes"
|
path = "../libimagnotes"
|
||||||
|
|
||||||
|
[dependencies.libimagentryedit]
|
||||||
|
path = "../libimagentryedit"
|
||||||
|
|
||||||
[dependencies.libimagentrytag]
|
[dependencies.libimagentrytag]
|
||||||
path = "../libimagentrytag"
|
path = "../libimagentrytag"
|
||||||
|
|
||||||
|
|
|
@ -5,12 +5,13 @@ extern crate semver;
|
||||||
|
|
||||||
extern crate libimagnotes;
|
extern crate libimagnotes;
|
||||||
extern crate libimagrt;
|
extern crate libimagrt;
|
||||||
|
extern crate libimagentryedit;
|
||||||
extern crate libimagentrytag;
|
extern crate libimagentrytag;
|
||||||
extern crate libimagerror;
|
extern crate libimagerror;
|
||||||
|
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
use libimagrt::edit::Edit;
|
use libimagentryedit::edit::Edit;
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagrt::setup::generate_runtime_setup;
|
use libimagrt::setup::generate_runtime_setup;
|
||||||
use libimagnotes::note::Note;
|
use libimagnotes::note::Note;
|
||||||
|
|
|
@ -24,3 +24,6 @@ path = "../libimagutil"
|
||||||
[dependencies.libimagrt]
|
[dependencies.libimagrt]
|
||||||
path = "../libimagrt"
|
path = "../libimagrt"
|
||||||
|
|
||||||
|
[dependencies.libimagentryedit]
|
||||||
|
path = "../libimagentryedit"
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,8 @@ use std::ops::Deref;
|
||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
|
|
||||||
use libimagstore::store::FileLockEntry;
|
use libimagstore::store::FileLockEntry;
|
||||||
use libimagrt::edit::Edit;
|
use libimagentryedit::edit::Edit;
|
||||||
use libimagrt::edit::EditResult;
|
use libimagentryedit::result::Result as EditResult;
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
|
|
||||||
use diaryid::DiaryId;
|
use diaryid::DiaryId;
|
||||||
|
|
|
@ -25,6 +25,7 @@ extern crate itertools;
|
||||||
#[macro_use] extern crate libimagstore;
|
#[macro_use] extern crate libimagstore;
|
||||||
#[macro_use] extern crate libimagutil;
|
#[macro_use] extern crate libimagutil;
|
||||||
#[macro_use] extern crate libimagerror;
|
#[macro_use] extern crate libimagerror;
|
||||||
|
extern crate libimagentryedit;
|
||||||
extern crate libimagrt;
|
extern crate libimagrt;
|
||||||
|
|
||||||
module_entry_path_mod!("diary", "0.2.0");
|
module_entry_path_mod!("diary", "0.2.0");
|
||||||
|
|
17
libimagentryedit/Cargo.toml
Normal file
17
libimagentryedit/Cargo.toml
Normal 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"
|
||||||
|
|
|
@ -1,23 +1,21 @@
|
||||||
use std::ops::DerefMut;
|
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 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 {
|
pub trait Edit {
|
||||||
fn edit_content(&mut self, rt: &Runtime) -> EditResult<()>;
|
fn edit_content(&mut self, rt: &Runtime) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Edit for String {
|
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(|_| ())
|
edit_in_tmpfile(rt, self).map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +23,7 @@ impl Edit for String {
|
||||||
|
|
||||||
impl Edit for Entry {
|
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())
|
edit_in_tmpfile(rt, self.get_content_mut())
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
}
|
}
|
||||||
|
@ -34,25 +32,25 @@ impl Edit for Entry {
|
||||||
|
|
||||||
impl<'a> Edit for FileLockEntry<'a> {
|
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)
|
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 tempfile::NamedTempFile;
|
||||||
use std::io::Seek;
|
use std::io::Seek;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::io::SeekFrom;
|
use std::io::SeekFrom;
|
||||||
use std::io::Write;
|
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 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.write_all(&s.clone().into_bytes()[..]).map_err_into(EditErrorKind::IOError));
|
||||||
try!(file.sync_data());
|
try!(file.sync_data().map_err_into(EditErrorKind::IOError));
|
||||||
|
|
||||||
if let Some(mut editor) = rt.editor() {
|
if let Some(mut editor) = rt.editor() {
|
||||||
let exit_status = editor.arg(file_path).status();
|
let exit_status = editor.arg(file_path).status();
|
||||||
|
@ -68,13 +66,12 @@ pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> EditResult<()> {
|
||||||
res
|
res
|
||||||
})
|
})
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.map_err(Box::new)
|
.map_err_into(EditErrorKind::IOError)
|
||||||
.map_err(|e| RuntimeErrorKind::IOError.into_error_with_cause(e))
|
|
||||||
},
|
},
|
||||||
Ok(false) => Err(RuntimeErrorKind::ProcessExitFailure.into()),
|
Ok(false) => Err(EditErrorKind::ProcessExitFailure.into()),
|
||||||
Err(e) => Err(RuntimeErrorKind::IOError.into_error_with_cause(e)),
|
Err(e) => Err(EditErrorKind::IOError.into_error_with_cause(e)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(RuntimeErrorKind::Instantiate.into())
|
Err(EditErrorKind::InstantiateError.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
12
libimagentryedit/src/error.rs
Normal file
12
libimagentryedit/src/error.rs
Normal 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;
|
||||||
|
|
8
libimagentryedit/src/lib.rs
Normal file
8
libimagentryedit/src/lib.rs
Normal 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;
|
6
libimagentryedit/src/result.rs
Normal file
6
libimagentryedit/src/result.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
use std::result::Result as RResult;
|
||||||
|
|
||||||
|
use error::EditError;
|
||||||
|
|
||||||
|
pub type Result<T> = RResult<T, EditError>;
|
||||||
|
|
|
@ -17,3 +17,6 @@ path = "../libimagstore"
|
||||||
[dependencies.libimagerror]
|
[dependencies.libimagerror]
|
||||||
path = "../libimagerror"
|
path = "../libimagerror"
|
||||||
|
|
||||||
|
[dependencies.libimagentryedit]
|
||||||
|
path = "../libimagentryedit"
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use libimagstore::store::Entry;
|
use libimagstore::store::Entry;
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagrt::edit::edit_in_tmpfile;
|
use libimagentryedit::edit::edit_in_tmpfile;
|
||||||
|
|
||||||
use viewer::Viewer;
|
use viewer::Viewer;
|
||||||
use result::Result;
|
use result::Result;
|
||||||
|
|
|
@ -21,6 +21,7 @@ extern crate toml;
|
||||||
extern crate libimagstore;
|
extern crate libimagstore;
|
||||||
extern crate libimagrt;
|
extern crate libimagrt;
|
||||||
#[macro_use] extern crate libimagerror;
|
#[macro_use] extern crate libimagerror;
|
||||||
|
extern crate libimagentryedit;
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod builtin;
|
pub mod builtin;
|
||||||
|
|
|
@ -17,6 +17,9 @@ path = "../libimagerror"
|
||||||
[dependencies.libimagrt]
|
[dependencies.libimagrt]
|
||||||
path = "../libimagrt"
|
path = "../libimagrt"
|
||||||
|
|
||||||
|
[dependencies.libimagentryedit]
|
||||||
|
path = "../libimagentryedit"
|
||||||
|
|
||||||
[dependencies.libimagentrytag]
|
[dependencies.libimagentrytag]
|
||||||
path = "../libimagentrytag"
|
path = "../libimagentrytag"
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ extern crate toml;
|
||||||
extern crate libimagrt;
|
extern crate libimagrt;
|
||||||
#[macro_use] extern crate libimagstore;
|
#[macro_use] extern crate libimagstore;
|
||||||
#[macro_use] extern crate libimagerror;
|
#[macro_use] extern crate libimagerror;
|
||||||
|
extern crate libimagentryedit;
|
||||||
extern crate libimagentrytag;
|
extern crate libimagentrytag;
|
||||||
|
|
||||||
module_entry_path_mod!("notes", "0.2.0");
|
module_entry_path_mod!("notes", "0.2.0");
|
||||||
|
|
|
@ -4,7 +4,8 @@ use std::ops::Deref;
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
|
|
||||||
use libimagrt::runtime::Runtime;
|
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::IntoStoreId;
|
||||||
use libimagstore::storeid::StoreId;
|
use libimagstore::storeid::StoreId;
|
||||||
use libimagstore::storeid::StoreIdIterator;
|
use libimagstore::storeid::StoreIdIterator;
|
||||||
|
|
|
@ -32,7 +32,6 @@ extern crate libimagutil;
|
||||||
mod configuration;
|
mod configuration;
|
||||||
mod logger;
|
mod logger;
|
||||||
|
|
||||||
pub mod edit;
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod runtime;
|
pub mod runtime;
|
||||||
pub mod setup;
|
pub mod setup;
|
||||||
|
|
Loading…
Reference in a new issue