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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use serde::Serialize;
use num_traits::{
    FromPrimitive,
    ToPrimitive
};


#[derive(Copy, Serialize, Debug, PartialEq, Clone, FromPrimitive, ToPrimitive)]
pub enum Note {
    A, ASharp, B, C, CSharp, D, DSharp, E, F, FSharp, G, GSharp
}

#[derive(Serialize, Debug, Default, PartialEq, Clone)]
pub struct Chord{
    pub root: Note,
    pub minor: bool,
    pub others: String,
    pub number: u8,
    pub bass: Note
}

impl Chord {
    pub fn major(n: Note) -> Self {
        Chord {
            root: n,
            minor: false,
            others: String::new(),
            number: 0,
            bass: n
        }
    }

    pub fn minor(n: Note) -> Self {
        Chord {
            root: n,
            minor: true,
            others: String::new(),
            number: 0,
            bass: n
        }
    }
}

impl std::ops::Add<i8> for Note {
    type Output = Note;
    fn add(self, other: i8) -> Self::Output {
        let k = (self.to_i8().unwrap() + other + 12) % 12;

        Note::from_i8(k).unwrap()
    }
}

impl std::ops::Sub<i8> for Note {
    type Output = Note;
    fn sub(self, other: i8) -> Self::Output {
        self + (-other)
    }
}

impl Default for Note{
    fn default() -> Self {
        Note::A
    }
}

impl std::fmt::Display for Note {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if f.alternate(){
            write!(f, "{}", match self {
                Note::A => "A",
                Note::ASharp => "A#",
                Note::B => "B",
                Note::C => "C",
                Note::CSharp => "C#",
                Note::D => "D",
                Note::DSharp => "D#",
                Note::E => "E",
                Note::F => "F",
                Note::FSharp => "F#",
                Note::G => "G",
                Note::GSharp => "G#",
            })
        } else{
            write!(f, "{}", match self {
                Note::A => "A",
                Note::ASharp => "Bb",
                Note::B => "B",
                Note::C => "C",
                Note::CSharp => "C#",
                Note::D => "D",
                Note::DSharp => "D#",
                Note::E => "E",
                Note::F => "F",
                Note::FSharp => "F#",
                Note::G => "G",
                Note::GSharp => "G#",
            })
        }
    }
}

impl std::fmt::Binary for Note {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if f.alternate(){
            write!(f, "{}", match self {
                Note::A => "La",
                Note::ASharp => "La#",
                Note::B => "Si",
                Note::C => "Do",
                Note::CSharp => "Do#",
                Note::D => "Re",
                Note::DSharp => "Re#",
                Note::E => "Mi",
                Note::F => "Fa",
                Note::FSharp => "Fa#",
                Note::G => "Sol",
                Note::GSharp => "Sol#",
            })
        } else{
            write!(f, "{}", match self {
                Note::A => "La",
                Note::ASharp => "Sib",
                Note::B => "Si",
                Note::C => "Do",
                Note::CSharp => "Do#",
                Note::D => "Re",
                Note::DSharp => "Re#",
                Note::E => "Mi",
                Note::F => "Fa",
                Note::FSharp => "Fa#",
                Note::G => "Sol",
                Note::GSharp => "Sol#",
            })
        }
    }
}

impl std::fmt::Display for Chord {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if f.alternate(){
            write!(f, "{:#}", self.root)
        } else {
            write!(f, "{}", self.root)
        }?;

        if self.minor {
            write!(f, "m")?;
        }

        write!(f, "{}", self.others)
    }
}

impl std::fmt::Binary for Chord {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if f.alternate(){
            write!(f, "{:#b}", self.root)
        } else {
            write!(f, "{:b}", self.root)
        }?;

        if self.minor {
            write!(f, "m")?;
        }

        write!(f, "{}", self.others)
    }
}