1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use crate::base::HasStaticName;

// Traits.

/// A trait for types that have a static name.
pub trait HasIsDominant {
    /// Returns whether the type (usually the modifier enum) is dominant.
    fn is_dominant(&self) -> bool;
}

// Enum.

/// An enum representing the degree of a dominant chord.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Ord, PartialOrd)]
#[repr(u8)]
pub enum Degree {
    /// Seventh degree.
    Seven,
    /// Ninth degree.
    Nine,
    /// Eleventh degree.
    Eleven,
    /// Thirteenth degree.
    Thirteen,
}

/// An enum representing the modifier of a chord.
/// 
/// Modifiers are "special extensions" that essentially have the capacity to _change_ 
/// how the chord is interpreted by the system.  E.g., a dominant flat 9 chord is not
/// _just_ a dominant chord with a flat 9 extension, but rather a chord that is
/// represented by an entirely specific scale (half/whole/half diminished).
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Ord, PartialOrd)]
#[repr(u8)]
pub enum Modifier {
    /// Minor modifier.
    Minor,

    /// Flat 5 modifier.
    Flat5,
    /// Sharp 5 modifier.
    Augmented5,

    /// Major 7 modifier.
    Major7,
    /// Dominant modifier with degree.
    Dominant(Degree),

    // Flat 9 modifier.
    Flat9,
    // Sharp 9 modifier.
    Sharp9,

    // Sharp 11 modifier.
    Sharp11,

    // Diminished modifier.
    Diminished,
}

/// An enum representing the extension of a chord.
/// 
/// Extensions are not really "special" in the sense that they do not change how the
/// chord is interpreted by the system.  E.g., an `add2` just adds a 2 to the chord,
/// and the chord is still interpreted as a major chord.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Ord, PartialOrd)]
#[repr(u8)]
pub enum Extension {
    Sus2,
    Sus4,

    Flat11,

    Flat13,
    Sharp13,

    Add2,
    Add4,
    Add6,

    Add9,
    Add11,
    Add13,
}

// Impls.

impl HasIsDominant for Modifier {
    fn is_dominant(&self) -> bool {
        matches!(self, Modifier::Dominant(_))
    }
}

impl HasStaticName for Degree {
    #[no_coverage]
    fn static_name(&self) -> &'static str {
        match self {
            Degree::Seven => "7",
            Degree::Nine => "9",
            Degree::Eleven => "11",
            Degree::Thirteen => "13",
        }
    }
}

impl HasStaticName for Modifier {
    #[no_coverage]
    fn static_name(&self) -> &'static str {
        match self {
            Modifier::Minor => "m",

            Modifier::Flat5 => "♭5",
            Modifier::Augmented5 => "+",

            Modifier::Major7 => "maj7",
            Modifier::Dominant(dominant) => dominant.static_name(),

            Modifier::Flat9 => "♭9",
            Modifier::Sharp9 => "♯9",

            Modifier::Sharp11 => "♯11",

            Modifier::Diminished => "°",
        }
    }
}

impl HasStaticName for Extension {
    #[no_coverage]
    fn static_name(&self) -> &'static str {
        match self {
            Extension::Sus2 => "sus2",
            Extension::Sus4 => "sus4",

            Extension::Flat11 => "♭11",

            Extension::Flat13 => "♭13",
            Extension::Sharp13 => "♯13",

            Extension::Add2 => "add2",
            Extension::Add4 => "add4",
            Extension::Add6 => "add6",

            Extension::Add9 => "add9",
            Extension::Add11 => "add11",
            Extension::Add13 => "add13",
        }
    }
}