Merge pull request #367 from matthiasbeyer/unwrap_err

Unwrap err
This commit is contained in:
Matthias Beyer 2016-04-21 15:08:36 +02:00
commit b629d0b8f1
19 changed files with 54 additions and 39 deletions

View file

@ -15,11 +15,11 @@ pub fn list(rt: &Runtime) {
let value = c.value(); let value = c.value();
if name.is_err() { if name.is_err() {
trace_error(&name.err().unwrap()); trace_error(&name.unwrap_err());
} else { } else {
if value.is_err() { if value.is_err() {
trace_error(&value.err().unwrap()); trace_error(&value.unwrap_err());
} else { } else {
println!("{} - {}", name.unwrap(), value.unwrap()); println!("{} - {}", name.unwrap(), value.unwrap());
} }

View file

@ -59,7 +59,7 @@ fn main() {
rt.unwrap() rt.unwrap()
} else { } else {
println!("Could not set up Runtime"); println!("Could not set up Runtime");
println!("{:?}", rt.err().unwrap()); println!("{:?}", rt.unwrap_err());
exit(1); exit(1);
} }
}; };

View file

@ -53,7 +53,7 @@ fn main() {
rt.unwrap() rt.unwrap()
} else { } else {
println!("Could not set up Runtime"); println!("Could not set up Runtime");
println!("{:?}", rt.err().unwrap()); println!("{:?}", rt.unwrap_err());
exit(1); exit(1);
} }
}; };
@ -200,7 +200,7 @@ fn handle_external_linking(rt: &Runtime) {
let entry_name = scmd.value_of("id").unwrap(); // enforced by clap let entry_name = scmd.value_of("id").unwrap(); // enforced by clap
let entry = get_entry_by_name(rt, entry_name); let entry = get_entry_by_name(rt, entry_name);
if entry.is_err() { if entry.is_err() {
trace_error(&entry.err().unwrap()); trace_error(&entry.unwrap_err());
exit(1); exit(1);
} }
let mut entry = entry.unwrap(); let mut entry = entry.unwrap();
@ -238,7 +238,7 @@ fn add_link_to_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEn
let link = Url::parse(link); let link = Url::parse(link);
if link.is_err() { if link.is_err() {
debug!("URL parsing error..."); debug!("URL parsing error...");
trace_error(&link.err().unwrap()); trace_error(&link.unwrap_err());
debug!("Exiting"); debug!("Exiting");
exit(1); exit(1);
} }
@ -258,7 +258,7 @@ fn remove_link_from_entry(store: &Store, matches: &ArgMatches, entry: &mut FileL
let link = Url::parse(link); let link = Url::parse(link);
if link.is_err() { if link.is_err() {
trace_error(&link.err().unwrap()); trace_error(&link.unwrap_err());
exit(1); exit(1);
} }
let link = link.unwrap(); let link = link.unwrap();

View file

@ -29,7 +29,7 @@ fn main() {
rt.unwrap() rt.unwrap()
} else { } else {
println!("Could not set up Runtime"); println!("Could not set up Runtime");
println!("{:?}", rt.err().unwrap()); println!("{:?}", rt.unwrap_err());
exit(1); exit(1);
} }
}; };
@ -81,7 +81,7 @@ fn edit(rt: &Runtime) {
fn edit_entry(rt: &Runtime, name: String) -> bool { fn edit_entry(rt: &Runtime, name: String) -> bool {
let note = Note::retrieve(rt.store(), name); let note = Note::retrieve(rt.store(), name);
if note.is_err() { if note.is_err() {
trace_error(&note.err().unwrap()); trace_error(&note.unwrap_err());
warn!("Cannot edit nonexistent Note"); warn!("Cannot edit nonexistent Note");
return false return false
} }
@ -100,7 +100,7 @@ fn list(rt: &Runtime) {
let iter = Note::all_notes(rt.store()); let iter = Note::all_notes(rt.store());
if iter.is_err() { if iter.is_err() {
trace_error(&iter.err().unwrap()); trace_error(&iter.unwrap_err());
exit(1); exit(1);
} }

View file

@ -38,7 +38,7 @@ pub fn create(rt: &Runtime) {
let path = build_entry_path(rt.store(), path.unwrap()); let path = build_entry_path(rt.store(), path.unwrap());
if path.is_err() { if path.is_err() {
trace_error(&path.err().unwrap()); trace_error(&path.unwrap_err());
exit(1); exit(1);
} }
let path = path.unwrap(); let path = path.unwrap();

View file

@ -12,7 +12,7 @@ pub fn delete(rt: &Runtime) {
.map(|id| { .map(|id| {
let path = build_entry_path(rt.store(), id); let path = build_entry_path(rt.store(), id);
if path.is_err() { if path.is_err() {
trace_error(&path.err().unwrap()); trace_error(&path.unwrap_err());
exit(1); exit(1);
} }
let path = path.unwrap(); let path = path.unwrap();

View file

@ -51,7 +51,7 @@ fn main() {
rt.unwrap() rt.unwrap()
} else { } else {
println!("Could not set up Runtime"); println!("Could not set up Runtime");
println!("{:?}", rt.err().unwrap()); println!("{:?}", rt.unwrap_err());
exit(1); exit(1);
} }
}; };

View file

@ -17,7 +17,7 @@ pub fn retrieve(rt: &Runtime) {
.map(|id| { .map(|id| {
let path = build_entry_path(rt.store(), id); let path = build_entry_path(rt.store(), id);
if path.is_err() { if path.is_err() {
trace_error(&path.err().unwrap()); trace_error(&path.unwrap_err());
exit(1); exit(1);
} }
let path = path.unwrap(); let path = path.unwrap();

View file

@ -15,7 +15,7 @@ pub fn update(rt: &Runtime) {
.map(|id| { .map(|id| {
let path = build_entry_path(rt.store(), id); let path = build_entry_path(rt.store(), id);
if path.is_err() { if path.is_err() {
trace_error(&path.err().unwrap()); trace_error(&path.unwrap_err());
exit(1); exit(1);
} }
let path = path.unwrap(); let path = path.unwrap();

View file

@ -32,7 +32,7 @@ fn main() {
rt.unwrap() rt.unwrap()
} else { } else {
println!("Could not set up Runtime"); println!("Could not set up Runtime");
println!("{:?}", rt.err().unwrap()); println!("{:?}", rt.unwrap_err());
exit(1); exit(1);
} }
}; };
@ -127,7 +127,7 @@ fn list(id: &str, rt: &Runtime) {
if entry.is_err() { if entry.is_err() {
debug!("Could not retrieve '{:?}' => {:?}", id, path); debug!("Could not retrieve '{:?}' => {:?}", id, path);
warn!("Could not retrieve entry '{}'", id); warn!("Could not retrieve entry '{}'", id);
trace_error(&entry.err().unwrap()); trace_error(&entry.unwrap_err());
exit(1); exit(1);
} }
let entry = entry.unwrap(); let entry = entry.unwrap();
@ -146,7 +146,7 @@ fn list(id: &str, rt: &Runtime) {
let tags = entry.get_tags(); let tags = entry.get_tags();
if tags.is_err() { if tags.is_err() {
trace_error(&tags.err().unwrap()); trace_error(&tags.unwrap_err());
exit(1); exit(1);
} }
let tags = tags.unwrap(); let tags = tags.unwrap();

View file

@ -54,7 +54,7 @@ fn main() {
rt.unwrap() rt.unwrap()
} else { } else {
println!("Could not set up Runtime"); println!("Could not set up Runtime");
println!("{:?}", rt.err().unwrap()); println!("{:?}", rt.unwrap_err());
exit(1); // we can afford not-executing destructors here exit(1); // we can afford not-executing destructors here
} }
}; };
@ -102,7 +102,7 @@ fn main() {
let entry = load_entry(entry_id, entry_version, &rt); let entry = load_entry(entry_id, entry_version, &rt);
if entry.is_err() { if entry.is_err() {
trace_error(&entry.err().unwrap()); trace_error(&entry.unwrap_err());
exit(1); // we can afford not-executing destructors here exit(1); // we can afford not-executing destructors here
} }
let entry = entry.unwrap(); let entry = entry.unwrap();

View file

@ -39,17 +39,17 @@ impl<'a> Counter<'a> {
let mut header = entry.get_header_mut(); let mut header = entry.get_header_mut();
let setres = header.set("counter", Value::Table(BTreeMap::new())); let setres = header.set("counter", Value::Table(BTreeMap::new()));
if setres.is_err() { if setres.is_err() {
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.err().unwrap())))); return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err()))));
} }
let setres = header.set("counter.name", Value::String(name)); let setres = header.set("counter.name", Value::String(name));
if setres.is_err() { if setres.is_err() {
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.err().unwrap())))); return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err()))));
} }
let setres = header.set("counter.value", Value::Integer(init)); let setres = header.set("counter.value", Value::Integer(init));
if setres.is_err() { if setres.is_err() {
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.err().unwrap())))); return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err()))));
} }
} }

