Skip to content

Commit

Permalink
chore: fix clippy errors manually
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanleiby committed Nov 11, 2024
1 parent 95cc085 commit e6071c5
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 107 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ jobs:
run: sudo apt install libasound2-dev
- name: Build
run: cargo build --verbose
- name: Run tests
- name: Unit tests
run: cargo test --verbose
- name: Lint
run: cargo clippy --all --all-features --tests -- -D warnings
2 changes: 2 additions & 0 deletions lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
git ls-files | entr cargo clippy --all --all-features --tests -- -D warnings
2 changes: 1 addition & 1 deletion src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl Audio {

/// schedules notes for a single sound to be played between last_scheduled_tick and tick_to_schedule
fn schedule_audio(
notes: &Vec<f64>,
notes: &[f64],
sound: &StaticSoundData,
volume: f64,
manager: &mut AudioManager,
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ impl AppConfig {
}

pub fn save(&self) {
// TODO: We may remove confy. Ignore for now
#[allow(clippy::match_single_binding)]
match confy::store("macroix", "AppConfig", self) {
// ignore failures. these happen in web builds
_ => (),
Expand Down
52 changes: 19 additions & 33 deletions src/egui_ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// mod app;

use egui::{
self,
emath::{self, RectTransform},
Expand Down Expand Up @@ -34,7 +32,9 @@ pub struct UIState {
is_playing: bool,
bpm: f32,
is_metronome_enabled: bool,
#[allow(dead_code)]
volume_metronome: f32,
#[allow(dead_code)]
volume_target_notes: f32,

// audio
Expand Down Expand Up @@ -102,6 +102,8 @@ impl Default for UIState {

impl UIState {
// TODO: rename related to choosing a loop
// TODO: investigate a fix for the clippy error. Changing to a slice threw errors elsewhere in code.
#[allow(clippy::ptr_arg)]
pub fn selector_vec(mut self, selector_vec: &Vec<String>) -> Self {
self.selector_vec = selector_vec.clone();
self
Expand Down Expand Up @@ -135,8 +137,8 @@ impl UIState {
self.latency_offset_s = offset;
}

pub fn set_user_hits(&mut self, hits: &Vec<UserHit>) {
self.user_hits = hits.clone();
pub fn set_user_hits(&mut self, hits: &[UserHit]) {
self.user_hits = hits.to_vec().clone();
}

pub fn set_desired_hits(&mut self, voices: &Voices) {
Expand Down Expand Up @@ -434,7 +436,7 @@ fn is_beat_enabled(
visible_row: usize,
col: usize,
enabled_beats: EnabledBeats,
visible_instruments: &Vec<&Instrument>,
visible_instruments: &[&Instrument],
) -> bool {
// determine instrument
let res = visible_instruments
Expand Down Expand Up @@ -497,8 +499,9 @@ fn draw_beat_grid(ui_state: &UIState, ui: &mut egui::Ui, events: &mut Vec<Events
ui.input(|i| {
for event in &i.raw.events {
if let egui::Event::PointerButton {
pos, pressed: true, ..
} = event {
pos, pressed: true, ..
} = event
{
// check if click is within the beat grid's bounds
if !response.rect.contains(*pos) {
continue;
Expand Down Expand Up @@ -594,8 +597,8 @@ fn draw_beat_grid(ui_state: &UIState, ui: &mut egui::Ui, events: &mut Vec<Events
painter.extend(shapes);

// add instrument names last, so they stay visible
for row in 0..visible_rows {
let name = match visible_instruments[row] {
for (row, item) in visible_instruments.iter().enumerate().take(visible_rows) {
let name = match item {
Instrument::ClosedHihat => "Hi-hat",
Instrument::Snare => "Snare",
Instrument::Kick => "Kick",
Expand Down Expand Up @@ -623,7 +626,7 @@ fn rect_for_col_row(
let base_pos = pos2(col as f32 * width_scale, row as f32 * height_scale);

// TODO: fix scaling to always draw a nicer looking square based grid

to_screen.transform_rect(egui::Rect {
min: base_pos,
max: base_pos + egui::Vec2::new(width_scale * 0.95, height_scale * 0.95),
Expand Down Expand Up @@ -656,7 +659,7 @@ fn draw_user_hits(
to_screen: RectTransform,
shapes: &mut Vec<Shape>,
height_scale: f32,
visible_instruments: &Vec<&Instrument>,
visible_instruments: &[&Instrument],
) {
for (instrument_idx, instrument) in visible_instruments.iter().enumerate() {
let user_notes = get_user_hit_timings_by_instrument(&ui_state.user_hits, **instrument);
Expand All @@ -679,7 +682,7 @@ fn draw_user_hit(
user_beat: f64,
row: usize,
audio_latency_beats: f64,
desired_hits: &Vec<f64>,
desired_hits: &[f64],
to_screen: RectTransform,
shapes: &mut Vec<Shape>,
height_scale: f32,
Expand Down Expand Up @@ -721,16 +724,17 @@ fn draw_user_hit(
shapes.push(shape);
}

#[allow(clippy::too_many_arguments)]
fn draw_note_successes(
user_hits: &Vec<UserHit>,
user_hits: &[UserHit],
desired_hits: &Voices,
audio_latency: f64,
loop_current_beat: f64,
to_screen: RectTransform,
shapes: &mut Vec<Shape>,
width_scale: f32,
height_scale: f32,
visible_instruments: &Vec<&Instrument>,
visible_instruments: &[&Instrument],
) {
for (instrument_idx, instrument) in visible_instruments.iter().enumerate() {
let actual = get_user_hit_timings_by_instrument(user_hits, **instrument);
Expand Down Expand Up @@ -797,11 +801,7 @@ fn gold_mode(ui: &mut egui::Ui, ui_state: &UIState) {
&ui_state.user_hits,
(ui_state.current_loop as i32 - i) as usize, // TODO: check for overflow
);
let summary_data = compute_last_loop_summary(
&nth_loop_hits,
&ui_state.desired_hits,
ui_state.get_audio_latency_in_beats() as f64,
);
let summary_data = compute_last_loop_summary(&nth_loop_hits, &ui_state.desired_hits);

// Simpler than chart.. TODO: support for colored emoji
// 🔴
Expand Down Expand Up @@ -838,17 +838,3 @@ fn gold_mode(ui: &mut egui::Ui, ui_state: &UIState) {
plot_ui.line(line);
});
}

fn powered_by_egui_and_eframe(ui: &mut egui::Ui) {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.label("Powered by ");
ui.hyperlink_to("egui", "https://github.com/emilk/egui");
ui.label(" and ");
ui.hyperlink_to(
"eframe",
"https://github.com/emilk/egui/tree/master/crates/eframe",
);
ui.label(".");
});
}
4 changes: 2 additions & 2 deletions src/fps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::collections::VecDeque;

use macroquad::prelude::*;

pub struct FPS {
pub struct Fps {
fps_tracker: VecDeque<i32>,
last_fps: i32,
last_updated_fps_timestamp: f64,
}

impl FPS {
impl Fps {
pub fn new() -> Self {
Self {
fps_tracker: VecDeque::<i32>::with_capacity(10),
Expand Down
77 changes: 36 additions & 41 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,52 +135,46 @@ pub fn process_system_events(
gold_mode: &mut GoldMode,
) {
// read events
loop {
match rx.try_recv() {
Ok(msg) => {
info!("[system event] {:?}", msg);
match msg {
TxMsg::AudioNew => (),
TxMsg::StartingLoop(loop_num) => {
let last_loop_hits = get_hits_from_nth_loop(
&audio.user_hits,
(audio.current_loop() - 1) as usize,
);
let audio_latency = audio.get_configured_audio_latency_seconds();
let summary_data =
compute_last_loop_summary(&last_loop_hits, voices, audio_latency);
info!("last loop summary = {:?}", summary_data);
let totals = summary_data.total();

if loop_num > 0 {
// Log user metric to a file, for eventual data analysis
let user_metric = UserMetric {
system_time_ms: current_time_millis(),
bpm: audio.get_bpm(),
score: totals.score(),
};
let log_result = log_user_metric(&user_metric);
if let Err(e) = log_result { println!("error logging user_metric. error was: {e}") }
}
while let Ok(msg) = rx.try_recv() {
info!("[system event] {:?}", msg);
match msg {
TxMsg::AudioNew => (),
TxMsg::StartingLoop(loop_num) => {
let last_loop_hits =
get_hits_from_nth_loop(&audio.user_hits, (audio.current_loop() - 1) as usize);
let summary_data = compute_last_loop_summary(&last_loop_hits, voices);
info!("last loop summary = {:?}", summary_data);
let totals = summary_data.total();

gold_mode.was_gold = false;
if totals.score() == 1. {
gold_mode.correct_takes += 1;
} else {
gold_mode.correct_takes = 0;
}

if gold_mode.correct_takes == GOLD_MODE_CORRECT_TAKES {
audio.set_bpm(audio.get_bpm() + GOLD_MODE_BPM_STEP);
gold_mode.correct_takes = 0;
gold_mode.was_gold = true;
// TODO: schedule a 1-off "success!" SFX to play
// TOOD: Maybe -- clear existing noise from mistaken notes
}
if loop_num > 0 {
// Log user metric to a file, for eventual data analysis
let user_metric = UserMetric {
system_time_ms: current_time_millis(),
bpm: audio.get_bpm(),
score: totals.score(),
};
let log_result = log_user_metric(&user_metric);
if let Err(e) = log_result {
println!("error logging user_metric. error was: {e}")
}
}

gold_mode.was_gold = false;
if totals.score() == 1. {
gold_mode.correct_takes += 1;
} else {
gold_mode.correct_takes = 0;
}

if gold_mode.correct_takes == GOLD_MODE_CORRECT_TAKES {
audio.set_bpm(audio.get_bpm() + GOLD_MODE_BPM_STEP);
gold_mode.correct_takes = 0;
gold_mode.was_gold = true;
// TODO: schedule a 1-off "success!" SFX to play
// TOOD: Maybe -- clear existing noise from mistaken notes
}
}
Err(_) => break,
}
}
}
Expand All @@ -199,6 +193,7 @@ fn log_user_metric(user_metric: &UserMetric) -> Result<(), Box<dyn Error>> {
}

/// update application state based on events (that came from user input)
#[allow(clippy::too_many_arguments)]
pub fn process_user_events(
voices: &mut Voices,
audio: &mut Audio,
Expand Down
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::error::Error;
use std::sync::mpsc::{self};

use crate::config::AppConfig;
use crate::fps::FPS;
use crate::fps::Fps;
use crate::ui::*;

use audio::Audio;
Expand Down Expand Up @@ -118,7 +118,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
audio.initialize().await?;

// debug
let mut fps_tracker = FPS::new();
let mut fps_tracker = Fps::new();

let mut ui = UI::new();
loop {
Expand Down Expand Up @@ -162,7 +162,6 @@ async fn main() -> Result<(), Box<dyn Error>> {

fn process_cli_args() -> String {
// read commnand line arg as directory name


std::env::args()
.nth(1)
Expand Down
2 changes: 1 addition & 1 deletion src/midi_input_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn get_midi_as_user_hits(midi_input: &MidiInput) -> Vec<UserHit> {
};

let ic_midi = match midi_input.get_device_name() {
s if s == "MPK Mini Mk II" => mpk_mini_mk_ii,
"MPK Mini Mk II" => mpk_mini_mk_ii,
s if s.contains("TD-17") => td17,
s if s.contains("TD-27") => td27,
s if s.contains("Nitro") => alesis_nitro,
Expand Down
Loading

0 comments on commit e6071c5

Please sign in to comment.