From 4a317b253c6938b3ad86eae826b523888bfe9298 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 9 Aug 2016 12:28:10 +0200 Subject: [PATCH 1/7] Initial import --- libimagentryedit/Cargo.toml | 6 ++++++ libimagentryedit/src/lib.rs | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 libimagentryedit/Cargo.toml create mode 100644 libimagentryedit/src/lib.rs diff --git a/libimagentryedit/Cargo.toml b/libimagentryedit/Cargo.toml new file mode 100644 index 00000000..ba86bfb7 --- /dev/null +++ b/libimagentryedit/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "libimagentryedit" +version = "0.2.0" +authors = ["Matthias Beyer "] + +[dependencies] diff --git a/libimagentryedit/src/lib.rs b/libimagentryedit/src/lib.rs new file mode 100644 index 00000000..cdfbe1aa --- /dev/null +++ b/libimagentryedit/src/lib.rs @@ -0,0 +1,6 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + } +} From e250ca632e2253958672691c80708ab7f63f5c9c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 9 Aug 2016 12:28:20 +0200 Subject: [PATCH 2/7] Move edit code from libimagrt --- {libimagrt => libimagentryedit}/src/edit.rs | 0 libimagentryedit/src/lib.rs | 7 +------ libimagrt/src/lib.rs | 1 - 3 files changed, 1 insertion(+), 7 deletions(-) rename {libimagrt => libimagentryedit}/src/edit.rs (100%) diff --git a/libimagrt/src/edit.rs b/libimagentryedit/src/edit.rs similarity index 100% rename from libimagrt/src/edit.rs rename to libimagentryedit/src/edit.rs diff --git a/libimagentryedit/src/lib.rs b/libimagentryedit/src/lib.rs index cdfbe1aa..99946a87 100644 --- a/libimagentryedit/src/lib.rs +++ b/libimagentryedit/src/lib.rs @@ -1,6 +1 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - } -} +pub mod edit; diff --git a/libimagrt/src/lib.rs b/libimagrt/src/lib.rs index d1d7e249..2aea566b 100644 --- a/libimagrt/src/lib.rs +++ b/libimagrt/src/lib.rs @@ -32,7 +32,6 @@ extern crate libimagutil; mod configuration; mod logger; -pub mod edit; pub mod error; pub mod runtime; pub mod setup; From 8fd52d0ef1a2725a558bd6f8fb95f614d2193fab Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 9 Aug 2016 12:31:56 +0200 Subject: [PATCH 3/7] Add dependencies --- libimagentryedit/Cargo.toml | 11 +++++++++++ libimagentryedit/src/lib.rs | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/libimagentryedit/Cargo.toml b/libimagentryedit/Cargo.toml index ba86bfb7..0c79700e 100644 --- a/libimagentryedit/Cargo.toml +++ b/libimagentryedit/Cargo.toml @@ -4,3 +4,14 @@ version = "0.2.0" authors = ["Matthias Beyer "] [dependencies] +tempfile = "2.1.1" + +[dependencies.libimagerror] +path = "../libimagerror" + +[dependencies.libimagrt] +path = "../libimagrt" + +[dependencies.libimagstore] +path = "../libimagstore" + diff --git a/libimagentryedit/src/lib.rs b/libimagentryedit/src/lib.rs index 99946a87..7819d8d8 100644 --- a/libimagentryedit/src/lib.rs +++ b/libimagentryedit/src/lib.rs @@ -1 +1,6 @@ +#[macro_use] extern crate libimagerror; +extern crate libimagstore; +extern crate libimagrt; +extern crate tempfile; + pub mod edit; From 96a22a0d47b02df01623a1003e74db1c211c9e84 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 9 Aug 2016 12:32:26 +0200 Subject: [PATCH 4/7] Add error module, result module --- libimagentryedit/src/error.rs | 10 ++++++++++ libimagentryedit/src/lib.rs | 2 ++ libimagentryedit/src/result.rs | 6 ++++++ 3 files changed, 18 insertions(+) create mode 100644 libimagentryedit/src/error.rs create mode 100644 libimagentryedit/src/result.rs diff --git a/libimagentryedit/src/error.rs b/libimagentryedit/src/error.rs new file mode 100644 index 00000000..07087e56 --- /dev/null +++ b/libimagentryedit/src/error.rs @@ -0,0 +1,10 @@ +generate_error_module!( + generate_error_types!(EditError, EditErrorKind, + IOError => "IO Error" + ); +); + +pub use self::error::EditError; +pub use self::error::EditErrorKind; +pub use self::error::MapErrInto; + diff --git a/libimagentryedit/src/lib.rs b/libimagentryedit/src/lib.rs index 7819d8d8..62cff1e4 100644 --- a/libimagentryedit/src/lib.rs +++ b/libimagentryedit/src/lib.rs @@ -4,3 +4,5 @@ extern crate libimagrt; extern crate tempfile; pub mod edit; +pub mod error; +pub mod result; diff --git a/libimagentryedit/src/result.rs b/libimagentryedit/src/result.rs new file mode 100644 index 00000000..7e58f0fe --- /dev/null +++ b/libimagentryedit/src/result.rs @@ -0,0 +1,6 @@ +use std::result::Result as RResult; + +use error::EditError; + +pub type Result = RResult; + From 8a255484d045b38a140e65636deaada8b01217f4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 9 Aug 2016 12:35:00 +0200 Subject: [PATCH 5/7] Add more error kinds --- libimagentryedit/src/error.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libimagentryedit/src/error.rs b/libimagentryedit/src/error.rs index 07087e56..0cd675e0 100644 --- a/libimagentryedit/src/error.rs +++ b/libimagentryedit/src/error.rs @@ -1,6 +1,8 @@ generate_error_module!( generate_error_types!(EditError, EditErrorKind, - IOError => "IO Error" + IOError => "IO Error", + ProcessExitFailure => "Process did not exit properly", + InstantiateError => "Instantation error" ); ); From 676146df3a5ecfe5070989492513b136c98fed1c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 9 Aug 2016 12:35:16 +0200 Subject: [PATCH 6/7] Rewrite error module --- libimagentryedit/src/edit.rs | 41 +++++++++++++++++------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/libimagentryedit/src/edit.rs b/libimagentryedit/src/edit.rs index b04bec06..cdf976a4 100644 --- a/libimagentryedit/src/edit.rs +++ b/libimagentryedit/src/edit.rs @@ -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 = Result; +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()) } } From 4614b9d43075cf3e88760c68b13c2c0124aa8ca3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 9 Aug 2016 13:52:41 +0200 Subject: [PATCH 7/7] Use libimagentryedit instead of libimagrt --- imag-diary/Cargo.toml | 3 +++ imag-diary/src/create.rs | 2 +- imag-diary/src/edit.rs | 2 +- imag-diary/src/main.rs | 1 + imag-notes/Cargo.toml | 3 +++ imag-notes/src/main.rs | 3 ++- libimagdiary/Cargo.toml | 3 +++ libimagdiary/src/entry.rs | 4 ++-- libimagdiary/src/lib.rs | 1 + libimagentryview/Cargo.toml | 3 +++ libimagentryview/src/builtin/editor.rs | 2 +- libimagentryview/src/lib.rs | 1 + libimagnotes/Cargo.toml | 3 +++ libimagnotes/src/lib.rs | 1 + libimagnotes/src/note.rs | 3 ++- 15 files changed, 28 insertions(+), 7 deletions(-) diff --git a/imag-diary/Cargo.toml b/imag-diary/Cargo.toml index 1665dfc6..2d0df647 100644 --- a/imag-diary/Cargo.toml +++ b/imag-diary/Cargo.toml @@ -15,6 +15,9 @@ path = "../libimagrt" [dependencies.libimagdiary] path = "../libimagdiary" +[dependencies.libimagentryedit] +path = "../libimagentryedit" + [dependencies.libimagentrylist] path = "../libimagentrylist" diff --git a/imag-diary/src/create.rs b/imag-diary/src/create.rs index 5777ef6a..d4b5edb7 100644 --- a/imag-diary/src/create.rs +++ b/imag-diary/src/create.rs @@ -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; diff --git a/imag-diary/src/edit.rs b/imag-diary/src/edit.rs index 6b684855..cc3362f3 100644 --- a/imag-diary/src/edit.rs +++ b/imag-diary/src/edit.rs @@ -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; diff --git a/imag-diary/src/main.rs b/imag-diary/src/main.rs index c65780a8..7a34f1ef 100644 --- a/imag-diary/src/main.rs +++ b/imag-diary/src/main.rs @@ -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; diff --git a/imag-notes/Cargo.toml b/imag-notes/Cargo.toml index 8ca03475..9eff3718 100644 --- a/imag-notes/Cargo.toml +++ b/imag-notes/Cargo.toml @@ -15,6 +15,9 @@ path = "../libimagrt" [dependencies.libimagnotes] path = "../libimagnotes" +[dependencies.libimagentryedit] +path = "../libimagentryedit" + [dependencies.libimagentrytag] path = "../libimagentrytag" diff --git a/imag-notes/src/main.rs b/imag-notes/src/main.rs index c14b2dec..3e478ca4 100644 --- a/imag-notes/src/main.rs +++ b/imag-notes/src/main.rs @@ -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; diff --git a/libimagdiary/Cargo.toml b/libimagdiary/Cargo.toml index eac1a292..5ac1b268 100644 --- a/libimagdiary/Cargo.toml +++ b/libimagdiary/Cargo.toml @@ -24,3 +24,6 @@ path = "../libimagutil" [dependencies.libimagrt] path = "../libimagrt" +[dependencies.libimagentryedit] +path = "../libimagentryedit" + diff --git a/libimagdiary/src/entry.rs b/libimagdiary/src/entry.rs index d5bddf9d..c042fd91 100644 --- a/libimagdiary/src/entry.rs +++ b/libimagdiary/src/entry.rs @@ -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; diff --git a/libimagdiary/src/lib.rs b/libimagdiary/src/lib.rs index 231ca0ea..1e68ee2e 100644 --- a/libimagdiary/src/lib.rs +++ b/libimagdiary/src/lib.rs @@ -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"); diff --git a/libimagentryview/Cargo.toml b/libimagentryview/Cargo.toml index ca3cec15..693119b3 100644 --- a/libimagentryview/Cargo.toml +++ b/libimagentryview/Cargo.toml @@ -17,3 +17,6 @@ path = "../libimagstore" [dependencies.libimagerror] path = "../libimagerror" +[dependencies.libimagentryedit] +path = "../libimagentryedit" + diff --git a/libimagentryview/src/builtin/editor.rs b/libimagentryview/src/builtin/editor.rs index 0f35c98e..d13c1fb4 100644 --- a/libimagentryview/src/builtin/editor.rs +++ b/libimagentryview/src/builtin/editor.rs @@ -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; diff --git a/libimagentryview/src/lib.rs b/libimagentryview/src/lib.rs index 159e0fa2..534b05f3 100644 --- a/libimagentryview/src/lib.rs +++ b/libimagentryview/src/lib.rs @@ -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; diff --git a/libimagnotes/Cargo.toml b/libimagnotes/Cargo.toml index 94fabbef..b5a50154 100644 --- a/libimagnotes/Cargo.toml +++ b/libimagnotes/Cargo.toml @@ -17,6 +17,9 @@ path = "../libimagerror" [dependencies.libimagrt] path = "../libimagrt" +[dependencies.libimagentryedit] +path = "../libimagentryedit" + [dependencies.libimagentrytag] path = "../libimagentrytag" diff --git a/libimagnotes/src/lib.rs b/libimagnotes/src/lib.rs index 577bd2c4..a275848e 100644 --- a/libimagnotes/src/lib.rs +++ b/libimagnotes/src/lib.rs @@ -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"); diff --git a/libimagnotes/src/note.rs b/libimagnotes/src/note.rs index fc0a507d..48f25bf0 100644 --- a/libimagnotes/src/note.rs +++ b/libimagnotes/src/note.rs @@ -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;