diff --git a/imag-counter/src/create.rs b/imag-counter/src/create.rs index 66f6590d..1b2838a5 100644 --- a/imag-counter/src/create.rs +++ b/imag-counter/src/create.rs @@ -17,7 +17,7 @@ pub fn create(rt: &Runtime) { .and_then(|i| FromStr::from_str(i).ok()) .unwrap_or(0); - match Counter::new(rt.store(), String::from(name.clone()), init) { + match Counter::new(rt.store(), String::from(name), init) { Err(e) => { warn!("Could not create Counter '{}' with initial value '{}'", name, init); trace_error(&e); diff --git a/imag-counter/src/interactive.rs b/imag-counter/src/interactive.rs index 03e6b40e..5a2b1e01 100644 --- a/imag-counter/src/interactive.rs +++ b/imag-counter/src/interactive.rs @@ -51,7 +51,7 @@ pub fn interactive(rt: &Runtime) { exit(1); } - let cont = if input.len() > 0 { + let cont = if !input.is_empty() { let increment = match input.chars().next() { Some('-') => false, _ => true }; input.chars().all(|chr| { match pairs.get_mut(&chr) { @@ -94,8 +94,8 @@ pub fn interactive(rt: &Runtime) { fn has_quit_binding(pairs: &BTreeMap) -> bool { pairs.iter() .any(|(_, bind)| { - match bind { - &Binding::Function(ref name, _) => name == "quit", + match *bind { + Binding::Function(ref name, _) => name == "quit", _ => false, } }) @@ -109,8 +109,8 @@ enum Binding<'a> { impl<'a> Display for Binding<'a> { fn fmt(&self, fmt: &mut Formatter) -> RResult<(), Error> { - match self { - &Binding::Counter(ref c) => { + match *self { + Binding::Counter(ref c) => { match c.name() { Ok(name) => { try!(write!(fmt, "{}", name)); @@ -122,7 +122,7 @@ impl<'a> Display for Binding<'a> { }, } }, - &Binding::Function(ref name, _) => write!(fmt, "{}()", name), + Binding::Function(ref name, _) => write!(fmt, "{}()", name), } } diff --git a/imag-counter/src/list.rs b/imag-counter/src/list.rs index b7276957..41e9ce62 100644 --- a/imag-counter/src/list.rs +++ b/imag-counter/src/list.rs @@ -16,13 +16,10 @@ pub fn list(rt: &Runtime) { if name.is_err() { trace_error(&name.unwrap_err()); + } else if value.is_err() { + trace_error(&value.unwrap_err()); } else { - - if value.is_err() { - trace_error(&value.unwrap_err()); - } else { - println!("{} - {}", name.unwrap(), value.unwrap()); - } + println!("{} - {}", name.unwrap(), value.unwrap()); } }) .map_err(|e| trace_error(&e)) diff --git a/imag-link/src/main.rs b/imag-link/src/main.rs index 70f1d019..2c1cf5fb 100644 --- a/imag-link/src/main.rs +++ b/imag-link/src/main.rs @@ -81,16 +81,14 @@ fn handle_internal_linking(rt: &Runtime) { if cmd.is_present("list") { debug!("List..."); - for entry in cmd.value_of("list").unwrap().split(",") { + for entry in cmd.value_of("list").unwrap().split(',') { debug!("Listing for '{}'", entry); match get_entry_by_name(rt, entry) { Ok(e) => { e.get_internal_links() .map(|links| { - let mut i = 0; - for link in links.iter().map(|l| l.to_str()).filter_map(|x| x) { + for (i, link) in links.iter().map(|l| l.to_str()).filter_map(|x| x).enumerate() { println!("{: <3}: {}", i, link); - i += 1; } }) .map_err(|e| trace_error(&e)) @@ -275,7 +273,7 @@ fn set_links_for_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLock .value_of("links") .map(String::from) .unwrap() - .split(",") + .split(',') .map(|uri| { match Url::parse(uri) { Err(e) => { @@ -299,10 +297,8 @@ fn set_links_for_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLock fn list_links_for_entry(store: &Store, entry: &mut FileLockEntry) { let res = entry.get_external_links(store) .and_then(|links| { - let mut i = 0; - for link in links { + for (i, link) in links.enumerate() { println!("{: <3}: {}", i, link); - i += 1; } Ok(()) }); diff --git a/imag-notes/src/main.rs b/imag-notes/src/main.rs index bea757fa..ee0df3d3 100644 --- a/imag-notes/src/main.rs +++ b/imag-notes/src/main.rs @@ -60,10 +60,9 @@ fn create(rt: &Runtime) { .map_err(|e| trace_error(&e)) .ok(); - if rt.cli().subcommand_matches("create").unwrap().is_present("edit") { - if !edit_entry(rt, name) { - exit(1); - } + if rt.cli().subcommand_matches("create").unwrap().is_present("edit") && + !edit_entry(rt, name) { + exit(1); } } diff --git a/imag-store/src/create.rs b/imag-store/src/create.rs index a8b85ad3..52c105b0 100644 --- a/imag-store/src/create.rs +++ b/imag-store/src/create.rs @@ -60,30 +60,24 @@ pub fn create(rt: &Runtime) { fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &PathBuf) -> Result<()> { let content = matches.subcommand_matches("entry") - .map(|entry_subcommand| { + .map_or_else(|| { + debug!("Didn't find entry subcommand, getting raw content"); + matches.value_of("from-raw") + .map_or_else(String::new, string_from_raw_src) + }, |entry_subcommand| { debug!("Found entry subcommand, parsing content"); entry_subcommand .value_of("content") - .map(String::from) - .unwrap_or_else(|| { - entry_subcommand - .value_of("content-from") - .map(|src| string_from_raw_src(src)) - .unwrap_or(String::new()) - }) - }) - .unwrap_or_else(|| { - debug!("Didn't find entry subcommand, getting raw content"); - matches.value_of("from-raw") - .map(|raw_src| string_from_raw_src(raw_src)) - .unwrap_or(String::new()) + .map_or_else(|| { + entry_subcommand.value_of("content-from") + .map_or_else(String::new, string_from_raw_src) + }, String::from) }); - debug!("Got content with len = {}", content.len()); let header = matches.subcommand_matches("entry") - .map(|entry_matches| build_toml_header(entry_matches, EntryHeader::new())) - .unwrap_or(EntryHeader::new()); + .map_or_else(EntryHeader::new, + |entry_matches| build_toml_header(entry_matches, EntryHeader::new())); create_with_content_and_header(rt, path, content, header) } @@ -91,7 +85,7 @@ fn create_from_cli_spec(rt: &Runtime, matches: &ArgMatches, path: &PathBuf) -> R fn create_from_source(rt: &Runtime, matches: &ArgMatches, path: &PathBuf) -> Result<()> { let content = matches .value_of("from-raw") - .ok_or(StoreError::new(StoreErrorKind::NoCommandlineCall, None)) + .ok_or_else(|| StoreError::new(StoreErrorKind::NoCommandlineCall, None)) .map(|raw_src| string_from_raw_src(raw_src)); if content.is_err() { diff --git a/imag-store/src/error.rs b/imag-store/src/error.rs index a9884250..30c9c8b5 100644 --- a/imag-store/src/error.rs +++ b/imag-store/src/error.rs @@ -3,20 +3,18 @@ use std::fmt::Error as FmtError; use std::clone::Clone; use std::fmt::{Display, Formatter}; -/** - * Kind of store error - */ #[derive(Clone, Copy, Debug, PartialEq)] +/// Kind of store error pub enum StoreErrorKind { BackendError, NoCommandlineCall, - // maybe more + // maybe more } fn store_error_type_as_str(e: &StoreErrorKind) -> &'static str { - match e { - &StoreErrorKind::BackendError => "Backend Error", - &StoreErrorKind::NoCommandlineCall => "No commandline call", + match *e { + StoreErrorKind::BackendError => "Backend Error", + StoreErrorKind::NoCommandlineCall => "No commandline call", } } @@ -37,9 +35,7 @@ pub struct StoreError { impl StoreError { - /** - * Build a new StoreError from an StoreErrorKind, optionally with cause - */ + ///Build a new StoreError from an StoreErrorKind, optionally with cause pub fn new(errtype: StoreErrorKind, cause: Option>) -> StoreError { @@ -49,11 +45,9 @@ impl StoreError { } } - /** - * Get the error type of this StoreError - */ + /// Get the error type of this StoreError pub fn err_type(&self) -> StoreErrorKind { - self.err_type.clone() + self.err_type } } @@ -70,7 +64,7 @@ impl Display for StoreError { impl Error for StoreError { fn description(&self) -> &str { - store_error_type_as_str(&self.err_type.clone()) + store_error_type_as_str(&self.err_type) } fn cause(&self) -> Option<&Error> { diff --git a/imag-store/src/util.rs b/imag-store/src/util.rs index f14d462b..197f2846 100644 --- a/imag-store/src/util.rs +++ b/imag-store/src/util.rs @@ -1,4 +1,5 @@ -use std::collections::BTreeMap; +use std::borrow::Cow; +use std::collections::btree_map::{BTreeMap, Entry}; use std::str::Split; use clap::ArgMatches; @@ -21,10 +22,10 @@ pub fn build_toml_header(matches: &ArgMatches, header: EntryHeader) -> EntryHead for tpl in kvs { let (key, value) = tpl.into(); debug!("Splitting: {:?}", key); - let mut split = key.split("."); + let mut split = key.split('.'); let current = split.next(); if current.is_some() { - insert_key_into(String::from(current.unwrap()), &mut split, value, &mut main); + insert_key_into(String::from(current.unwrap()), &mut split, Cow::Owned(value), &mut main); } } @@ -36,9 +37,9 @@ pub fn build_toml_header(matches: &ArgMatches, header: EntryHeader) -> EntryHead } } -fn insert_key_into(current: String, - rest_path: &mut Split<&str>, - value: String, +fn insert_key_into<'a>(current: String, + rest_path: &mut Split, + value: Cow<'a, str>, map: &mut BTreeMap) { let next = rest_path.next(); @@ -47,27 +48,30 @@ fn insert_key_into(current: String, map.insert(current, parse_value(value)); } else { debug!("Inserting into {:?} ... = {:?}", current, value); - if map.contains_key(¤t) { - match map.get_mut(¤t).unwrap() { - &mut Value::Table(ref mut t) => { - insert_key_into(String::from(next.unwrap()), rest_path, value, t); - }, - _ => unreachable!(), + match map.entry(current) { + Entry::Occupied(ref mut e) => { + match *e.get_mut() { + Value::Table(ref mut t) => { + insert_key_into(String::from(next.unwrap()), rest_path, value, t); + }, + _ => unreachable!(), + } + }, + Entry::Vacant(v) => { v.insert(Value::Table( { + let mut submap = BTreeMap::new(); + insert_key_into(String::from(next.unwrap()), rest_path, value, &mut submap); + debug!("Inserting submap = {:?}", submap); + submap })); } - } else { - let mut submap = BTreeMap::new(); - insert_key_into(String::from(next.unwrap()), rest_path, value, &mut submap); - debug!("Inserting submap = {:?}", submap); - map.insert(current, Value::Table(submap)); } } } -fn parse_value(value: String) -> Value { +fn parse_value(value: Cow) -> Value { use std::str::FromStr; - fn is_ary(v: &String) -> bool { - v.chars().next() == Some('[') && v.chars().last() == Some(']') && v.len() >= 3 + fn is_ary(v: &str) -> bool { + v.starts_with('[') && v.ends_with(']') && v.len() >= 3 } if value == "true" { @@ -79,7 +83,7 @@ fn parse_value(value: String) -> Value { } else if is_ary(&value) { debug!("Building Array out of: {:?}...", value); let sub = &value[1..(value.len()-1)]; - Value::Array(sub.split(",").map(|v| parse_value(String::from(v))).collect()) + Value::Array(sub.split(',').map(|x| parse_value(Cow::from(x))).collect()) } else { FromStr::from_str(&value[..]) .map(|i: i64| { @@ -94,7 +98,7 @@ fn parse_value(value: String) -> Value { }) .unwrap_or_else(|_| { debug!("Building String out of: {:?}...", value); - Value::String(value) + Value::String(value.into_owned()) }) }) } diff --git a/imag-tag/src/main.rs b/imag-tag/src/main.rs index e1a35a34..9ed409a3 100644 --- a/imag-tag/src/main.rs +++ b/imag-tag/src/main.rs @@ -77,8 +77,7 @@ fn alter(rt: &Runtime, id: &str, add: Option<&str>, rem: Option<&str>, set: Opti .retrieve(path) .map(|mut e| { add.map(|tags| { - let tags = tags.split(","); - for tag in tags { + for tag in tags.split(',') { info!("Adding tag '{}'", tag); if let Err(e) = e.add_tag(String::from(tag)) { trace_error(&e); @@ -87,8 +86,7 @@ fn alter(rt: &Runtime, id: &str, add: Option<&str>, rem: Option<&str>, set: Opti }); rem.map(|tags| { - let tags = tags.split(","); - for tag in tags { + for tag in tags.split(',') { info!("Removing tag '{}'", tag); if let Err(e) = e.remove_tag(String::from(tag)) { trace_error(&e); @@ -98,7 +96,7 @@ fn alter(rt: &Runtime, id: &str, add: Option<&str>, rem: Option<&str>, set: Opti set.map(|tags| { info!("Setting tags '{}'", tags); - let tags = tags.split(",").map(String::from).collect(); + let tags = tags.split(',').map(String::from).collect(); if let Err(e) = e.set_tags(tags) { trace_error(&e); } diff --git a/imag-view/src/error.rs b/imag-view/src/error.rs index bdbb1d76..ab73bfe8 100644 --- a/imag-view/src/error.rs +++ b/imag-view/src/error.rs @@ -15,11 +15,11 @@ pub enum ViewErrorKind { } fn view_error_type_as_str(e: &ViewErrorKind) -> &'static str { - match e { - &ViewErrorKind::StoreError => "Store error", - &ViewErrorKind::NoVersion => "No version specified", - &ViewErrorKind::PatternError => "Error in Pattern", - &ViewErrorKind::GlobBuildError => "Could not build glob() Argument", + match *e { + ViewErrorKind::StoreError => "Store error", + ViewErrorKind::NoVersion => "No version specified", + ViewErrorKind::PatternError => "Error in Pattern", + ViewErrorKind::GlobBuildError => "Could not build glob() Argument", } } @@ -59,7 +59,7 @@ impl ViewError { * Get the error type of this ViewError */ pub fn err_type(&self) -> ViewErrorKind { - self.err_type.clone() + self.err_type } } @@ -67,7 +67,7 @@ impl ViewError { impl Display for ViewError { fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", view_error_type_as_str(&self.err_type.clone()))); + try!(write!(fmt, "[{}]", view_error_type_as_str(&self.err_type))); Ok(()) } @@ -76,7 +76,7 @@ impl Display for ViewError { impl Error for ViewError { fn description(&self) -> &str { - view_error_type_as_str(&self.err_type.clone()) + view_error_type_as_str(&self.err_type) } fn cause(&self) -> Option<&Error> { diff --git a/imag-view/src/main.rs b/imag-view/src/main.rs index f12e639d..100d9aab 100644 --- a/imag-view/src/main.rs +++ b/imag-view/src/main.rs @@ -129,7 +129,7 @@ fn load_entry<'a>(id: &str, let version = { if version.is_none() { - let r = id.split("~").last(); + let r = id.split('~').last(); if r.is_none() { warn!("No version"); return Err(ViewError::new(ViewErrorKind::NoVersion, None)); @@ -144,7 +144,7 @@ fn load_entry<'a>(id: &str, debug!("Building path from {:?} and {:?}", id, version); let mut path = rt.store().path().clone(); - if id.chars().next() == Some('/') { + if id.starts_with('/') { path.push(format!("{}~{}", &id[1..id.len()], version)); } else { path.push(format!("{}~{}", id, version)); @@ -161,7 +161,7 @@ fn view_versions_of(id: &str, rt: &Runtime) -> Result<()> { let mut path = rt.store().path().clone(); - if id.chars().next() == Some('/') { + if id.starts_with('/') { path.push(format!("{}~*", &id[1..id.len()])); } else { path.push(format!("{}~*", id)); diff --git a/libimagcounter/src/counter.rs b/libimagcounter/src/counter.rs index 2285071f..a93046f7 100644 --- a/libimagcounter/src/counter.rs +++ b/libimagcounter/src/counter.rs @@ -144,12 +144,12 @@ impl<'a> Counter<'a> { } trait FromStoreId { - fn from_storeid<'a>(&'a Store, StoreId) -> Result>; + fn from_storeid(&Store, StoreId) -> Result; } impl<'a> FromStoreId for Counter<'a> { - fn from_storeid<'b>(store: &'b Store, id: StoreId) -> Result> { + fn from_storeid(store: &Store, id: StoreId) -> Result { debug!("Loading counter from storeid: '{:?}'", id); match store.retrieve(id) { Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))), diff --git a/libimagcounter/src/error.rs b/libimagcounter/src/error.rs index f1c842f7..1aa347a6 100644 --- a/libimagcounter/src/error.rs +++ b/libimagcounter/src/error.rs @@ -1,6 +1,5 @@ use std::error::Error; use std::fmt::Error as FmtError; -use std::clone::Clone; use std::fmt::{Display, Formatter}; /** @@ -15,11 +14,11 @@ pub enum CounterErrorKind { } fn counter_error_type_as_str(e: &CounterErrorKind) -> &'static str { - match e { - &CounterErrorKind::StoreReadError => "Store read error", - &CounterErrorKind::StoreWriteError => "Store write error", - &CounterErrorKind::HeaderTypeError => "Header type error", - &CounterErrorKind::HeaderFieldMissingError => "Header field missing error", + match *e { + CounterErrorKind::StoreReadError => "Store read error", + CounterErrorKind::StoreWriteError => "Store write error", + CounterErrorKind::HeaderTypeError => "Header type error", + CounterErrorKind::HeaderFieldMissingError => "Header field missing error", } } @@ -59,7 +58,7 @@ impl CounterError { * Get the error type of this CounterError */ pub fn err_type(&self) -> CounterErrorKind { - self.err_type.clone() + self.err_type } } @@ -67,7 +66,7 @@ impl CounterError { impl Display for CounterError { fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{}]", counter_error_type_as_str(&self.err_type.clone()))); + try!(write!(fmt, "[{}]", counter_error_type_as_str(&self.err_type))); Ok(()) } @@ -76,7 +75,7 @@ impl Display for CounterError { impl Error for CounterError { fn description(&self) -> &str { - counter_error_type_as_str(&self.err_type.clone()) + counter_error_type_as_str(&self.err_type) } fn cause(&self) -> Option<&Error> { diff --git a/libimagentryfilter/src/builtin/header/field_gt.rs b/libimagentryfilter/src/builtin/header/field_gt.rs index 6975737b..7067399d 100644 --- a/libimagentryfilter/src/builtin/header/field_gt.rs +++ b/libimagentryfilter/src/builtin/header/field_gt.rs @@ -14,15 +14,15 @@ struct EqGt { impl Predicate for EqGt { fn evaluate(&self, v: Value) -> bool { - match &self.comp { - &Value::Integer(i) => { + match self.comp { + Value::Integer(i) => { match v { Value::Integer(j) => i > j, Value::Float(f) => (i as f64) > f, _ => false, } }, - &Value::Float(f) => { + Value::Float(f) => { match v { Value::Integer(i) => f > (i as f64), Value::Float(d) => f > d, diff --git a/libimagentryfilter/src/builtin/header/field_isempty.rs b/libimagentryfilter/src/builtin/header/field_isempty.rs index a7ff4ab1..3da9360f 100644 --- a/libimagentryfilter/src/builtin/header/field_isempty.rs +++ b/libimagentryfilter/src/builtin/header/field_isempty.rs @@ -27,11 +27,11 @@ impl Filter for FieldIsEmpty { .map(|v| { match v { Some(Value::Array(a)) => a.is_empty(), - Some(Value::Boolean(_)) => false, - Some(Value::Float(_)) => false, - Some(Value::Integer(_)) => false, Some(Value::String(s)) => s.is_empty(), Some(Value::Table(t)) => t.is_empty(), + Some(Value::Boolean(_)) | + Some(Value::Float(_)) | + Some(Value::Integer(_)) => false, _ => true, } }) diff --git a/libimagentryfilter/src/builtin/header/field_istype.rs b/libimagentryfilter/src/builtin/header/field_istype.rs index 8423c0ab..5af73623 100644 --- a/libimagentryfilter/src/builtin/header/field_istype.rs +++ b/libimagentryfilter/src/builtin/header/field_istype.rs @@ -21,11 +21,11 @@ impl Type { fn matches(&self, v: &Value) -> bool { match (self, v) { - (&Type::String, &Value::String(_)) => true, - (&Type::Integer, &Value::Integer(_)) => true, - (&Type::Float, &Value::Float(_)) => true, - (&Type::Boolean, &Value::Boolean(_)) => true, - (&Type::Array, &Value::Array(_)) => true, + (&Type::String, &Value::String(_)) | + (&Type::Integer, &Value::Integer(_)) | + (&Type::Float, &Value::Float(_)) | + (&Type::Boolean, &Value::Boolean(_)) | + (&Type::Array, &Value::Array(_)) | (&Type::Table, &Value::Table(_)) => true, _ => false, } diff --git a/libimagentryfilter/src/builtin/header/field_lt.rs b/libimagentryfilter/src/builtin/header/field_lt.rs index 15558d4b..dee98476 100644 --- a/libimagentryfilter/src/builtin/header/field_lt.rs +++ b/libimagentryfilter/src/builtin/header/field_lt.rs @@ -14,15 +14,15 @@ struct EqLt { impl Predicate for EqLt { fn evaluate(&self, v: Value) -> bool { - match &self.comp { - &Value::Integer(i) => { + match self.comp { + Value::Integer(i) => { match v { Value::Integer(j) => i < j, Value::Float(f) => (i as f64) < f, _ => false, } }, - &Value::Float(f) => { + Value::Float(f) => { match v { Value::Integer(i) => f < (i as f64), Value::Float(d) => f < d, diff --git a/libimagentryfilter/src/builtin/header/version/eq.rs b/libimagentryfilter/src/builtin/header/version/eq.rs index 0e28cfb2..e6b93893 100644 --- a/libimagentryfilter/src/builtin/header/version/eq.rs +++ b/libimagentryfilter/src/builtin/header/version/eq.rs @@ -23,7 +23,7 @@ impl Filter for VersionEq { e.get_header() .read("imag.version") .map(|val| { - val.map(|v| { + val.map_or(false, |v| { match v { Value::String(s) => { match Version::parse(&s[..]) { @@ -34,7 +34,6 @@ impl Filter for VersionEq { _ => false, } }) - .unwrap_or(false) }) .unwrap_or(false) } diff --git a/libimagentryfilter/src/builtin/header/version/gt.rs b/libimagentryfilter/src/builtin/header/version/gt.rs index 119e64a8..f03b0fa9 100644 --- a/libimagentryfilter/src/builtin/header/version/gt.rs +++ b/libimagentryfilter/src/builtin/header/version/gt.rs @@ -23,7 +23,7 @@ impl Filter for VersionGt { e.get_header() .read("imag.version") .map(|val| { - val.map(|v| { + val.map_or(false, |v| { match v { Value::String(s) => { match Version::parse(&s[..]) { @@ -34,7 +34,6 @@ impl Filter for VersionGt { _ => false, } }) - .unwrap_or(false) }) .unwrap_or(false) } diff --git a/libimagentryfilter/src/builtin/header/version/lt.rs b/libimagentryfilter/src/builtin/header/version/lt.rs index 6263f6de..5be6a191 100644 --- a/libimagentryfilter/src/builtin/header/version/lt.rs +++ b/libimagentryfilter/src/builtin/header/version/lt.rs @@ -23,7 +23,7 @@ impl Filter for VersionLt { e.get_header() .read("imag.version") .map(|val| { - val.map(|v| { + val.map_or(false, |v| { match v { Value::String(s) => { match Version::parse(&s[..]) { @@ -34,7 +34,6 @@ impl Filter for VersionLt { _ => false, } }) - .unwrap_or(false) }) .unwrap_or(false) }