Skip to content

Commit

Permalink
clippy and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodie committed Apr 23, 2019
1 parent 02a9baa commit f5e36cc
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 120 deletions.
154 changes: 87 additions & 67 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "asciii"
version = "3.5.0"
version = "3.5.1"
authors = ["Hendrik Sollich <[email protected]>"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand Down
14 changes: 7 additions & 7 deletions lang/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -556,31 +556,31 @@ msgstr ""
msgid "no remote name"
msgstr ""

#: /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:395
#: /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:396
msgid "You have to provide either a search term or path"
msgstr ""

#: /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:511
#: /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:512
msgid "do you want to set your name?"
msgstr ""

#: /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:513
#: /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:514
msgid "Is your name {:?}"
msgstr ""

#: /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:519
#: /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:520
msgid "What is your name?"
msgstr ""

#: /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:638 /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:648
#: /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:639 /home/hendrik/code/ascii/asciii/src/bin/cli/subcommands/mod.rs:649
msgid "{} does not exist"
msgstr ""

#: /home/hendrik/code/ascii/asciii/src/document_export/mod.rs:184
#: /home/hendrik/code/ascii/asciii/src/document_export/mod.rs:185
msgid "WARNING: Can't make sense of {}"
msgstr ""

#: /home/hendrik/code/ascii/asciii/src/document_export/mod.rs:222
#: /home/hendrik/code/ascii/asciii/src/document_export/mod.rs:223
msgid "Project file is younger than pdf, continue anyway?"
msgstr ""

Expand Down
6 changes: 3 additions & 3 deletions src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn unpayed_employees(projects: &[Project]) -> HashMap<String, Currency> {
let employees = projects.iter()
.filter(|p| !p.canceled() && p.age().unwrap_or(0) > 0)
.filter_map(|p| p.hours().employees())
.flat_map(|e| e.into_iter());
.flat_map(IntoIterator::into_iter);

for employee in employees {
let bucket = buckets.entry(employee.name.clone()).or_insert_with(Currency::new);
Expand Down Expand Up @@ -151,8 +151,8 @@ pub fn spec() -> Result<(), Error> {
project.age().map(|a|format!("{} days", a)).unwrap();
project.modified_date().map(|d|d.year().to_string()).unwrap();
project.sum_sold().map(|c|util::currency_to_string(&c)).unwrap();
project.responsible().map(|s|s.to_owned()).unwrap();
project.name().map(|s|s.to_owned()).unwrap();
project.responsible().map(ToOwned::to_owned).unwrap();
project.name().map(ToOwned::to_owned).unwrap();
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/bin/cli/subcommands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn list(matches: &ArgMatches<'_>) -> Result<(), Error> {
matches.is_present("csv"));

let extra_details = matches.values_of("details")
.map(|v| v.collect::<Vec<&str>>());
.map(Iterator::collect);
let config_details = CONFIG.get_strs("list/extra_details");

let mut list_config = ListConfig {
Expand All @@ -36,7 +36,7 @@ pub fn list(matches: &ArgMatches<'_>) -> Result<(), Error> {
mode: list_mode,
details: extra_details.or(config_details),
filter_by: matches.values_of("filter")
.map(|v| v.collect::<Vec<&str>>()),
.map(Iterator::collect),
show_errors: matches.is_present("errors"),

..Default::default()
Expand Down
23 changes: 12 additions & 11 deletions src/bin/cli/subcommands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use clap::ArgMatches;
use failure::{bail, format_err, Error};
use chrono::prelude::*;
use log::{debug, trace, error, warn};
use yaml_rust::Yaml;

use asciii::{self, CONFIG, config, util, actions};
use asciii::project::Exportable;
Expand Down Expand Up @@ -43,7 +44,7 @@ pub use self::show::*;
// #[deprecated(note="move to asciii::actions")]
pub fn new(matches: &ArgMatches<'_>) -> Result<(), Error> {
let project_name = matches.value_of("name").expect("You did not pass a \"Name\"!");
let editor = CONFIG.get("user/editor").and_then(|e| e.as_str());
let editor = CONFIG.get("user/editor").and_then(Yaml::as_str);

let template_name = matches.value_of("template")
.or_else(||CONFIG.get("template").unwrap().as_str())
Expand Down Expand Up @@ -118,7 +119,7 @@ fn matches_to_dir(matches: &ArgMatches<'_>) -> StorageDir {
fn matches_to_search<'a>(matches: &'a ArgMatches<'_>) -> (Vec<&'a str>, StorageDir) {
let search_terms = matches
.values_of("search_term")
.map(|v| v.collect::<Vec<&str>>())
.map(Iterator::collect)
.unwrap_or_else(Vec::new);

debug!("matches_to_search: --archive={:?}", matches.value_of("archive"));
Expand All @@ -133,7 +134,7 @@ fn matches_to_search<'a>(matches: &'a ArgMatches<'_>) -> (Vec<&'a str>, StorageD
/// This is more general than `with_projects`, as this includes templates too.
pub fn matches_to_paths(matches: &ArgMatches<'_>, storage: &Storage<Project>) -> Result<Vec<PathBuf>, Error> {
let search_terms = matches.values_of("search_term")
.map(|v| v.collect::<Vec<&str>>())
.map(Iterator::collect)
.unwrap_or_else(Vec::new);

if matches.is_present("template") {
Expand All @@ -155,7 +156,7 @@ pub fn matches_to_paths(matches: &ArgMatches<'_>, storage: &Storage<Project>) ->

Ok(storage.search_projects_any(dir, &search_terms)?
.iter()
.map(|project| project.dir())
.map(Storable::dir)
.collect::<Vec<_>>())
}
}
Expand All @@ -168,7 +169,7 @@ pub fn bootstrap(matches: &ArgMatches<'_>) -> Result<(), Error> {
let repo = matches.value_of("repo").unwrap();
let editor = matches.value_of("editor")
.or_else(|| CONFIG.get("user.editor")
.and_then(|e|e.as_str()));
.and_then(Yaml::as_str));

let default_to = get_storage_path()
.to_str()
Expand Down Expand Up @@ -205,7 +206,7 @@ pub fn edit(matches: &ArgMatches<'_>) -> Result<(), Error> {

let editor = matches.value_of("editor")
.or_else(|| CONFIG.get("user/editor")
.and_then(|e| e.as_str()));
.and_then(Yaml::as_str));

if matches.is_present("template") {
with_templates(search_term,
Expand Down Expand Up @@ -237,7 +238,7 @@ fn edit_projects(dir: StorageDir, search_terms: &[&str], editor: Option<&str>) -
if all_projects.is_empty() {
bail!(ActionError::NothingFound(search_terms.iter().map(ToString::to_string).collect()));
} else {
let all_paths = all_projects.iter().map(|p| p.file()).collect::<Vec<PathBuf>>();
let all_paths = all_projects.iter().map(Storable::file).collect::<Vec<PathBuf>>();
util::pass_to_command(editor, &all_paths);
Ok(())
}
Expand Down Expand Up @@ -284,7 +285,7 @@ pub fn workspace(matches: &ArgMatches<'_>) -> Result<(), Error> {

let editor = matches.value_of("editor")
.or_else(|| CONFIG.get("user/editor")
.and_then(|e| e.as_str()));
.and_then(Yaml::as_str));
util::pass_to_command(editor, &[storage.working_dir()]);
Ok(())
}
Expand All @@ -306,7 +307,7 @@ pub fn set(m: &ArgMatches<'_>) -> Result<(), Error> {
let field = m.value_of("field name")
.unwrap()
.chars()
.flat_map(|c| c.to_uppercase())
.flat_map(char::to_uppercase)
.collect::<String>();
let value = m.value_of("field value").unwrap();
let (search_terms, dir) = matches_to_search(m);
Expand Down Expand Up @@ -363,7 +364,7 @@ fn infer_bill_type(m: &ArgMatches<'_>) -> Option<BillType> {
fn matches_to_export_config<'a>(m: &'a ArgMatches<'_>) -> Option<ExportConfig<'a>> {

let template_name = m.value_of("template")
.or_else(||CONFIG.get("document_export/default_template").and_then(|e| e.as_str()))
.or_else(||CONFIG.get("document_export/default_template").and_then(Yaml::as_str))
.unwrap();
let bill_type = infer_bill_type(m);

Expand Down Expand Up @@ -466,7 +467,7 @@ pub fn unarchive(matches: &ArgMatches<'_>) -> Result<(), Error> {
pub fn config(matches: &ArgMatches<'_>) -> Result<(), Error> {
let editor = matches.value_of("editor")
.or_else(|| CONFIG.get("user.editor")
.and_then(|e|e.as_str()));
.and_then(Yaml::as_str));

if let Some(path) = matches.value_of("show") {
config_show(path)?;
Expand Down
6 changes: 3 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl ConfigReader {

fn envify_path(path: &str) -> String {
path.split(|c| c == '/' || c == '.')
.map(|word| word.to_uppercase())
.map(str::to_uppercase)
.fold(String::from("ASCIII"), |mut acc, w| {
acc.push_str("_");
acc.push_str(&w);
Expand Down Expand Up @@ -133,7 +133,7 @@ impl ConfigReader {
.as_vec()
.map(|v| {
v.iter()
.filter_map(|s| s.as_str())
.filter_map(Yaml::as_str)
.collect()
})
}
Expand Down Expand Up @@ -167,7 +167,7 @@ impl ConfigReader {
/// You should have a default config for everything that you use.
pub fn get_bool(&self, key: &str) -> bool {
self.get(key)
.and_then(|y| y.as_bool())
.and_then(Yaml::as_bool)
.unwrap_or_else(|| panic!("{}", format!("Config file {} in field {} does not contain a boolean value",
DEFAULT_LOCATION,
key)))
Expand Down
5 changes: 3 additions & 2 deletions src/document_export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::path::{Path, PathBuf};

use serde::ser::Serialize;
use failure::{bail, Error};
use yaml_rust::Yaml;

use open;
use handlebars::{Handlebars, no_escape, Helper, RenderContext, HelperDef, Context, Output, HelperResult};
Expand Down Expand Up @@ -58,7 +59,7 @@ struct CountHelper;
impl HelperDef for CountHelper {
#[allow(clippy::extra_unused_lifetimes)]
fn call<'reg: 'rc, 'rc>(&self, h: &Helper<'_, '_>, _: &Handlebars, _: &Context, _: &mut RenderContext<'_>, out: &mut dyn Output) -> HelperResult {
let count = h.param(0).unwrap().value().as_array().map_or(0, |a|a.len());
let count = h.param(0).unwrap().value().as_array().map_or(0, Vec::len);
out.write(&format!("{}", count))?;
Ok(())
}
Expand Down Expand Up @@ -137,7 +138,7 @@ fn project_to_doc(project: &Project, config: &ExportConfig<'_>) -> Result<Option
.expect("Faulty default config")
.as_vec().expect("Faulty default config")
.iter()
.map(|v|v.as_str()).collect::<Vec<_>>();
.map(Yaml::as_str).collect::<Vec<_>>();

let template_path = output_template_path(template_name)?;
debug!("converting with {:?}", convert_tool);
Expand Down
2 changes: 1 addition & 1 deletion src/project/computed_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl ComputedField {
let storage = storage::get_storage_path();

match *self {
ComputedField::Responsible => project.responsible().map(|s| s.to_owned()),
ComputedField::Responsible => project.responsible().map(ToOwned::to_owned),
ComputedField::OfferNumber => project.offer().number(),
ComputedField::InvoiceNumber => project.invoice().number_str(),
ComputedField::InvoiceNumberLong => project.invoice().number_long_str(),
Expand Down
2 changes: 1 addition & 1 deletion src/project/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait ExportTarget<T> {
}

fn opt_str(opt: Option<&str>) -> Option<String> {
opt.map(|e| e.to_owned())
opt.map(ToOwned::to_owned)
}

#[derive(Debug, PartialEq)]
Expand Down
4 changes: 2 additions & 2 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ impl Storable for Project {

fn short_desc(&self) -> String {
self.name()
.map(|n|n.to_owned())
.map(ToOwned::to_owned)
.unwrap_or_else(|| format!("unnamed: {:?}",
self.dir()
.file_name()
Expand Down Expand Up @@ -652,7 +652,7 @@ impl Storable for Project {
let project_file_extension = crate::CONFIG.get_to_string("extensions.project_file");
let file_path = list_path_content(folder_path)?.iter()
.filter(|f|f.extension().unwrap_or(&OsStr::new("")) == project_file_extension.as_str())
.nth(0).map(|b|b.to_owned())
.nth(0).map(ToOwned::to_owned)
.ok_or_else(|| StorageError::NoProjectFile(folder_path.to_owned()))?;
Self::open_file(&file_path)
}
Expand Down
22 changes: 11 additions & 11 deletions src/project/spec_yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,20 @@ pub trait ProvidesData {
///
/// Same mentality as `yaml_rust`, only returns `Some`, if it's a `Yaml::String`.
fn get_str<'a>(&'a self, path: &str) -> Option<&'a str> {
self.get(path).and_then(|y| y.as_str())
self.get(path).and_then(Yaml::as_str)
}

/// Gets an `Int` value.
///
/// Same mentality as `yaml_rust`, only returns `Some`, if it's a `Yaml::Int`.
fn get_int<'a>(&'a self, path: &str) -> Option<i64> {
self.get(path).and_then(|y| y.as_i64())
self.get(path).and_then(Yaml::as_i64)
}

/// Gets a Date in `dd.mm.YYYY` format.
fn get_dmy(&self, path: &str) -> Option<Date<Utc>> {
self.get(path)
.and_then(|y| y.as_str())
.and_then(Yaml::as_str)
.and_then(|d| self.parse_dmy_date(d))
}

Expand Down Expand Up @@ -150,7 +150,7 @@ pub trait ProvidesData {
/// Gets `Some(Yaml::Hash)` or `None`.
//pub fn get_hash<'a>(yaml:&'a Yaml, key:&str) -> Option<&'a BTreeMap<Yaml,Yaml>> {
fn get_hash<'a>(&'a self, path: &str) -> Option<&'a YamlHash> {
self.get(path).and_then(|y| y.as_hash())
self.get(path).and_then(Yaml::as_hash)
}

/// Gets a `Float` value.
Expand Down Expand Up @@ -274,16 +274,16 @@ impl HasEvents for Project {
#[allow(unused_qualifications)]
fn events(&self) -> Option<Vec<spec::Event>> {
let dates = ProvidesData::get(self, "event.dates/")
.and_then(|a| a.as_vec())?;
.and_then(Yaml::as_vec)?;
dates.iter()
.map(|h| {

let begin = self.get_direct(h, "begin")
.and_then(|y| y.as_str())
.and_then(Yaml::as_str)
.and_then(|d| self.parse_dmy_date(d))?;

let end = self.get_direct(h, "end")
.and_then(|y| y.as_str())
.and_then(Yaml::as_str)
.and_then(|d| self.parse_dmy_date(d));

Some(spec::Event {
Expand All @@ -296,17 +296,17 @@ impl HasEvents for Project {
}

fn times(&self, yaml: &Yaml) -> Option<Vec<EventTime>> {
let times = self.get_direct(yaml, "times").and_then(|l| l.as_vec())?;
let times = self.get_direct(yaml, "times").and_then(Yaml::as_vec)?;
times.iter()
.map(|h| {

let start = self.get_direct(h, "begin")
.and_then(|y| y.as_str())
.and_then(Yaml::as_str)
.or(Some("00.00"))
.and_then(util::naive_time_from_str);

let end = self.get_direct(h, "end")
.and_then(|y| y.as_str())
.and_then(Yaml::as_str)
.and_then(util::naive_time_from_str)
.or(start); // TODO assume a duration of one hour instead

Expand Down Expand Up @@ -549,7 +549,7 @@ impl<'a> Offerable for Offer<'a> {
.map(|s| format!("{}-{}", s, num))

// old spec
.or_else(|| self.get_str("manumber").map(|s|s.to_string()))
.or_else(|| self.get_str("manumber").map(ToString::to_string))
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub fn list_path_content(path:&Path) -> Result<Vec<PathBuf>, Error> {
}

Ok(fs::read_dir(path)?
.filter_map(|entry| entry.ok())
.filter_map(Result::ok)
.map(|entry| entry.path())
.collect::<Vec<PathBuf>>())
}
Expand Down Expand Up @@ -610,7 +610,7 @@ impl<L:Storable> Storage<L> {

// must be in a dir that parses into a year
let parent_is_num = archived_dir.parent()
.and_then(|p| p.file_stem())
.and_then(Path::file_stem)
.and_then(OsStr::to_str)
.map_or(false, |s| s.parse::<i32>().is_ok());

Expand Down Expand Up @@ -695,7 +695,7 @@ impl<L:Storable> Storage<L> {
fn get_project_dir_from_archive(&self, name:&str, year:Year) -> Result<PathBuf, Error> {
for project_file in &self.list_project_files(StorageDir::Archive(year))?{
if project_file.ends_with(slugify(name) + "."+ &L::file_extension()) {
return project_file.parent().map(|p|p.to_owned()).ok_or_else (|| StorageError::ProjectDoesNotExist.into());
return project_file.parent().map(ToOwned::to_owned).ok_or_else (|| StorageError::ProjectDoesNotExist.into());
}
}
bail!(StorageError::ProjectDoesNotExist)
Expand Down
2 changes: 1 addition & 1 deletion src/storage/storable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub trait Storable: Send+Sync {
fn from_template(project_name: &str, template: &Path, data: &HashMap<&str, String>) -> Result<StorableAndTempDir<Self>, Error> where Self: Sized;

/// For file names
fn ident(&self) -> String{ self.dir().file_stem().and_then(|s|s.to_str()).unwrap().to_owned() }
fn ident(&self) -> String{ self.dir().file_stem().and_then(std::ffi::OsStr::to_str).unwrap().to_owned() }

fn short_desc(&self) -> String;
fn modified_date(&self) -> Option<Date<Utc>>;
Expand Down
Loading

0 comments on commit f5e36cc

Please sign in to comment.