Skip to content

Commit c351251

Browse files
authored
Add types for more clearity (#25)
* Add types Semitones and StaffSteps * Replace type Frets with FretID * Use newtype pattern for FretID * Revert "Use newtype pattern for FretID" This reverts commit 72596f0. * Define chart width in semitones * Move type definitions * Update documentation
1 parent 50609a9 commit c351251

File tree

13 files changed

+87
-71
lines changed

13 files changed

+87
-71
lines changed

src/chord/chord.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::chord::ChordShapeSet;
2+
use crate::chord::FretID;
23
use crate::diagram::ChordDiagram;
34
use crate::note::Interval;
45
use crate::note::Note;
5-
use crate::Frets;
66
use crate::STRING_COUNT;
77
use regex::Regex;
88
use std::fmt;
@@ -63,7 +63,7 @@ impl Chord {
6363
self.notes.contains(&note)
6464
}
6565

66-
pub fn get_diagram(self, min_fret: Frets) -> ChordDiagram {
66+
pub fn get_diagram(self, min_fret: FretID) -> ChordDiagram {
6767
let chord_shapes = ChordShapeSet::new(self.quality);
6868

6969
let (frets, intervals) = chord_shapes.get_config(self.root, min_fret);

src/chord/chord_shape.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use crate::chord::ChordQuality;
2+
use crate::chord::FretID;
3+
use crate::chord::FretPattern;
24
use crate::note::Interval;
35
use crate::note::Note;
4-
use crate::FretPattern;
5-
use crate::Frets;
6-
use crate::IntervalPattern;
6+
use crate::note::Semitones;
77
use crate::STRING_COUNT;
88
use std::str::FromStr;
99

10+
type IntervalPattern = [Interval; STRING_COUNT];
11+
1012
/// A chord shape is a configuration of frets to be pressed to play a
1113
/// chord with a certain chord quality. The shape can be moved along
1214
/// the fretboard to derive several chords.
@@ -38,7 +40,7 @@ impl ChordShape {
3840

3941
/// Apply the chord shape while moving it `n` frets forward on the fretboard.
4042
/// Return the resulting fret pattern.
41-
fn apply(self, n: Frets) -> (FretPattern, IntervalPattern) {
43+
fn apply(self, n: Semitones) -> (FretPattern, IntervalPattern) {
4244
let mut frets = self.frets;
4345

4446
for f in &mut frets[..] {
@@ -78,7 +80,7 @@ impl ChordShapeSet {
7880
}
7981

8082
/// Return a fret pattern to play `chord` starting from fret number `min_fret`.
81-
pub fn get_config(self, root: Note, min_fret: Frets) -> (FretPattern, IntervalPattern) {
83+
pub fn get_config(self, root: Note, min_fret: FretID) -> (FretPattern, IntervalPattern) {
8284
let (chord_shape, diff) = self
8385
.chord_shapes
8486
.into_iter()

src/chord/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,13 @@ pub use self::chord::Chord;
66
pub use self::chord::ChordQuality;
77
pub use self::chord_shape::ChordShape;
88
pub use self::chord_shape::ChordShapeSet;
9+
10+
use crate::STRING_COUNT;
11+
12+
/// The ID of a fret on the fretboard. 0 corresponds to the nut,
13+
/// 1 corresponds to the first fret, 2 to the second etc.
14+
pub type FretID = u8;
15+
16+
/// A pattern of frets to push down for playing a chord.
17+
/// Each index of the array corresponds to a ukulele string.
18+
pub type FretPattern = [FretID; STRING_COUNT];

src/diagram/chord_diagram.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use crate::chord::Chord;
2+
use crate::chord::FretID;
3+
use crate::chord::FretPattern;
24
use crate::diagram::StringDiagram;
35
use crate::diagram::CHART_WIDTH;
46
use crate::note::Note;
5-
use crate::FretPattern;
6-
use crate::Frets;
7-
use crate::NotePattern;
87
use crate::STRING_COUNT;
98
use std::fmt;
109
use std::str::FromStr;
1110

11+
type NotePattern = [Note; STRING_COUNT];
12+
1213
pub struct ChordDiagram {
1314
chord: Chord,
1415
roots: NotePattern,
@@ -38,7 +39,7 @@ impl ChordDiagram {
3839
/// If the rightmost fret fits on the diagram, show the fretboard
3940
/// beginning at the first fret, otherwise use the leftmost fret
4041
/// needed for the chords to be played.
41-
fn get_base_fret(&self) -> Frets {
42+
fn get_base_fret(&self) -> FretID {
4243
let max_fret = *self.frets.iter().max().unwrap();
4344

4445
match max_fret {
@@ -169,7 +170,7 @@ mod tests {
169170
")
170171
),
171172
)]
172-
fn test_to_diagram(chord_name: &str, min_fret: Frets, diagram: &str) {
173+
fn test_to_diagram(chord_name: &str, min_fret: FretID, diagram: &str) {
173174
let chord = Chord::from_str(chord_name).unwrap();
174175
let chord_diagram = chord.get_diagram(min_fret);
175176
assert_eq!(chord_diagram.to_string(), diagram);

src/diagram/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod string_diagram;
44
pub use self::chord_diagram::ChordDiagram;
55
pub use self::string_diagram::StringDiagram;
66

7-
use crate::Frets;
7+
use crate::note::Semitones;
88

99
/// Number of frets shown on the fretboard chart.
10-
pub const CHART_WIDTH: Frets = 4;
10+
pub const CHART_WIDTH: Semitones = 4;

src/diagram/string_diagram.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
use crate::chord::FretID;
12
use crate::diagram::CHART_WIDTH;
23
use crate::note::Note;
3-
use crate::Frets;
44
use std::fmt;
55

66
/// A line within a chord diagram which represents a string of a ukulele.
77
pub struct StringDiagram {
88
root: Note,
9-
base_fret: Frets,
10-
fret: Frets,
9+
base_fret: FretID,
10+
fret: FretID,
1111
note: Note,
1212
}
1313

1414
impl StringDiagram {
15-
pub fn new(root: Note, base_fret: Frets, fret: Frets, note: Note) -> Self {
15+
pub fn new(root: Note, base_fret: FretID, fret: FretID, note: Note) -> Self {
1616
Self {
1717
root,
1818
base_fret,
@@ -77,8 +77,8 @@ mod tests {
7777
)]
7878
fn test_format_line(
7979
root_name: &str,
80-
base_fret: Frets,
81-
fret: Frets,
80+
base_fret: FretID,
81+
fret: FretID,
8282
note_name: &str,
8383
diagram: &str,
8484
) {

src/lib.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
pub mod chord;
22
mod diagram;
3-
mod note;
4-
5-
/// Type for the number of frets (corresponds to the number of semitones)
6-
/// to move from one note or pitch class to another.
7-
pub type Frets = u8;
3+
pub mod note;
84

95
/// Number of strings on our string instrument.
106
pub const STRING_COUNT: usize = 4;
11-
12-
pub type FretPattern = [Frets; STRING_COUNT];
13-
14-
use crate::note::Interval;
15-
pub type IntervalPattern = [Interval; STRING_COUNT];
16-
17-
use crate::note::Note;
18-
pub type NotePattern = [Note; STRING_COUNT];

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use structopt::StructOpt;
22
use ukebox::chord::Chord;
3-
use ukebox::Frets;
3+
use ukebox::chord::FretID;
44

55
#[derive(StructOpt)]
66
struct Cmd {
77
#[structopt(short = "f", long, default_value = "0")]
88
/// Minimal fret (= minimal position) from which to play <chord>
9-
min_fret: Frets,
9+
min_fret: FretID,
1010
/// Name of the chord to be shown
1111
chord: Chord,
1212
}

src/note/interval.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::Frets;
1+
use crate::note::Semitones;
2+
use crate::note::StaffSteps;
23
use std::fmt;
34
use std::str::FromStr;
45

@@ -26,7 +27,7 @@ pub enum Interval {
2627

2728
impl Interval {
2829
/// Return the number of semitones that the interval encompasses.
29-
pub fn to_semitones(self) -> Frets {
30+
pub fn to_semitones(self) -> Semitones {
3031
use Interval::*;
3132

3233
match self {
@@ -39,7 +40,7 @@ impl Interval {
3940

4041
/// Return the interval's number. It corresponds to the number of
4142
/// staff positions that the intervall encompasses.
42-
pub fn to_number(self) -> u8 {
43+
pub fn to_number(self) -> StaffSteps {
4344
use Interval::*;
4445

4546
match self {

src/note/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ pub use self::interval::Interval;
88
pub use self::note::Note;
99
pub use self::pitch_class::PitchClass;
1010
pub use self::staff_position::StaffPosition;
11+
12+
/// The number of semitones (corresponds to the number of frets)
13+
/// to move from one note or pitch class to another.
14+
pub type Semitones = u8;
15+
16+
/// The number of steps in a staff to move from one staff position
17+
/// to another.
18+
type StaffSteps = u8;

0 commit comments

Comments
 (0)