Fix: Entry::to_str() should return Result<_>

Because serializing might fail.

Also fixes all usages of the API.
This commit is contained in:
Matthias Beyer 2018-03-23 15:39:52 +01:00
parent ec2f87afad
commit e61ccc9561
11 changed files with 30 additions and 20 deletions

View File

@ -69,10 +69,10 @@ struct Diagnostic {
pub num_internal_links: usize, pub num_internal_links: usize,
} }
impl<'a> From<FileLockEntry<'a>> for Diagnostic { impl Diagnostic {
fn from(entry: FileLockEntry<'a>) -> Diagnostic { fn for_entry<'a>(entry: FileLockEntry<'a>) -> Result<Diagnostic, ::libimagstore::error::StoreError> {
Diagnostic { Ok(Diagnostic {
id: entry.get_location().clone(), id: entry.get_location().clone(),
entry_store_version: entry entry_store_version: entry
.get_header() .get_header()
@ -88,10 +88,10 @@ impl<'a> From<FileLockEntry<'a>> for Diagnostic {
_ => 0 _ => 0
}, },
bytecount_content: entry.get_content().as_str().len(), 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(), verified: entry.verify().is_ok(),
num_internal_links: entry.get_internal_links().map(Iterator::count).unwrap_or(0), 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())) .ok_or(Error::from("Unable to get entry".to_owned()))
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
}) })
.map(Diagnostic::from) .map(Diagnostic::for_entry)
.collect::<Vec<_>>(); .collect::<Result<Vec<_>, _>>()
.map_err_trace_exit_unwrap(1);
let mut version_counts : BTreeMap<String, usize> = BTreeMap::new(); let mut version_counts : BTreeMap<String, usize> = BTreeMap::new();
let mut sum_header_sections = 0; let mut sum_header_sections = 0;

View File

@ -355,7 +355,9 @@ mod tests {
let mut path = PathBuf::new(); let mut path = PathBuf::new();
path.set_file_name(name); 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"); debug!("Default entry constructed");

View File

@ -53,7 +53,7 @@ pub fn retrieve(rt: &Runtime) {
pub fn print_entry(rt: &Runtime, scmd: &ArgMatches, e: FileLockEntry) { pub fn print_entry(rt: &Runtime, scmd: &ArgMatches, e: FileLockEntry) {
if do_print_raw(scmd) { if do_print_raw(scmd) {
debug!("Printing raw content..."); 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() .to_exit_code()
.unwrap_or_exit(); .unwrap_or_exit();
} else if do_filter(scmd) { } else if do_filter(scmd) {

View File

@ -244,7 +244,9 @@ mod tests {
let mut path = PathBuf::new(); let mut path = PathBuf::new();
path.set_file_name(name); 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 id = StoreId::new_baseless(path)?;
let mut entry = rt.store().create(id.clone())?; let mut entry = rt.store().create(id.clone())?;

View File

@ -30,6 +30,7 @@ error_chain! {
Io(::std::io::Error); Io(::std::io::Error);
Fmt(::std::fmt::Error); Fmt(::std::fmt::Error);
TomlDeserError(::toml::de::Error); TomlDeserError(::toml::de::Error);
TomlSerError(::toml::ser::Error);
GlobPatternError(::glob::PatternError); GlobPatternError(::glob::PatternError);
TomlQueryError(::toml_query::error::Error); TomlQueryError(::toml_query::error::Error);
} }

View File

@ -78,7 +78,7 @@ impl FileAbstractionInstance for FSFileAbstractionInstance {
fn write_file_content(&mut self, buf: &Entry) -> Result<(), SE> { fn write_file_content(&mut self, buf: &Entry) -> Result<(), SE> {
use std::io::Write; use std::io::Write;
let buf = buf.to_str().into_bytes(); let buf = buf.to_str()?.into_bytes();
let (file, path) = match *self { let (file, path) = match *self {
FSFileAbstractionInstance::File(ref mut f, _) => return { FSFileAbstractionInstance::File(ref mut f, _) => return {

View File

@ -928,10 +928,10 @@ impl Entry {
/// ///
/// This means not only the content of the entry, but the complete entry (from memory, not from /// This means not only the content of the entry, but the complete entry (from memory, not from
/// disk). /// disk).
pub fn to_str(&self) -> String { pub fn to_str(&self) -> Result<String> {
format!("---\n{header}---\n{content}", Ok(format!("---\n{header}---\n{content}",
header = ::toml::ser::to_string_pretty(&self.header).unwrap(), header = ::toml::ser::to_string_pretty(&self.header)?,
content = self.content) content = self.content))
} }
/// Get the location of the Entry /// Get the location of the Entry
@ -1255,7 +1255,7 @@ Hai
println!("{}", TEST_ENTRY); println!("{}", TEST_ENTRY);
let entry = Entry::from_str(StoreId::new_baseless(PathBuf::from("test/foo~1.3")).unwrap(), let entry = Entry::from_str(StoreId::new_baseless(PathBuf::from("test/foo~1.3")).unwrap(),
TEST_ENTRY).unwrap(); TEST_ENTRY).unwrap();
let string = entry.to_str(); let string = entry.to_str().unwrap();
assert_eq!(TEST_ENTRY, string); assert_eq!(TEST_ENTRY, string);
} }
@ -1267,7 +1267,7 @@ Hai
println!("{}", TEST_ENTRY_TNL); println!("{}", TEST_ENTRY_TNL);
let entry = Entry::from_str(StoreId::new_baseless(PathBuf::from("test/foo~1.3")).unwrap(), let entry = Entry::from_str(StoreId::new_baseless(PathBuf::from("test/foo~1.3")).unwrap(),
TEST_ENTRY_TNL).unwrap(); TEST_ENTRY_TNL).unwrap();
let string = entry.to_str(); let string = entry.to_str().unwrap();
assert_eq!(TEST_ENTRY_TNL, string); assert_eq!(TEST_ENTRY_TNL, string);
} }

View File

@ -62,7 +62,7 @@ impl EditHeader for Entry {
} }
fn edit_header_and_content(&mut self, rt: &Runtime) -> Result<()> { 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)?; let _ = edit_in_tmpfile(rt, &mut header_and_content)?;
self.replace_from_buffer(&header_and_content).map_err(EE::from) self.replace_from_buffer(&header_and_content).map_err(EE::from)
} }

View File

@ -126,7 +126,7 @@ impl LinkProcessor {
/// function returns all errors returned by the Store. /// function returns all errors returned by the Store.
/// ///
pub fn process<'a>(&self, entry: &mut Entry, store: &'a Store) -> Result<()> { 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()); trace!("Processing: {:?}", entry.get_location());
for link in extract_links(&text).into_iter() { for link in extract_links(&text).into_iter() {
trace!("Processing {:?}", link); trace!("Processing {:?}", link);

View File

@ -36,7 +36,7 @@ impl<'a> EditorView<'a> {
impl<'a> Viewer for EditorView<'a> { impl<'a> Viewer for EditorView<'a> {
fn view_entry(&self, e: &Entry) -> Result<()> { 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) edit_in_tmpfile(self.0, &mut entry).chain_err(|| VEK::ViewError)
} }
} }

View File

@ -22,6 +22,10 @@ error_chain! {
ViewError, ViewErrorKind, ResultExt, Result; ViewError, ViewErrorKind, ResultExt, Result;
} }
links {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
}
errors { errors {
Unknown { Unknown {
description("Unknown view error") description("Unknown view error")