Skip to content

Commit

Permalink
Merge pull request #9 from barafael/clippy
Browse files Browse the repository at this point in the history
Clippy Lint Fixes
  • Loading branch information
twitchax authored May 30, 2023
2 parents 36eb2cf + 7affa60 commit 23df53c
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/analyze/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn get_notes_from_audio_data(data: &[f32], length_in_seconds: u8) -> Res<Vec

let num_nan = data.iter().filter(|n| n.is_nan()).count();
if num_nan > 0 {
return Err(anyhow::Error::msg(format!("{} NaNs in audio data.", num_nan)));
return Err(anyhow::Error::msg(format!("{num_nan} NaNs in audio data.")));
}

let frequency_space = get_frequency_space(data, length_in_seconds);
Expand Down Expand Up @@ -229,7 +229,7 @@ fn get_likely_notes_from_peak_space(peak_space: &[(f32, f32)], cutoff: f32) -> V

let mut candidates = HashMap::new();

for (frequency, magnitude) in peak_space.iter() {
for (frequency, magnitude) in &peak_space {
if let Some(pair) = binary_search_closest(ALL_PITCH_NOTES_WITH_FREQUENCY.deref(), *frequency, |t| t.1) {
let note = pair.0;
let entry = candidates.entry(note).or_insert(*magnitude);
Expand Down
8 changes: 4 additions & 4 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,14 @@ fn start(args: Args) -> Void {

let chord = Chord::parse(parts.next().unwrap()).unwrap();

let length = parts.next().map(|l| l.parse::<u16>().unwrap()).unwrap_or(32);
let length = parts.next().map_or(32, |l| l.parse::<u16>().unwrap());

(chord, length)
})
.collect::<Vec<_>>();

loop {
for (chord, length) in chord_pairs.iter() {
for (chord, length) in &chord_pairs {
let length = (*length as f32) * 60f32 / bpm / 8f32;
play(chord, 0.0, length, 0.1)?;
}
Expand Down Expand Up @@ -596,7 +596,7 @@ fn start(args: Args) -> Void {
}

fn describe(chord: &Chord) {
println!("{}", chord);
println!("{chord}");
}

fn play(chord: &Chord, delay: f32, length: f32, fade_in: f32) -> Void {
Expand All @@ -615,7 +615,7 @@ fn play(chord: &Chord, delay: f32, length: f32, fade_in: f32) -> Void {
}

fn show_notes_and_chords(notes: &[Note]) -> Res<()> {
println!("Notes: {}", notes.iter().map(|n| n.to_string()).collect::<Vec<_>>().join(" "));
println!("Notes: {}", notes.iter().map(ToString::to_string).collect::<Vec<_>>().join(" "));

let candidates = Chord::try_from_notes(notes)?;

Expand Down
12 changes: 6 additions & 6 deletions src/core/chord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl Chord {
}

// Remove extensions and modifiers that are expressed elsewhere in the chord.
result.iter_mut().for_each(|c| {
for c in &mut result {
let dominant_degree = c.dominant_degree();

if let Some(degree) = dominant_degree {
Expand All @@ -425,7 +425,7 @@ impl Chord {
c.extensions.remove(&Extension::Add11);
c.extensions.remove(&Extension::Add13);
}
_ => {}
Degree::Seven => {}
}
}

Expand All @@ -434,7 +434,7 @@ impl Chord {
c.modifiers.remove(&Modifier::Flat5);
c.modifiers.remove(&Modifier::Augmented5);
}
});
}

// Order the candidates by "simplicity" (i.e., least slashes, least extensions, least modifiers, and least inversion).
result.sort();
Expand Down Expand Up @@ -480,7 +480,7 @@ impl HasName for Chord {

// Add extensions.
if !self.extensions.is_empty() {
for e in self.extensions.iter() {
for e in &self.extensions {
name.push_str(&format!("({})", e.static_name()));
}
}
Expand Down Expand Up @@ -559,8 +559,8 @@ impl HasIsCrunchy for Chord {

impl Display for Chord {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let scale = self.scale().iter().map(|n| n.static_name()).collect::<Vec<_>>().join(", ");
let chord = self.chord().iter().map(|n| n.static_name()).collect::<Vec<_>>().join(", ");
let scale = self.scale().iter().map(HasStaticName::static_name).collect::<Vec<_>>().join(", ");
let chord = self.chord().iter().map(HasStaticName::static_name).collect::<Vec<_>>().join(", ");

write!(f, "{}\n {}\n {}\n {}", self.precise_name(), self.description(), scale, chord)
}
Expand Down
4 changes: 1 addition & 3 deletions src/core/named_pitch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,7 @@ impl Add<i8> for NamedPitch {

let new_index = index as i8 + rhs;

if !(0..=49).contains(&new_index) {
panic!("NamedPitch out of range.");
}
assert!((0..=49).contains(&new_index), "NamedPitch out of range.");

ALL_PITCHES[new_index as usize]
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ impl Sub for Note {
}
}

panic!("{} - {} is not a valid interval", high, low);
panic!("{high} - {low} is not a valid interval");
}
}

Expand Down
17 changes: 4 additions & 13 deletions src/core/octave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub trait HasOctave {
// Enum.

/// An enum representing the octave of a note.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Ord, PartialOrd)]
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Default, Ord, PartialOrd)]
#[repr(u8)]
pub enum Octave {
/// The octave 0.
Expand All @@ -29,6 +29,7 @@ pub enum Octave {
/// The octave 3.
Three,
/// The octave 4.
#[default]
Four,
/// The octave 5.
Five,
Expand Down Expand Up @@ -86,9 +87,7 @@ impl Add for Octave {
fn add(self, rhs: Self) -> Self::Output {
let new_octave = self as u8 + rhs as u8;

if new_octave > 15 {
panic!("Octave overflow.");
}
assert!(new_octave <= 15, "Octave overflow");

// SAFETY: The new octave is guaranteed to be less than or equal to 15.
unsafe { std::mem::transmute(new_octave) }
Expand All @@ -101,9 +100,7 @@ impl Sub for Octave {
fn sub(self, rhs: Self) -> Self::Output {
let new_octave = (self as u8).checked_sub(rhs as u8).expect("Octave underflow.");

if new_octave > 15 {
panic!("Octave overflow.");
}
assert!(new_octave <= 15, "Octave overflow");

// SAFETY: The new octave is guaranteed to be less than or equal to 15.
unsafe { std::mem::transmute(new_octave) }
Expand Down Expand Up @@ -172,12 +169,6 @@ impl HasOctave for Octave {
}
}

impl Default for Octave {
fn default() -> Self {
Octave::Four
}
}

// Statics.

/// An array of all octaves.
Expand Down
84 changes: 28 additions & 56 deletions src/core/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,68 +22,40 @@ pub struct ChordParser;
pub fn note_str_to_note(note_str: &str) -> Res<Note> {
let chord = match note_str {
"A" => note::A,
"A#" => note::ASharp,
"A♯" => note::ASharp,
"A##" => note::ADoubleSharp,
"A𝄪" => note::ADoubleSharp,
"Ab" => note::AFlat,
"A♭" => note::AFlat,
"Abb" => note::ADoubleFlat,
"A𝄫" => note::ADoubleFlat,
"A#" | "A♯" => note::ASharp,
"A##" | "A𝄪" => note::ADoubleSharp,
"Ab" | "A♭" => note::AFlat,
"Abb" | "A𝄫" => note::ADoubleFlat,
"B" => note::B,
"B#" => note::BSharp,
"B♯" => note::BSharp,
"B##" => note::BDoubleSharp,
"B𝄪" => note::BDoubleSharp,
"Bb" => note::BFlat,
"B♭" => note::BFlat,
"Bbb" => note::BDoubleFlat,
"B𝄫" => note::BDoubleFlat,
"B#" | "B♯" => note::BSharp,
"B##" | "B𝄪" => note::BDoubleSharp,
"Bb" | "B♭" => note::BFlat,
"Bbb" | "B𝄫" => note::BDoubleFlat,
"C" => note::C,
"C#" => note::CSharp,
"C♯" => note::CSharp,
"C##" => note::CDoubleSharp,
"C𝄪" => note::CDoubleSharp,
"Cb" => note::CFlat,
"C♭" => note::CFlat,
"Cbb" => note::CDoubleFlat,
"C𝄫" => note::CDoubleFlat,
"C#" | "C♯" => note::CSharp,
"C##" | "C𝄪" => note::CDoubleSharp,
"Cb" | "C♭" => note::CFlat,
"Cbb" | "C𝄫" => note::CDoubleFlat,
"D" => note::D,
"D#" => note::DSharp,
"D♯" => note::DSharp,
"D##" => note::DDoubleSharp,
"D𝄪" => note::DDoubleSharp,
"Db" => note::DFlat,
"D♭" => note::DFlat,
"Dbb" => note::DDoubleFlat,
"D𝄫" => note::DDoubleFlat,
"D#" | "D♯" => note::DSharp,
"D##" | "D𝄪" => note::DDoubleSharp,
"Db" | "D♭" => note::DFlat,
"Dbb" | "D𝄫" => note::DDoubleFlat,
"E" => note::E,
"E#" => note::ESharp,
"E♯" => note::ESharp,
"E##" => note::EDoubleSharp,
"E𝄪" => note::EDoubleSharp,
"Eb" => note::EFlat,
"E♭" => note::EFlat,
"Ebb" => note::EDoubleFlat,
"E𝄫" => note::EDoubleFlat,
"E#" | "E♯" => note::ESharp,
"E##" | "E𝄪" => note::EDoubleSharp,
"Eb" | "E♭" => note::EFlat,
"Ebb" | "E𝄫" => note::EDoubleFlat,
"F" => note::F,
"F#" => note::FSharp,
"F♯" => note::FSharp,
"F##" => note::FDoubleSharp,
"F𝄪" => note::FDoubleSharp,
"Fb" => note::FFlat,
"F♭" => note::FFlat,
"Fbb" => note::FDoubleFlat,
"F𝄫" => note::FDoubleFlat,
"F#" | "F♯" => note::FSharp,
"F##" | "F𝄪" => note::FDoubleSharp,
"Fb" | "F♭" => note::FFlat,
"Fbb" | "F𝄫" => note::FDoubleFlat,
"G" => note::G,
"G#" => note::GSharp,
"G♯" => note::GSharp,
"G##" => note::GDoubleSharp,
"G𝄪" => note::GDoubleSharp,
"Gb" => note::GFlat,
"G♭" => note::GFlat,
"Gbb" => note::GDoubleFlat,
"G𝄫" => note::GDoubleFlat,
"G#" | "G♯" => note::GSharp,
"G##" | "G𝄪" => note::GDoubleSharp,
"Gb" | "G♭" => note::GFlat,
"Gbb" | "G𝄫" => note::GDoubleFlat,
_ => return Err(crate::core::base::Err::msg("Please use fairly standard notes (e.g., don't use triple sharps / flats).")),
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
pub mod core;
pub mod helpers;

#[cfg(any(feature = "analyze_base"))]
#[cfg(feature = "analyze_base")]
pub mod analyze;

#[cfg(feature = "ml_base")]
Expand Down

0 comments on commit 23df53c

Please sign in to comment.