From 4130ae4a1de234920033f09a38281690d27e003a Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 28 Feb 2022 14:47:17 +0300 Subject: [PATCH] fix(style): clippy warnings --- src/comp/break_timer.rs | 8 +++---- src/comp/deinit.rs | 9 ++++---- src/comp/timer.rs | 27 ++++++++++++---------- src/config.rs | 2 +- src/delegate.rs | 2 +- src/env.rs | 50 +++++++++++++++++++++-------------------- src/main.rs | 4 +++- src/sound.rs | 1 + src/state.rs | 7 +++--- src/win/notifier.rs | 8 +++---- src/win/rest.rs | 8 +++---- src/win/status.rs | 18 +++++++-------- 12 files changed, 76 insertions(+), 68 deletions(-) diff --git a/src/comp/break_timer.rs b/src/comp/break_timer.rs index 34f9741..7d433bd 100644 --- a/src/comp/break_timer.rs +++ b/src/comp/break_timer.rs @@ -8,9 +8,9 @@ use std::rc::Rc; pub fn build( name: &str, - duration_env_key: Key, - postpone_duration_env_key: Key, - rest_duration_env_key: Key, + duration_env_key: &Key, + postpone_duration_env_key: &Key, + rest_duration_env_key: &Key, sound_sender: Rc, ) -> impl Widget { comp::flex::row_sta_sta() @@ -18,7 +18,7 @@ pub fn build( .with_child( comp::timer::build() .controller( - comp::timer::TimerController::new(move |ctx, _env, rest_duration_secs| { + comp::timer::Controller::new(move |ctx, _env, rest_duration_secs| { sound_sender.send(sound::Type::EndBreakTimer).ok(); ctx.submit_command(cmd::PAUSE_ALL_TIMER_COMP); diff --git a/src/comp/deinit.rs b/src/comp/deinit.rs index 813aee9..a9cb1c7 100644 --- a/src/comp/deinit.rs +++ b/src/comp/deinit.rs @@ -1,13 +1,12 @@ use crate::cmd; -use druid::widget::Controller; use druid::{Data, Env, Event, EventCtx, LifeCycle, LifeCycleCtx, UpdateCtx, Widget}; #[derive(Default)] -pub struct DeinitController { +pub struct Controller { deinit: bool, } -impl Controller for DeinitController +impl druid::widget::Controller for Controller where D: Data, W: Widget, @@ -30,13 +29,13 @@ where env: &Env, ) { if !self.deinit { - child.lifecycle(ctx, event, data, env) + child.lifecycle(ctx, event, data, env); } } fn update(&mut self, child: &mut W, ctx: &mut UpdateCtx, old_data: &D, data: &D, env: &Env) { if !self.deinit { - child.update(ctx, old_data, data, env) + child.update(ctx, old_data, data, env); } } } diff --git a/src/comp/timer.rs b/src/comp/timer.rs index da1fb20..2737853 100644 --- a/src/comp/timer.rs +++ b/src/comp/timer.rs @@ -2,7 +2,7 @@ use crate::cmd; use crate::comp; use crate::env; use crate::state; -use druid::widget::{Controller, Label, ProgressBar}; +use druid::widget::{Label, ProgressBar}; use druid::{Env, Event, EventCtx, KeyOrValue, TimerToken, Widget, WidgetExt}; use std::time::{Duration, Instant}; @@ -17,7 +17,9 @@ pub fn build() -> impl Widget { .with_child(progress_bar) } -pub struct TimerController { +type FinishHandler = Box; + +pub struct Controller { duration: KeyOrValue, init_duration: Option>, postpone_duration: Option>, @@ -26,25 +28,25 @@ pub struct TimerController { pause_time: Option, render_timer_id: TimerToken, finish_timer_id: TimerToken, - finish_handler: Option>, + finish_handler: Option, postpone_times: u32, } -impl TimerController { +impl Controller { pub fn new(finish_handler: Handler) -> Self where Handler: Fn(&mut EventCtx, &Env, f64) + 'static, { - Self { + Controller { finish_handler: Some(Box::new(finish_handler)), - ..Default::default() + ..Controller::default() } } } -impl Default for TimerController { +impl Default for Controller { fn default() -> Self { - Self { + Controller { duration: env::TIMER_DURATION.into(), init_duration: None, postpone_duration: None, @@ -59,7 +61,7 @@ impl Default for TimerController { } } -impl TimerController { +impl Controller { pub fn with_duration(mut self, secs: impl Into>) -> Self { self.duration = secs.into(); self @@ -81,7 +83,7 @@ impl TimerController { } } -impl Controller for TimerController +impl druid::widget::Controller for Controller where W: Widget, { @@ -159,7 +161,7 @@ where self.postpone_times = 0; self.start_time = Instant::now(); if data.paused { - self.pause_time = Some(self.start_time) + self.pause_time = Some(self.start_time); } else { self.render_timer_id = ctx.request_timer(TIMER_INTERVAL); self.finish_timer_id = ctx.request_timer(duration); @@ -176,7 +178,7 @@ where } } -impl TimerController { +impl Controller { fn full_rest_duration(&self, env: &Env) -> Duration { self.rest_duration(env) + self.postpone_times * self.postpone_rest_duration(env) } @@ -210,6 +212,7 @@ impl TimerController { Duration::from_secs_f64(self.duration.resolve(env)) } + #[allow(clippy::single_match_else)] fn postpone_duration(&self, env: &Env) -> Duration { match self.postpone_times { 0 => Duration::ZERO, diff --git a/src/config.rs b/src/config.rs index a5f3940..d8be889 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,7 +18,7 @@ pub fn default() -> Env { } fn mins(m: f64) -> f64 { - return m * 60.0; + m * 60.0 } #[derive(Deserialize)] diff --git a/src/delegate.rs b/src/delegate.rs index 5533b13..14c4707 100644 --- a/src/delegate.rs +++ b/src/delegate.rs @@ -71,7 +71,7 @@ impl AppDelegate for Delegate { druid::commands::CONFIGURE_WINDOW .with(druid::WindowConfig::default().set_level(druid_shell::WindowLevel::Modal)) .to(Target::Window(id)), - ) + ); } } } diff --git a/src/env.rs b/src/env.rs index c840763..e5b6505 100644 --- a/src/env.rs +++ b/src/env.rs @@ -65,52 +65,54 @@ pub fn configure(env: &mut Env, config: &config::Env) { ); } +#[allow(clippy::many_single_char_names)] fn hsl(h: f64, s: f64, l: f64) -> Color { hsla(h, s, l, 1.0) } +#[allow(clippy::many_single_char_names)] fn hsla(h: f64, s: f64, l: f64, a: f64) -> Color { let (r, g, b) = hsl_to_rgb(h, s, l); Color::rgba(r, g, b, a) } +#[allow(clippy::many_single_char_names)] fn hsl_to_rgb(h: f64, s: f64, l: f64) -> (f64, f64, f64) { - let r; - let g; - let b; - if s == 0.0 { - r = l; - g = l; - b = l; // achromatic + (l, l, l) } else { - let q = if l < 0.5 { l * (1. + s) } else { l + s - l * s }; + let q = if l < 0.5 { + l * (1.0 + s) + } else { + l + s - l * s + }; - let p = 2. * l - q; - r = hue_to_rgb(p, q, h + 1. / 3.); - g = hue_to_rgb(p, q, h); - b = hue_to_rgb(p, q, h - 1. / 3.); + let p = 2.0 * l - q; + ( + hue_to_rgb(p, q, h + 1.0 / 3.0), + hue_to_rgb(p, q, h), + hue_to_rgb(p, q, h - 1.0 / 3.0), + ) } - - return (r, g, b); } fn hue_to_rgb(p: f64, q: f64, t: f64) -> f64 { let mut t = t; - if t < 0. { - t += 1. + if t < 0.0 { + t += 1.0; } - if t > 1. { - t -= 1. + if t > 1.0 { + t -= 1.0; }; - if t < 1. / 6. { - return p + (q - p) * 6. * t; + if t < 1.0 / 6.0 { + return p + (q - p) * 6.0 * t; } - if t < 1. / 2. { + if t < 1.0 / 2.0 { return q; } - if t < 2. / 3. { - return p + (q - p) * (2. / 3. - t) * 6.; + if t < 2.0 / 3.0 { + return p + (q - p) * (2.0 / 3.0 - t) * 6.0; } - return p; + + p } diff --git a/src/main.rs b/src/main.rs index 5ee227a..77e848b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![deny(clippy::pedantic)] + mod cmd; mod comp; mod config; @@ -14,7 +16,7 @@ use std::sync::mpsc::channel; use std::thread; fn main() { - let cfg = if let Some(config_path) = std::env::args().skip(1).next() { + let cfg = if let Some(config_path) = std::env::args().nth(1) { log::debug!("[cli] config_path={}", config_path); let file_content = std::fs::read_to_string(config_path).unwrap_or_else(|err| { eprintln!("[ERROR] [fs] {}", err); diff --git a/src/sound.rs b/src/sound.rs index d4bcf6e..60ebcef 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -1,5 +1,6 @@ pub type Sender = std::sync::mpsc::Sender; +#[allow(clippy::enum_variant_names)] pub enum Type { EndBreakTimer, EndNotifier, diff --git a/src/state.rs b/src/state.rs index 71693cf..2dd4c3e 100644 --- a/src/state.rs +++ b/src/state.rs @@ -49,13 +49,13 @@ impl Timer { pub fn new(paused: bool) -> Self { Self { progress: Default::default(), - time: Default::default(), + time: String::default(), paused, } } pub fn reset(&mut self, duration: Duration) { - self.update_progress_and_time(Duration::ZERO, duration) + self.update_progress_and_time(Duration::ZERO, duration); } pub fn update_progress_and_time(&mut self, elapsed: Duration, full_duration: Duration) { @@ -67,6 +67,7 @@ impl Timer { self.progress = elapsed.as_secs_f64().div(duration.as_secs_f64()).min(1.0); } + #[allow(clippy::cast_possible_truncation)] fn update_time(&mut self, elapsed: Duration, duration: Duration) { let all_secs = duration.as_secs_f64() - elapsed.as_secs_f64(); let sign = if all_secs < 0.0 { "-" } else { "" }; @@ -79,7 +80,7 @@ impl Timer { sign, mins as i32, if secs < 10.0 { "0" } else { "" }, - secs as u32 + secs as i32 ); } } diff --git a/src/win/notifier.rs b/src/win/notifier.rs index 441a70d..31f0d82 100644 --- a/src/win/notifier.rs +++ b/src/win/notifier.rs @@ -19,12 +19,12 @@ pub fn create( let x = (rect.width() - win_width) / 2.0; let y = 0.0; - return WindowDesc::new(move || build(parent_widget_id, rest_duration_secs, sound_sender)) + WindowDesc::new(move || build(parent_widget_id, rest_duration_secs, sound_sender)) .show_titlebar(false) .menu(MenuDesc::empty()) .set_position((x, y)) .with_min_size((win_width, win_height)) - .window_size((win_width, win_height)); + .window_size((win_width, win_height)) } fn build( @@ -49,7 +49,7 @@ fn build_notifier_timer( ) -> impl Widget { comp::timer::build() .controller( - comp::timer::TimerController::new(move |ctx, _env, _rest_duration| { + comp::timer::Controller::new(move |ctx, _env, _rest_duration| { sound_sender.send(sound::Type::EndNotifier).ok(); ctx.submit_command(cmd::DEINIT_COMP.to(Target::Widget(ctx.widget_id()))); @@ -62,7 +62,7 @@ fn build_notifier_timer( }) .with_duration(env::BREAK_NOTIFIER_TIMER_DURATION), ) - .controller(comp::deinit::DeinitController::default()) + .controller(comp::deinit::Controller::default()) } fn build_postpone_btn(parent_widget_id: WidgetId) -> impl Widget { diff --git a/src/win/rest.rs b/src/win/rest.rs index 55de4ba..1ee6fe4 100644 --- a/src/win/rest.rs +++ b/src/win/rest.rs @@ -19,12 +19,12 @@ pub fn create( let x = (rect.width() - win_width) / 2.0; let y = (rect.height() - win_height) / 2.0; - return WindowDesc::new(move || build(parent_widget_id, rest_duration_secs, sound_sender)) + WindowDesc::new(move || build(parent_widget_id, rest_duration_secs, sound_sender)) .show_titlebar(false) .menu(MenuDesc::empty()) .set_position((x, y)) .with_min_size((win_width, win_height)) - .window_size((win_width, win_height)); + .window_size((win_width, win_height)) } fn build( @@ -52,7 +52,7 @@ fn build_idle_timer( ) -> impl Widget { comp::timer::build() .controller( - comp::timer::TimerController::new(move |ctx, env, _rest_duration| { + comp::timer::Controller::new(move |ctx, env, _rest_duration| { sound_sender.send(sound::Type::EndRest).ok(); ctx.submit_command(cmd::DEINIT_COMP.to(Target::Widget(ctx.widget_id()))); @@ -67,7 +67,7 @@ fn build_idle_timer( .with_duration(rest_duration_secs) .with_init_duration(env::BREAK_NOTIFIER_TIMER_DURATION), ) - .controller(comp::deinit::DeinitController::default()) + .controller(comp::deinit::Controller::default()) } fn build_finish_btn(parent_widget_id: WidgetId) -> impl Widget { diff --git a/src/win/status.rs b/src/win/status.rs index 17971b5..9075fbd 100644 --- a/src/win/status.rs +++ b/src/win/status.rs @@ -30,9 +30,9 @@ fn build_timers(sender: Rc) -> impl Widget { .with_child( comp::break_timer::build( "Micro", - env::MICRO_BREAK_TIMER_DURATION, - env::MICRO_BREAK_TIMER_POSTPONE_DURATION, - env::MICRO_BREAK_TIMER_REST_DURATION, + &env::MICRO_BREAK_TIMER_DURATION, + &env::MICRO_BREAK_TIMER_POSTPONE_DURATION, + &env::MICRO_BREAK_TIMER_REST_DURATION, sender.clone(), ) .lens(state::App::micro_break), @@ -41,10 +41,10 @@ fn build_timers(sender: Rc) -> impl Widget { .with_child( comp::break_timer::build( "Rest", - env::REST_BREAK_TIMER_DURATION, - env::REST_BREAK_TIMER_POSTPONE_DURATION, - env::REST_BREAK_TIMER_REST_DURATION, - sender.clone(), + &env::REST_BREAK_TIMER_DURATION, + &env::REST_BREAK_TIMER_POSTPONE_DURATION, + &env::REST_BREAK_TIMER_REST_DURATION, + sender, ) .lens(state::App::rest_break), ) @@ -57,7 +57,7 @@ fn build_pause_btn() -> impl Widget { .with_child( Button::new("Start").on_click(|ctx, data: &mut state::App, _env| { data.paused = false; - ctx.submit_command(cmd::UNPAUSE_ALL_TIMER_COMP.with(false)) + ctx.submit_command(cmd::UNPAUSE_ALL_TIMER_COMP.with(false)); }), ) .with_default_spacer() @@ -68,7 +68,7 @@ fn build_pause_btn() -> impl Widget { comp::flex::row_sta_sta().with_child(Button::new("Pause").on_click( |ctx, data: &mut state::App, _env| { data.paused = true; - ctx.submit_command(cmd::PAUSE_ALL_TIMER_COMP) + ctx.submit_command(cmd::PAUSE_ALL_TIMER_COMP); }, )), )