Merge pull request #1366 from matthiasbeyer/libimagstore/entry-to-str-result

Fix: Entry::to_str() should return Result<_>
This commit is contained in:
Matthias Beyer 2018-03-26 15:37:08 +02:00 committed by GitHub
commit 348389f745
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 30 additions and 20 deletions

View File

@ -69,10 +69,10 @@ struct Diagnostic {
pub num_internal_links: usize,
}
impl<'a> From<FileLockEntry<'a>> for Diagnostic {
impl Diagnostic {
fn from(entry: FileLockEntry<'a>) -> Diagnostic {
Diagnostic {
fn for_entry<'a>(entry: FileLockEntry<'a>) -> Result<Diagnostic, ::libimagstore::error::StoreError> {
Ok(Diagnostic {
id: entry.get_location().clone(),
entry_store_version: entry
.get_header()
@ -88,10 +88,10 @@ impl<'a> From<FileLockEntry<'a>> 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::<Vec<_>>();
.map(Diagnostic::for_entry)
.collect::<Result<Vec<_>, _>>()
.map_err_trace_exit_unwrap(1);
let mut version_counts : BTreeMap<String, usize> = BTreeMap::new();
let mut sum_header_sections = 0;

View File

@ -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");

View File

@ -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) {

View File

@ -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())?;

View File

@ -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);
}

View File

@ -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 {

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
/// 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<String> {
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);
}

View File

@ -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)
}

View File

@ -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);

View File

@ -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)
}
}

View File

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