~neon/activity-graph

91b095c772f1033690fb66fd5cf34735bef9da97 — Jens Pitkanen 2 years ago 61f5fd0
Fix clippy warnings
6 files changed, 39 insertions(+), 37 deletions(-)

M src/commits.rs
M src/find_repositories.rs
M src/log.rs
M src/main.rs
M src/render.rs
M src/server.rs
M src/commits.rs => src/commits.rs +5 -8
@@ 38,16 38,13 @@ pub fn find_dates(
    });

    #[cfg(feature = "rayon")]
    let commit_dates = commit_dates.reduce(
        || Vec::new(),
        |mut a, b| {
            a.extend(b);
            a
        },
    );
    let commit_dates = commit_dates.reduce(Vec::new, |mut a, b| {
        a.extend(b);
        a
    });
    #[cfg(not(feature = "rayon"))]
    let commit_dates = commit_dates.fold(Vec::new(), |mut a, b| {
        a.extend(&b);
        a.extend(b);
        a
    });


M src/find_repositories.rs => src/find_repositories.rs +6 -5
@@ 1,4 1,5 @@
use std::collections::HashSet;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};



@@ 38,22 39,22 @@ fn analyze_dir(
    if let Some(path) = path
        .canonicalize()
        .ok()
        .map(|path| path.into_os_string())
        .map(PathBuf::into_os_string)
        .and_then(|path| path.into_string().ok())
    {
        log::verbose_println(&format!("scanning: {}\r", path), true);
    }

    let dirs: Vec<fs::DirEntry> = dirs.filter_map(|result| result.ok()).collect();
    let dirs: Vec<fs::DirEntry> = dirs.filter_map(Result::ok).collect();
    if dirs
        .iter()
        .map(|dir_entry| dir_entry.file_name())
        .map(fs::DirEntry::file_name)
        .any(|file_name| file_name == ".git")
    {
        if let Some(name) = path
            .file_name()
            .and_then(|os_str| os_str.to_str())
            .map(|s| s.to_string())
            .and_then(OsStr::to_str)
            .map(ToString::to_string)
        {
            git_paths.insert(ProjectMetadata {
                name,

M src/log.rs => src/log.rs +1 -4
@@ 15,10 15,7 @@ pub fn set_verbosity(verbose: bool) {

pub fn verbose_println(s: &str, updating_line: bool) {
    if VERBOSE.load(Ordering::Relaxed) {
        let width = term_size::dimensions()
            .map(|(w, _)| w - 1)
            .unwrap_or(70)
            .max(4);
        let width = term_size::dimensions().map_or(70, |(w, _)| w - 1).max(4);

        if updating_line {
            // Throttle the line updates to once per 20ms, 50 Hz is plenty real-time.

M src/main.rs => src/main.rs +12 -3
@@ 1,4 1,13 @@
// TODO: Run clippy, and add a #warn for it
// First, enable all the warnings.
#![warn(clippy::all, clippy::pedantic)]
// Then, disable the pedantic warnings I don't like.
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_possible_wrap,
    clippy::cast_precision_loss,
    clippy::must_use_candidate
)]

use structopt::StructOpt;



@@ 30,7 39,7 @@ pub struct Day {

#[derive(Clone)]
pub struct Year {
    year: i32,
    year: usize,
    days: Vec<Day>,
}



@@ 124,7 133,7 @@ fn main() {
        match command {
            CommandArgs::Generate { html, css } => {
                let write_to_file = |path: &Path, s: String, name: &str| {
                    let mut writer = File::create(path).map(|file| BufWriter::new(file));
                    let mut writer = File::create(path).map(BufWriter::new);
                    match &mut writer {
                        Ok(writer) => {
                            if let Err(err) = writer.write(&s.as_bytes()) {

M src/render.rs => src/render.rs +11 -11
@@ 5,7 5,7 @@ use chrono::{DateTime, Datelike, Utc};

use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Component, PathBuf};
use std::path::{Component, Path, PathBuf};

use crate::{log, Day, ExternalResources, ProjectMetadata, Year};



@@ 20,14 20,14 @@ pub fn gather_years(mut commit_dates: Vec<(DateTime<Utc>, ProjectMetadata)>) -> 

    commit_dates.sort_by(|(a, _), (b, _)| a.cmp(b));

    let get_year = |date: DateTime<Utc>| date.date().year();
    let get_year = |date: DateTime<Utc>| date.date().year() as usize;
    let first_year = get_year(commit_dates[0].0);
    let last_year = get_year(commit_dates[commit_dates.len() - 1].0);

    // Years is a vec containing vecs of years, which consist
    // of weekday-major grids of days: eg. the first row
    // represents all of the mondays in the year, in order.
    let mut years = Vec::with_capacity((last_year - first_year + 1) as usize);
    let mut years = Vec::with_capacity(last_year - first_year + 1);
    for year in first_year..=last_year {
        years.push(Year {
            year,


@@ 40,14 40,14 @@ pub fn gather_years(mut commit_dates: Vec<(DateTime<Utc>, ProjectMetadata)>) -> 
    for year in first_year..=last_year {
        // Loop through the years

        let weekday_offset = NaiveDate::from_ymd(year, 1, 1)
        let weekday_offset = NaiveDate::from_ymd(year as i32, 1, 1)
            .weekday()
            .num_days_from_monday() as usize;
        let last_day =
            weekday_offset + NaiveDate::from_ymd(year + 1, 1, 1).pred().ordinal() as usize;
            weekday_offset + NaiveDate::from_ymd(year as i32 + 1, 1, 1).pred().ordinal() as usize;
        let last_week = (last_day - (last_day % 7)) / 7;

        let (before, after) = years.split_at_mut((year + 1 - first_year) as usize);
        let (before, after) = years.split_at_mut(year - first_year + 1);
        let (before, current) = before.split_at_mut(before.len() - 1);
        let days = &mut current[0].days;
        let mut last_year_days = if year > first_year {


@@ 64,11 64,11 @@ pub fn gather_years(mut commit_dates: Vec<(DateTime<Utc>, ProjectMetadata)>) -> 
            // Loop through the days until the commit is from
            // next year or commits run out

            if date.year() != year {
            if date.year() != year as i32 {
                break;
            }

            let ordinal_with_offset = (date.ordinal0()) as usize + weekday_offset;
            let ordinal_with_offset = date.ordinal0() as usize + weekday_offset;
            let weekday_index = ordinal_with_offset % 7;
            let week_index = ordinal_with_offset / 7;
            if week_index < WEEKS {


@@ 146,7 146,7 @@ pub fn html(
    if let (Some(base), Some(css_path)) = (html_path.parent(), &css_path) {
        if let Some(relative_path) = pathdiff::diff_paths(&css_path, base) {
            // Add the <link> element instead of <style> if using external css
            let path = create_web_path(relative_path);
            let path = create_web_path(&relative_path);
            style = Some(format!("<link href=\"{}\" rel=\"stylesheet\">", path));
        }
    }


@@ 226,7 226,7 @@ pub fn ascii(years: &[Year]) -> String {
    result
}

fn create_web_path(path: PathBuf) -> String {
fn create_web_path(path: &Path) -> String {
    path.components()
        .filter_map(|component| match component {
            Component::Normal(s) => s.to_str(),


@@ 235,7 235,7 @@ fn create_web_path(path: PathBuf) -> String {
            _ => None,
        })
        .fold(String::new(), |mut a, b| {
            if a.len() > 0 {
            if a.is_empty() {
                a += "/";
            }
            a += b;

M src/server.rs => src/server.rs +4 -6
@@ 14,8 14,8 @@ use crate::{generate_years, log, render, Args, ExternalResources, GenerationData

lazy_static::lazy_static! {
    // These are set before the server is run, and only used in responses
    static ref GENERATION_DATA: RwLock<GenerationData> = RwLock::new(Default::default());
    static ref EXTERNAL_HTML: RwLock<ExternalResources> = RwLock::new(Default::default());
    static ref GENERATION_DATA: RwLock<GenerationData> = RwLock::new(GenerationData::default());
    static ref EXTERNAL_HTML: RwLock<ExternalResources> = RwLock::new(ExternalResources::default());
    static ref LAST_CACHE: RwLock<Instant> = RwLock::new(Instant::now());
    static ref CACHE_LIFETIME: RwLock<Duration> = RwLock::new(Duration::from_secs(0));



@@ 127,9 127,7 @@ async fn refresh_caches() {

    // If the cache hasn't been initialized yet, wait for the refresh
    // to run by `await`ing it.
    if !CACHE_INITIALIZED.load(Ordering::Relaxed) {
        if let Ok(_) = task.await {
            CACHE_INITIALIZED.store(true, Ordering::Relaxed);
        }
    if !CACHE_INITIALIZED.load(Ordering::Relaxed) && task.await.is_ok() {
        CACHE_INITIALIZED.store(true, Ordering::Relaxed);
    }
}