Skip to content

Commit

Permalink
Merge pull request #123 from schneiderfelipe/migrate-to-clap4
Browse files Browse the repository at this point in the history
Migrate to clap 4.x (WIP)
  • Loading branch information
noeddl authored May 15, 2024
2 parents 6867ab7 + b427f33 commit 563a22c
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 327 deletions.
297 changes: 181 additions & 116 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ rust-version = "1.74"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = {version = "3.2", features = ["derive"]}
clap = {version = "4.3", features = ["derive"] }
itertools = "0.10"
lazy_static = "1.4"
petgraph = "0.6"
thiserror = "1.0.58"

[dev-dependencies]
assert_cmd = "2.0"
Expand Down
91 changes: 42 additions & 49 deletions src/chord.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
use std::convert::TryFrom;
use std::error::Error;
use std::fmt;
use std::ops::{Add, Sub};
use std::str::FromStr;

use itertools::Itertools;

use crate::{
ChordType, Note, PitchClass, Semitones, UkeString, Voicing, VoicingConfig, STRING_COUNT,
ChordType, NoMatchingChordTypeFoundError, Note, PitchClass, Semitones, UkeString, Voicing,
VoicingConfig, STRING_COUNT,
};

/// Custom error for strings that cannot be parsed into chords.
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
#[error("could not parse chord name '{name}'")]
pub struct ParseChordError {
name: String,
}

impl Error for ParseChordError {}

impl fmt::Display for ParseChordError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Could not parse chord name \"{}\"", self.name)
}
}

/// A chord such as C, Cm and so on.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Chord {
Expand Down Expand Up @@ -81,7 +74,7 @@ impl Chord {
.sorted()
}