View file

@ -170,7 +170,7 @@ impl ExternalLinker for Entry {
let file = store.retrieve(file_id.clone()); let file = store.retrieve(file_id.clone());
if file.is_err() { if file.is_err() {
debug!("Failed to create or retrieve an file for this link '{:?}'", link); debug!("Failed to create or retrieve an file for this link '{:?}'", link);
return Err(LE::new(LEK::StoreWriteError, Some(Box::new(file.err().unwrap())))); return Err(LE::new(LEK::StoreWriteError, Some(Box::new(file.unwrap_err()))));
} }
let mut file = file.unwrap(); let mut file = file.unwrap();

View file

@ -139,7 +139,7 @@ fn process_rw_result(links: StoreResult<Option<Value>>) -> Result<Vec<Link>> {
if links.is_err() { if links.is_err() {
debug!("RW action on store failed. Generating LinkError"); debug!("RW action on store failed. Generating LinkError");
let lerr = LinkError::new(LinkErrorKind::EntryHeaderReadError, let lerr = LinkError::new(LinkErrorKind::EntryHeaderReadError,
Some(Box::new(links.err().unwrap()))); Some(Box::new(links.unwrap_err())));
return Err(lerr); return Err(lerr);
} }
let links = links.unwrap(); let links = links.unwrap();

View file

@ -31,7 +31,7 @@ impl Tagable for EntryHeader {
let tags = self.read("imag.tags"); let tags = self.read("imag.tags");
if tags.is_err() { if tags.is_err() {
let kind = TagErrorKind::HeaderReadError; let kind = TagErrorKind::HeaderReadError;
return Err(TagError::new(kind, Some(Box::new(tags.err().unwrap())))); return Err(TagError::new(kind, Some(Box::new(tags.unwrap_err()))));
} }
let tags = tags.unwrap(); let tags = tags.unwrap();
@ -106,7 +106,7 @@ impl Tagable for EntryHeader {
let tags = self.read("imag.tags"); let tags = self.read("imag.tags");
if tags.is_err() { if tags.is_err() {
let kind = TagErrorKind::HeaderReadError; let kind = TagErrorKind::HeaderReadError;
return Err(TagError::new(kind, Some(Box::new(tags.err().unwrap())))); return Err(TagError::new(kind, Some(Box::new(tags.unwrap_err()))));
} }
let tags = tags.unwrap(); let tags = tags.unwrap();
@ -129,7 +129,7 @@ impl Tagable for EntryHeader {
for tag in tags { for tag in tags {
let check = self.has_tag(tag); let check = self.has_tag(tag);
if check.is_err() { if check.is_err() {
return Err(check.err().unwrap()); return Err(check.unwrap_err());
} }
let check = check.unwrap(); let check = check.unwrap();

View file

@ -19,6 +19,7 @@ use result::Result;
use error::NoteError as NE; use error::NoteError as NE;
use error::NoteErrorKind as NEK; use error::NoteErrorKind as NEK;
#[derive(Debug)]
pub struct Note<'a> { pub struct Note<'a> {
entry: FileLockEntry<'a>, entry: FileLockEntry<'a>,
} }
@ -32,7 +33,7 @@ impl<'a> Note<'a> {
let fle = { let fle = {
let lockentry = store.create(ModuleEntryPath::new(name.clone()).into_storeid()); let lockentry = store.create(ModuleEntryPath::new(name.clone()).into_storeid());
if lockentry.is_err() { if lockentry.is_err() {
return Err(NE::new(NEK::StoreWriteError, Some(Box::new(lockentry.err().unwrap())))); return Err(NE::new(NEK::StoreWriteError, Some(Box::new(lockentry.unwrap_err()))));
} }
let mut lockentry = lockentry.unwrap(); let mut lockentry = lockentry.unwrap();
@ -44,13 +45,13 @@ impl<'a> Note<'a> {
let setres = header.set("note", Value::Table(BTreeMap::new())); let setres = header.set("note", Value::Table(BTreeMap::new()));
if setres.is_err() { if setres.is_err() {
let kind = NEK::StoreWriteError; let kind = NEK::StoreWriteError;
return Err(NE::new(kind, Some(Box::new(setres.err().unwrap())))); return Err(NE::new(kind, Some(Box::new(setres.unwrap_err()))));
} }
let setres = header.set("note.name", Value::String(name)); let setres = header.set("note.name", Value::String(name));
if setres.is_err() { if setres.is_err() {
let kind = NEK::StoreWriteError; let kind = NEK::StoreWriteError;
return Err(NE::new(kind, Some(Box::new(setres.err().unwrap())))); return Err(NE::new(kind, Some(Box::new(setres.unwrap_err()))));
} }
} }
@ -171,6 +172,7 @@ impl<'a> Deref for Note<'a> {
} }
#[derive(Debug)]
pub struct NoteIterator<'a> { pub struct NoteIterator<'a> {
store: &'a Store, store: &'a Store,
iditer: StoreIdIterator, iditer: StoreIdIterator,

View file

@ -17,6 +17,7 @@ use logger::ImagLogger;
use libimagstore::store::Store; use libimagstore::store::Store;
#[derive(Debug)]
pub struct Runtime<'a> { pub struct Runtime<'a> {
rtp: PathBuf, rtp: PathBuf,
configuration: Option<Configuration>, configuration: Option<Configuration>,
@ -73,7 +74,7 @@ impl<'a> Runtime<'a> {
let cfg = Configuration::new(&rtp); let cfg = Configuration::new(&rtp);
let cfg = if cfg.is_err() { let cfg = if cfg.is_err() {
let e = cfg.err().unwrap(); let e = cfg.unwrap_err();
if e.err_type() != ConfigErrorKind::NoConfigFileFound { if e.err_type() != ConfigErrorKind::NoConfigFileFound {
let cause : Option<Box<Error>> = Some(Box::new(e)); let cause : Option<Box<Error>> = Some(Box::new(e));
return Err(RuntimeError::new(RuntimeErrorKind::Instantiate, cause)); return Err(RuntimeError::new(RuntimeErrorKind::Instantiate, cause));

View file

@ -156,7 +156,7 @@ impl Store {
if c.is_err() { if c.is_err() {
debug!("Failed"); debug!("Failed");
return Err(StoreError::new(StoreErrorKind::StorePathCreate, return Err(StoreError::new(StoreErrorKind::StorePathCreate,
Some(Box::new(c.err().unwrap())))); Some(Box::new(c.unwrap_err()))));
} }
} else { } else {
if location.is_file() { if location.is_file() {
@ -537,6 +537,7 @@ impl Drop for Store {
} }
/// A struct that allows you to borrow an Entry /// A struct that allows you to borrow an Entry
#[derive(Debug)]
pub struct FileLockEntry<'a> { pub struct FileLockEntry<'a> {
store: &'a Store, store: &'a Store,
entry: Entry, entry: Entry,
@ -764,7 +765,7 @@ impl EntryHeader {
pub fn set_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<Option<Value>> { pub fn set_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<Option<Value>> {
let tokens = EntryHeader::tokenize(spec, sep); let tokens = EntryHeader::tokenize(spec, sep);
if tokens.is_err() { // return parser error if any if tokens.is_err() { // return parser error if any
return Err(tokens.err().unwrap()); return Err(tokens.unwrap_err());
} }
let tokens = tokens.unwrap(); let tokens = tokens.unwrap();
debug!("tokens = {:?}", tokens); debug!("tokens = {:?}", tokens);
@ -779,7 +780,7 @@ impl EntryHeader {
let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens
let value = EntryHeader::walk_header(&mut self.header, path_to_dest); // walk N-1 tokens let value = EntryHeader::walk_header(&mut self.header, path_to_dest); // walk N-1 tokens
if value.is_err() { if value.is_err() {
return Err(value.err().unwrap()); return Err(value.unwrap_err());
} }
let mut value = value.unwrap(); let mut value = value.unwrap();
debug!("walked value = {:?}", value); debug!("walked value = {:?}", value);
@ -871,14 +872,14 @@ impl EntryHeader {
pub fn read_with_sep(&self, spec: &str, splitchr: char) -> Result<Option<Value>> { pub fn read_with_sep(&self, spec: &str, splitchr: char) -> Result<Option<Value>> {
let tokens = EntryHeader::tokenize(spec, splitchr); let tokens = EntryHeader::tokenize(spec, splitchr);
if tokens.is_err() { // return parser error if any if tokens.is_err() { // return parser error if any
return Err(tokens.err().unwrap()); return Err(tokens.unwrap_err());
} }
let tokens = tokens.unwrap(); let tokens = tokens.unwrap();
let mut header_clone = self.header.clone(); // we clone as READing is simpler this way let mut header_clone = self.header.clone(); // we clone as READing is simpler this way
let value = EntryHeader::walk_header(&mut header_clone, tokens); // walk N-1 tokens let value = EntryHeader::walk_header(&mut header_clone, tokens); // walk N-1 tokens
if value.is_err() { if value.is_err() {
let e = value.err().unwrap(); let e = value.unwrap_err();
return match e.err_type() { return match e.err_type() {
// We cannot find the header key, as there is no path to it // We cannot find the header key, as there is no path to it
StoreErrorKind::HeaderKeyNotFound => Ok(None), StoreErrorKind::HeaderKeyNotFound => Ok(None),
@ -891,7 +892,7 @@ impl EntryHeader {
pub fn delete(&mut self, spec: &str) -> Result<Option<Value>> { pub fn delete(&mut self, spec: &str) -> Result<Option<Value>> {
let tokens = EntryHeader::tokenize(spec, '.'); let tokens = EntryHeader::tokenize(spec, '.');
if tokens.is_err() { // return parser error if any if tokens.is_err() { // return parser error if any
return Err(tokens.err().unwrap()); return Err(tokens.unwrap_err());
} }
let tokens = tokens.unwrap(); let tokens = tokens.unwrap();
@ -905,7 +906,7 @@ impl EntryHeader {
let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens
let value = EntryHeader::walk_header(&mut self.header, path_to_dest); // walk N-1 tokens let value = EntryHeader::walk_header(&mut self.header, path_to_dest); // walk N-1 tokens
if value.is_err() { if value.is_err() {
return Err(value.err().unwrap()); return Err(value.unwrap_err());
} }
let mut value = value.unwrap(); let mut value = value.unwrap();
debug!("walked value = {:?}", value); debug!("walked value = {:?}", value);

View file

@ -1,6 +1,9 @@
use std::path::PathBuf; use std::path::PathBuf;
use glob::Paths; use glob::Paths;
use semver::Version; use semver::Version;
use std::fmt::{Debug, Formatter};
use std::fmt::Error as FmtError;
use std::result::Result as RResult;
use error::{StoreError, StoreErrorKind}; use error::{StoreError, StoreErrorKind};
use store::Result; use store::Result;
@ -94,6 +97,14 @@ pub struct StoreIdIterator {
paths: Paths, paths: Paths,
} }
impl Debug for StoreIdIterator {
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
write!(fmt, "StoreIdIterator")
}
}
impl StoreIdIterator { impl StoreIdIterator {
pub fn new(paths: Paths) -> StoreIdIterator { pub fn new(paths: Paths) -> StoreIdIterator {