diff --git a/bin/core/imag-diagnostics/src/main.rs b/bin/core/imag-diagnostics/src/main.rs index 50dd2be0..63f3df99 100644 --- a/bin/core/imag-diagnostics/src/main.rs +++ b/bin/core/imag-diagnostics/src/main.rs @@ -69,10 +69,10 @@ struct Diagnostic { pub num_internal_links: usize, } -impl<'a> From> for Diagnostic { +impl Diagnostic { - fn from(entry: FileLockEntry<'a>) -> Diagnostic { - Diagnostic { + fn for_entry<'a>(entry: FileLockEntry<'a>) -> Result { + Ok(Diagnostic { id: entry.get_location().clone(), entry_store_version: entry .get_header() @@ -88,10 +88,10 @@ impl<'a> From> for Diagnostic { _ => 0 }, bytecount_content: entry.get_content().as_str().len(), - overall_byte_size: entry.to_str().as_str().len(), + overall_byte_size: entry.to_str()?.as_str().len(), verified: entry.verify().is_ok(), num_internal_links: entry.get_internal_links().map(Iterator::count).unwrap_or(0), - } + }) } } @@ -111,8 +111,9 @@ fn main() { .ok_or(Error::from("Unable to get entry".to_owned())) .map_err_trace_exit_unwrap(1) }) - .map(Diagnostic::from) - .collect::>(); + .map(Diagnostic::for_entry) + .collect::, _>>() + .map_err_trace_exit_unwrap(1); let mut version_counts : BTreeMap = BTreeMap::new(); let mut sum_header_sections = 0; diff --git a/bin/core/imag-link/src/main.rs b/bin/core/imag-link/src/main.rs index d68142bc..bc6af042 100644 --- a/bin/core/imag-link/src/main.rs +++ b/bin/core/imag-link/src/main.rs @@ -355,7 +355,9 @@ mod tests { let mut path = PathBuf::new(); path.set_file_name(name); - let default_entry = Entry::new(StoreId::new_baseless(PathBuf::from("")).unwrap()).to_str(); + let default_entry = Entry::new(StoreId::new_baseless(PathBuf::from("")).unwrap()) + .to_str() + .unwrap(); debug!("Default entry constructed"); diff --git a/bin/core/imag-store/src/retrieve.rs b/bin/core/imag-store/src/retrieve.rs index ea895138..68ef5491 100644 --- a/bin/core/imag-store/src/retrieve.rs +++ b/bin/core/imag-store/src/retrieve.rs @@ -53,7 +53,7 @@ pub fn retrieve(rt: &Runtime) { pub fn print_entry(rt: &Runtime, scmd: &ArgMatches, e: FileLockEntry) { if do_print_raw(scmd) { debug!("Printing raw content..."); - let _ = writeln!(rt.stdout(), "{}", e.to_str()) + let _ = writeln!(rt.stdout(), "{}", e.to_str().map_err_trace_exit_unwrap(1)) .to_exit_code() .unwrap_or_exit(); } else if do_filter(scmd) { diff --git a/bin/core/imag-tag/src/main.rs b/bin/core/imag-tag/src/main.rs index 2c3f00fb..39f468a5 100644 --- a/bin/core/imag-tag/src/main.rs +++ b/bin/core/imag-tag/src/main.rs @@ -244,7 +244,9 @@ mod tests { let mut path = PathBuf::new(); path.set_file_name(name); - let default_entry = Entry::new(StoreId::new_baseless(PathBuf::from("")).unwrap()).to_str(); + let default_entry = Entry::new(StoreId::new_baseless(PathBuf::from("")).unwrap()) + .to_str() + .unwrap(); let id = StoreId::new_baseless(path)?; let mut entry = rt.store().create(id.clone())?; diff --git a/lib/core/libimagstore/src/error.rs b/lib/core/libimagstore/src/error.rs index 1714b459..e10803d4 100644 --- a/lib/core/libimagstore/src/error.rs +++ b/lib/core/libimagstore/src/error.rs @@ -30,6 +30,7 @@ error_chain! { Io(::std::io::Error); Fmt(::std::fmt::Error); TomlDeserError(::toml::de::Error); + TomlSerError(::toml::ser::Error); GlobPatternError(::glob::PatternError); TomlQueryError(::toml_query::error::Error); } diff --git a/lib/core/libimagstore/src/file_abstraction/fs.rs b/lib/core/libimagstore/src/file_abstraction/fs.rs index c26f9210..c1d5c4c1 100644 --- a/lib/core/libimagstore/src/file_abstraction/fs.rs +++ b/lib/core/libimagstore/src/file_abstraction/fs.rs @@ -78,7 +78,7 @@ impl FileAbstractionInstance for FSFileAbstractionInstance { fn write_file_content(&mut self, buf: &Entry) -> Result<(), SE> { use std::io::Write; - let buf = buf.to_str().into_bytes(); + let buf = buf.to_str()?.into_bytes(); let (file, path) = match *self { FSFileAbstractionInstance::File(ref mut f, _) => return { diff --git a/lib/core/libimagstore/src/store.rs b/lib/core/libimagstore/src/store.rs index 29dc6890..97626b10 100644 --- a/lib/core/libimagstore/src/store.rs +++ b/lib/core/libimagstore/src/store.rs @@ -928,10 +928,10 @@ impl Entry { /// /// This means not only the content of the entry, but the complete entry (from memory, not from /// disk). - pub fn to_str(&self) -> String { - format!("---\n{header}---\n{content}", - header = ::toml::ser::to_string_pretty(&self.header).unwrap(), - content = self.content) + pub fn to_str(&self) -> Result { + Ok(format!("---\n{header}---\n{content}", + header = ::toml::ser::to_string_pretty(&self.header)?, + content = self.content)) } /// Get the location of the Entry @@ -1255,7 +1255,7 @@ Hai println!("{}", TEST_ENTRY); let entry = Entry::from_str(StoreId::new_baseless(PathBuf::from("test/foo~1.3")).unwrap(), TEST_ENTRY).unwrap(); - let string = entry.to_str(); + let string = entry.to_str().unwrap(); assert_eq!(TEST_ENTRY, string); } @@ -1267,7 +1267,7 @@ Hai println!("{}", TEST_ENTRY_TNL); let entry = Entry::from_str(StoreId::new_baseless(PathBuf::from("test/foo~1.3")).unwrap(), TEST_ENTRY_TNL).unwrap(); - let string = entry.to_str(); + let string = entry.to_str().unwrap(); assert_eq!(TEST_ENTRY_TNL, string); } diff --git a/lib/entry/libimagentryedit/src/edit.rs b/lib/entry/libimagentryedit/src/edit.rs index b2d45383..33af2c2b 100644 --- a/lib/entry/libimagentryedit/src/edit.rs +++ b/lib/entry/libimagentryedit/src/edit.rs @@ -62,7 +62,7 @@ impl EditHeader for Entry { } fn edit_header_and_content(&mut self, rt: &Runtime) -> Result<()> { - let mut header_and_content = self.to_str(); + let mut header_and_content = self.to_str()?; let _ = edit_in_tmpfile(rt, &mut header_and_content)?; self.replace_from_buffer(&header_and_content).map_err(EE::from) } diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs index 7203d9a5..351ba321 100644 --- a/lib/entry/libimagentrymarkdown/src/processor.rs +++ b/lib/entry/libimagentrymarkdown/src/processor.rs @@ -126,7 +126,7 @@ impl LinkProcessor { /// function returns all errors returned by the Store. /// pub fn process<'a>(&self, entry: &mut Entry, store: &'a Store) -> Result<()> { - let text = entry.to_str(); + let text = entry.to_str()?; trace!("Processing: {:?}", entry.get_location()); for link in extract_links(&text).into_iter() { trace!("Processing {:?}", link); diff --git a/lib/entry/libimagentryview/src/builtin/editor.rs b/lib/entry/libimagentryview/src/builtin/editor.rs index e3887f2c..25d2992b 100644 --- a/lib/entry/libimagentryview/src/builtin/editor.rs +++ b/lib/entry/libimagentryview/src/builtin/editor.rs @@ -36,7 +36,7 @@ impl<'a> EditorView<'a> { impl<'a> Viewer for EditorView<'a> { fn view_entry(&self, e: &Entry) -> Result<()> { - let mut entry = e.to_str().clone().to_string(); + let mut entry = e.to_str()?.clone().to_string(); edit_in_tmpfile(self.0, &mut entry).chain_err(|| VEK::ViewError) } } diff --git a/lib/entry/libimagentryview/src/error.rs b/lib/entry/libimagentryview/src/error.rs index e6538f6f..7c313f76 100644 --- a/lib/entry/libimagentryview/src/error.rs +++ b/lib/entry/libimagentryview/src/error.rs @@ -22,6 +22,10 @@ error_chain! { ViewError, ViewErrorKind, ResultExt, Result; } + links { + StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind); + } + errors { Unknown { description("Unknown view error")