pub fn transpose(&self, semitones: i8) -> Chord {
pub fn transpose(&self, semitones: i8) -> Self {
match semitones {
s if s < 0 => self.clone() - semitones.unsigned_abs() as Semitones,
_ => self.clone() + semitones as Semitones,
Expand All @@ -92,7 +85,7 @@ impl Chord {
impl fmt::Display for Chord {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = format!("{}{}", self.root, self.chord_type.to_symbol());
write!(f, "{} - {} {}", name, self.root, self.chord_type)
write!(f, "{name} - {} {}", self.root, self.chord_type)
}
}

Expand Down Expand Up @@ -122,7 +115,7 @@ impl FromStr for Chord {
}

impl TryFrom<&[PitchClass]> for Chord {
type Error = &'static str;
type Error = NoMatchingChordTypeFoundError;

/// Determine the chord that is represented by a list of pitch classes.
fn try_from(pitches: &[PitchClass]) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -167,7 +160,7 @@ mod tests {
case("CmMaj7b5")
)]
fn test_from_str_fail(chord: &str) {
assert!(Chord::from_str(chord).is_err())
assert!(Chord::from_str(chord).is_err());
}

#[rstest(
Expand Down Expand Up @@ -200,7 +193,7 @@ mod tests {
third: Note,
fifth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth]);
assert_eq!(chord.chord_type, ChordType::Major);
Expand Down Expand Up @@ -238,7 +231,7 @@ mod tests {
fifth: Note,
seventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh]);
assert_eq!(chord.chord_type, ChordType::MajorSeventh);
Expand Down Expand Up @@ -278,7 +271,7 @@ mod tests {
seventh: Note,
ninth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh, ninth]);
assert_eq!(chord.chord_type, ChordType::MajorNinth);
Expand Down Expand Up @@ -320,7 +313,7 @@ mod tests {
ninth: Note,
eleventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(
chord.notes,
Expand Down Expand Up @@ -367,7 +360,7 @@ mod tests {
eleventh: Note,
thirteenth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(
chord.notes,
Expand Down Expand Up @@ -408,7 +401,7 @@ mod tests {
fifth: Note,
sixth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, sixth]);
assert_eq!(chord.chord_type, ChordType::MajorSixth);
Expand Down Expand Up @@ -448,7 +441,7 @@ mod tests {
sixth: Note,
ninth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, sixth, ninth]);
assert_eq!(chord.chord_type, ChordType::SixthNinth);
Expand Down Expand Up @@ -486,7 +479,7 @@ mod tests {
fifth: Note,
seventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh]);
assert_eq!(chord.chord_type, ChordType::DominantSeventh);
Expand Down Expand Up @@ -526,7 +519,7 @@ mod tests {
seventh: Note,
ninth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh, ninth]);
assert_eq!(chord.chord_type, ChordType::DominantNinth);
Expand Down Expand Up @@ -568,7 +561,7 @@ mod tests {
ninth: Note,
eleventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(
chord.notes,
Expand Down Expand Up @@ -615,7 +608,7 @@ mod tests {
eleventh: Note,
thirteenth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(
chord.notes,
Expand Down Expand Up @@ -658,7 +651,7 @@ mod tests {
seventh: Note,
ninth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh, ninth]);
assert_eq!(chord.chord_type, ChordType::DominantSeventhFlatNinth);
Expand Down Expand Up @@ -698,7 +691,7 @@ mod tests {
seventh: Note,
ninth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh, ninth]);
assert_eq!(chord.chord_type, ChordType::DominantSeventhSharpNinth);
Expand Down Expand Up @@ -736,7 +729,7 @@ mod tests {
fifth: Note,
seventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh]);
assert_eq!(chord.chord_type, ChordType::DominantSeventhFlatFifth);
Expand Down Expand Up @@ -772,7 +765,7 @@ mod tests {
fourth: Note,
fifth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, fourth, fifth]);
assert_eq!(chord.chord_type, ChordType::SuspendedFourth);
Expand Down Expand Up @@ -808,7 +801,7 @@ mod tests {
second: Note,
fifth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, second, fifth]);
assert_eq!(chord.chord_type, ChordType::SuspendedSecond);
Expand Down Expand Up @@ -846,7 +839,7 @@ mod tests {
fifth: Note,
seventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, fourth, fifth, seventh]);
assert_eq!(chord.chord_type, ChordType::DominantSeventhSuspendedFourth);
Expand Down Expand Up @@ -884,7 +877,7 @@ mod tests {
fifth: Note,
seventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, second, fifth, seventh]);
assert_eq!(chord.chord_type, ChordType::DominantSeventhSuspendedSecond);
Expand Down Expand Up @@ -920,7 +913,7 @@ mod tests {
third: Note,
fifth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth]);
assert_eq!(chord.chord_type, ChordType::Minor);
Expand Down Expand Up @@ -958,7 +951,7 @@ mod tests {
fifth: Note,
seventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh]);
assert_eq!(chord.chord_type, ChordType::MinorSeventh);
Expand Down Expand Up @@ -996,7 +989,7 @@ mod tests {
fifth: Note,
seventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh]);
assert_eq!(chord.chord_type, ChordType::MinorMajorSeventh);
Expand Down Expand Up @@ -1034,7 +1027,7 @@ mod tests {
fifth: Note,
sixth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, sixth]);
assert_eq!(chord.chord_type, ChordType::MinorSixth);
Expand Down Expand Up @@ -1074,7 +1067,7 @@ mod tests {
seventh: Note,
ninth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh, ninth]);
assert_eq!(chord.chord_type, ChordType::MinorNinth);
Expand Down Expand Up @@ -1116,7 +1109,7 @@ mod tests {
ninth: Note,
eleventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(
chord.notes,
Expand Down Expand Up @@ -1163,7 +1156,7 @@ mod tests {
eleventh: Note,
thirteenth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(
chord.notes,
Expand Down Expand Up @@ -1202,7 +1195,7 @@ mod tests {
third: Note,
fifth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth]);
assert_eq!(chord.chord_type, ChordType::Diminished);
Expand Down Expand Up @@ -1240,7 +1233,7 @@ mod tests {
fifth: Note,
seventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh]);
assert_eq!(chord.chord_type, ChordType::DiminishedSeventh);
Expand Down Expand Up @@ -1278,7 +1271,7 @@ mod tests {
fifth: Note,
seventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh]);
assert_eq!(chord.chord_type, ChordType::HalfDiminishedSeventh);
Expand Down Expand Up @@ -1312,7 +1305,7 @@ mod tests {
root: Note,
fifth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, fifth]);
assert_eq!(chord.chord_type, ChordType::Fifth);
Expand Down Expand Up @@ -1348,7 +1341,7 @@ mod tests {
third: Note,
fifth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth]);
assert_eq!(chord.chord_type, ChordType::Augmented);
Expand Down Expand Up @@ -1386,7 +1379,7 @@ mod tests {
fifth: Note,
seventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh]);
assert_eq!(chord.chord_type, ChordType::AugmentedSeventh);
Expand Down Expand Up @@ -1424,7 +1417,7 @@ mod tests {
fifth: Note,
seventh: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, seventh]);
assert_eq!(chord.chord_type, ChordType::AugmentedMajorSeventh);
Expand Down Expand Up @@ -1462,7 +1455,7 @@ mod tests {
fifth: Note,
ninth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fifth, ninth]);
assert_eq!(chord.chord_type, ChordType::AddedNinth);
Expand Down Expand Up @@ -1500,7 +1493,7 @@ mod tests {
fourth: Note,
fifth: Note,
) {
let chord = Chord::from_str(&format!("{}{}", chord_base, chord_suffix)).unwrap();
let chord = Chord::from_str(&format!("{chord_base}{chord_suffix}")).unwrap();

assert_eq!(chord.notes, vec![root, third, fourth, fifth]);
assert_eq!(chord.chord_type, ChordType::AddedFourth);
Expand Down
Loading

0 comments on commit 563a22c

Please sign in to comment.