chord-parser 1.1.1

Utilities for parsing chord signatures
Documentation
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/// Represents basic musical pitch types (excluding accidentals)
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
pub enum Pitch {
    /// Base A pitch
    A,
    /// Base B pitch
    B,
    /// Base C pitch
    C,
    /// Base D pitch
    D,
    /// Base E pitch
    E,
    /// Base F pitch
    F,
    /// Base G pitch
    G,
}

impl Pitch {
    /// Tries to parse a single character into `Pitch`. Case insensitive.
    /// # Examples
    /// ```
    /// use chord_parser::chord::*;
    /// 
    /// assert_eq!(Some(Pitch::A), Pitch::from_char(&'a')); // Case insensitive
    /// assert_eq!(None, Pitch::from_char(&' ')); // Invalid input
    /// ```
    pub fn from_char(ch: &char) -> Option<Self> {
        let formatted = match ch.to_lowercase().next() {
            Some(ch) => ch,
            None => return None,
        };
        
        match formatted {
            'a' => Some(Self::A),
            'b' => Some(Self::B),
            'c' => Some(Self::C),
            'd' => Some(Self::D),
            'e' => Some(Self::E),
            'f' => Some(Self::F),
            'g' => Some(Self::G),
            _ => None,
        }
    }
}

impl ToString for Pitch {
    fn to_string(&self) -> String {
        match self {
            Self::A => "A".into(),
            Self::B => "B".into(),
            Self::C => "C".into(),
            Self::D => "D".into(),
            Self::E => "E".into(),
            Self::F => "F".into(),
            Self::G => "G".into(),
        }
    }
}

/// Represents an accidental alteration on a note.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Accidental {
    /// Natural (no accidental). '♮' symbol.
    Natural,
    /// Sharp accidental. '♯', '#' symbols.
    Sharp,
    /// Double sharp accidental. '𝄪', '##' symbols.
    DoubleSharp,
    /// Flat accidental. '♭', 'b' symbols.
    Flat,
    /// Double flat accidental. '𝄫', 'bb' symbols.
    DoubleFlat,
}

impl Accidental {
    /// Tries to parse a string into an accidental. Case insensitive (for both flat alterations).
    /// # Examples
    /// ```
    /// use chord_parser::chord::*;
    /// 
    /// assert_eq!(Some(Accidental::Flat), Accidental::from_str("B")); // Case insensitive
    /// assert_eq!(Some(Accidental::DoubleFlat), Accidental::from_str("𝄫")); // Unicode works
    /// assert_eq!(None, Accidental::from_str("as")); // Invalid input
    /// ```
    pub fn from_str(s: &str) -> Option<Self> {
        let len = s.chars().count();

        if len > 2 || s.is_empty() {
            return None;
        }

        let fst = s.chars().next().unwrap();

        match fst {
            '' => return Some(Self::Natural),
            '' => return Some(Self::Sharp),
            '𝄪' => return Some(Self::DoubleSharp),
            '' => return Some(Self::Flat),
            '𝄫' => return Some(Self::DoubleFlat),
            _ => (),
        }
        
        let is_sharp = match fst {
            '#' => true,
            'b' | 'B' => false,
            _ => return None,
        };

        if len > 1 {
            let snd = s.chars().nth(1).unwrap();

            match is_sharp {
                true => {
                    if snd != '#' {
                        return None;
                    }

                    Some(Self::DoubleSharp)
                }
                false => {
                    if snd != 'b' && snd != 'B' {
                        return None;
                    }

                    Some(Self::DoubleFlat)
                }
            }
        } else {
            match is_sharp {
                true => Some(Self::Sharp),
                false => Some(Self::Flat),
            }
        }
    }
}

impl ToString for Accidental {
    fn to_string(&self) -> String {
        match self {
            Self::Natural => "".into(),
            Self::Sharp => "".into(),
            Self::DoubleSharp => "𝄪".into(),
            Self::Flat => "".into(),
            Self::DoubleFlat => "𝄫".into(),
        }
    }
}

/// Represents a chord note.
/// 
/// # Note
/// `pitch` and `accidental` can be used to calculate the precise note or a frequency in Hz,
/// however would require additional input of octave.
/// Since octave is not a part of a note on a chord SIGNATURE, this property is therefore omitted.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Note {
    /// The pitch of the note
    pub pitch: Pitch,
    /// The accidental of the note
    pub accidental: Accidental,
}

impl ToString for Note {
    fn to_string(&self) -> String {
        let mut s = self.pitch.to_string();
        s.push_str(self.accidental.to_string().as_str());

        s
    }
}

/// Represents a triad type of a chord.
/// 
/// # Note
/// Chord signatures like "C7#5" or "Cm7b5" would NOT be considered augmented and diminished triad respectively.
/// This enum refers to the triad type right after the specified pitch.
/// Therefore, the provided examples in this note would be considered
/// `Self::Major` and `Self::Minor` type respectively.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
pub enum ChordTriadType {
    /// Major triad
    Major,
    /// Minor triad
    Minor,
    /// Augmented triad
    Augmented,
    /// Diminished triad
    Diminished,
}

impl ToString for ChordTriadType {
    fn to_string(&self) -> String {
        match self {
            Self::Major => "".into(),
            Self::Minor => "m".into(),
            Self::Augmented => "aug".into(),
            Self::Diminished => "dim".into(),
        }
    }
}

/// Represents the type of seventh in a chord. Usually a part of [`Alterations`] struct.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Seventh {
    /// No seventh present in a chord
    None,
    /// Flatten (regular) seventh. Used for dominant and regular minor chords.
    Flat,
    /// Major (sharpened) seventh. In a chord signature - "maj7".
    Major,
}

impl ToString for Seventh {
    fn to_string(&self) -> String {
        match self {
            Self::None => "".into(),
            Self::Flat => "7".into(),
            Self::Major => "maj7".into(),
        }
    }
}

/// Represents the interval of an alteration. Used in [`ChordAlter`] enum.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum AlteredInterval {
    /// "2" interval
    Second,
    /// "4" interval
    Fourth,
    /// "5" interval
    Fifth,
    /// "6" interval
    Sixth,
    /// "7" interval
    Seventh,
    /// "9" interval
    Ninth,
    /// "10" interval
    Tenth,
    /// "11" interval
    Eleventh,
    /// "13" interval
    Thirteenth,
}

impl AlteredInterval {
    /// Tries to parse a [`usize`] numer into `AlteredInterval`.
    /// 
    /// # Examples
    /// ```
    /// use chord_parser::chord::*;
    /// 
    /// assert_eq!(Some(AlteredInterval::Second), AlteredInterval::from_usize(2));
    /// assert_eq!(Some(AlteredInterval::Ninth), AlteredInterval::from_usize(9));
    /// assert_eq!(Some(AlteredInterval::Thirteenth), AlteredInterval::from_usize(13));
    /// assert_eq!(None, AlteredInterval::from_usize(15)); // Invalid input
    /// ```
    pub fn from_usize(num: usize) -> Option<AlteredInterval> {
        match num {
            2 => Some(Self::Second),
            4 => Some(Self::Fourth),
            5 => Some(Self::Fifth),
            6 => Some(Self::Sixth),
            7 => Some(Self::Seventh),
            9 => Some(Self::Ninth),
            10 => Some(Self::Tenth),
            11 => Some(Self::Eleventh),
            13 => Some(Self::Thirteenth),
            _ => None,
        }
    }
}

impl ToString for AlteredInterval {
    fn to_string(&self) -> String {
        match self {
            Self::Second => "2".into(),
            Self::Fourth => "4".into(),
            Self::Fifth => "5".into(),
            Self::Sixth => "6".into(),
            Self::Seventh => "7".into(),
            Self::Ninth => "9".into(),
            Self::Tenth => "10".into(),
            Self::Eleventh => "11".into(),
            Self::Thirteenth => "13".into(),
        }
    }
}

/// Represents an ålteration of a note
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ChordNoteAlter {
    /// The interval note being altered
    pub interval: AlteredInterval,
    /// The new accidental of the interval note
    pub accidental: Accidental,
}

impl ToString for ChordNoteAlter {
    fn to_string(&self) -> String {
        let mut s = self.interval.to_string();
        s.push_str(self.accidental.to_string().as_str());

        s
    }
}

/// Represents an alteration in a chord.
/// 
/// Typically used in [`Alterations.alters(&self)`] vector array.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum ChordAlter {
    /// Added note interval alteration
    Add(ChordNoteAlter),
    /// Suspension intreval alteration
    Suspended(AlteredInterval),
}

impl ToString for ChordAlter {
    fn to_string(&self) -> String {
        match self {
            Self::Add(alter) => {
                "add".to_string() + alter.to_string().as_str()
            }
            Self::Suspended(interval) => {
                "sus".to_string() + interval.to_string().as_str()
            }
        }
    }
}

/// Represents a "no." notation of chords that a tone is missing.
/// 
/// "5" chords are technically "no3" chords.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum No {
    /// "no." is not present in a chord
    None,
    /// Omit 3rd. "no3"
    Third,
    /// Omit 5th. "no5"
    Fifth,
}

impl ToString for No {
    fn to_string(&self) -> String {
        match &self {
            Self::None => "".into(),
            Self::Third => "no3".into(),
            Self::Fifth => "no5".into(),
        }
    }
}

/// Represents a list of all the alterations presented in a chord.
/// 
/// This struct provides a simple and intuitive way to add alterations using the safe included methods.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Alterations {
    /// Represents an omitted tone in the
    pub no: No,
    /// Represents a seventh in the chord
    pub seventh: Seventh,
    /// Represents the bass note override (slash chord)
    pub slash: Option<Note>,
    
    alters: Vec<ChordAlter>,
}

impl Alterations {
    /// Creates a new empty instance of `Alterations`.
    /// 
    /// # Examples
    /// ```
    /// use chord_parser::chord::*;
    /// 
    /// let mut alterations = Alterations::new();
    /// 
    /// // Do whatever
    /// alterations.set_suspension(&AlteredInterval::Fourth);
    /// ```
    pub fn new() -> Self {
        Alterations { no: No::None, seventh: Seventh::None, slash: None, alters: vec![] }
    }

    /// Returns a list of all the added alterations.
    /// 
    /// # Examples
    /// ```
    /// use chord_parser::chord::*;
    /// 
    /// let mut alterations = Alterations::new();
    /// 
    /// alterations.set_note(&ChordNoteAlter {
    ///     interval: AlteredInterval::Ninth,
    ///     accidental: Accidental::Sharp,
    /// });
    /// 
    /// alterations.set_suspension(&AlteredInterval::Fourth);
    /// 
    /// alterations.alters(); // Returns the added ninth and suspended fourth
    /// ```
    pub fn alters(&self) -> &Vec<ChordAlter> {
        &self.alters
    }

    /// Tries to get a note alteration associated with the passed interval.
    /// 
    /// # Examples
    /// ```
    /// use chord_parser::chord::*;
    /// 
    /// let mut alterations = Alterations::new();
    /// 
    /// alterations.set_note(&ChordNoteAlter {
    ///     interval: AlteredInterval::Ninth,
    ///     accidental: Accidental::Sharp,
    /// });
    /// 
    /// alterations.get_note(&AlteredInterval::Ninth); // Returns the interval and accidental
    /// ```
    pub fn get_note(&self, interval: &AlteredInterval) -> Option<&ChordNoteAlter> {
        for alter in self.alters.iter() {
            match alter {
                ChordAlter::Add(a) => if a.interval == *interval {
                    return Some(a);
                }
                _ => continue,
            }
        }

        None
    }

    /// Tries to get the accidental altered for an interval.
    /// 
    /// # Examples
    /// ```
    /// use chord_parser::chord::*;
    /// 
    /// let mut alterations = Alterations::new();
    /// 
    /// alterations.set_note(&ChordNoteAlter {
    ///     interval: AlteredInterval::Ninth,
    ///     accidental: Accidental::Sharp,
    /// });
    /// 
    /// alterations.get_accidental(&AlteredInterval::Ninth); // Returns Accidental::Sharp
    /// ```
    pub fn get_accidental(&self, interval: &AlteredInterval) -> Option<Accidental> {
        if let Some(alter) = self.get_note(interval) {
            return Some(alter.accidental.clone());
        }

        None
    }

    /// Set a new alteration for an interval.
    /// 
    /// If an alteration for the interval already exists, the accidental will be overwritten.
    /// If it doesn't exist, a new alteration is added.
    /// 
    /// # Examples
    /// ```
    /// use chord_parser::chord::*;
    /// 
    /// let mut alterations = Alterations::new();
    /// 
    /// alterations.set_note(&ChordNoteAlter {
    ///     interval: AlteredInterval::Ninth,
    ///     accidental: Accidental::Sharp,
    /// });
    /// 
    /// // Now the 9th is flatten
    /// alterations.set_note(&ChordNoteAlter {
    ///     interval: AlteredInterval::Ninth,
    ///     accidental: Accidental::Flat,
    /// });
    /// 
    /// alterations.set_note(&ChordNoteAlter {
    ///     interval: AlteredInterval::Eleventh,
    ///     accidental: Accidental::Sharp,
    /// });
    /// 
    /// // Get back all the 2 alterations added
    /// alterations.alters();
    /// ```
    pub fn set_note(&mut self, alter: &ChordNoteAlter) {
        for other in self.alters.iter_mut() {
            match other {
                ChordAlter::Add(other) => if other.interval == alter.interval {
                    other.accidental = alter.accidental.clone();
                    return;
                }
                _ => continue,
            }
        }

        self.alters.push(ChordAlter::Add( alter.clone()));
    }

    /// Tries to get the suspended note alteration in the chord
    /// 
    /// # Examples
    /// ```
    /// use chord_parser::chord::*;
    /// 
    /// let mut alterations = Alterations::new();
    /// 
    /// alterations.set_suspension(&AlteredInterval::Fourth);
    /// alterations.get_suspension(); // Returns Some(&AlteredInterval::Fourth)
    /// ```
    pub fn get_suspension(&self) -> Option<&AlteredInterval> {
        for alter in self.alters.iter() {
            match alter {
                ChordAlter::Suspended(interval) => return Some(interval),
                _ => (),
            }
        }

        None
    }

    /// Set the new suspended interval.
    /// 
    /// Old suspended interval will be replaced with the new one,
    /// if the old interval is present.
    /// 
    /// # Examples
    /// ```
    /// use chord_parser::chord::*;
    /// 
    /// let mut alterations = Alterations::new();
    /// 
    /// alterations.set_suspension(&AlteredInterval::Second);
    /// alterations.get_suspension(); // Returns Some(&AlteredInterval::Second)
    /// ```
    pub fn set_suspension(&mut self, interval: &AlteredInterval) {
        for (i, alter) in self.alters.iter_mut().enumerate() {
            match alter {
                ChordAlter::Suspended(_) => {
                    self.alters[i] = ChordAlter::Suspended(interval.clone());
                    return;
                }
                _ => (),
            }
        }

        self.alters.push(ChordAlter::Suspended(interval.clone()));
    }
}

impl ToString for Alterations {
    fn to_string(&self) -> String {
        let mut s = self.no.to_string();
        s.push_str(self.seventh.to_string().as_str());

        self.alters.iter().for_each(|a| s.push_str(a.to_string().as_str()));

        if let Some(slash) = &self.slash {
            let slash_str = "/".to_string() + slash.to_string().as_str();
            s.push_str(slash_str.as_str());
        }

        s
    }
}

/// Represents a full chord signature.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Chord {
    /// Root note
    pub note: Note,
    /// Triad type
    pub chord_type: ChordTriadType,
    /// Alterations
    pub alterations: Alterations,
}

impl ToString for Chord {
    fn to_string(&self) -> String {
        let mut s = self.note.to_string();
        s.push_str(self.chord_type.to_string().as_str());
        s.push_str(self.alterations.to_string().as_str());

        s
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pitch_quality() {
        assert_eq!(Pitch::C, Pitch::from_char(&'C').unwrap());
        assert_eq!(Pitch::D, Pitch::from_char(&'D').unwrap());
        assert_eq!(Pitch::E, Pitch::from_char(&'E').unwrap());
        assert_eq!(Pitch::F, Pitch::from_char(&'F').unwrap());
        assert_eq!(Pitch::G, Pitch::from_char(&'G').unwrap());
        assert_eq!(Pitch::A, Pitch::from_char(&'A').unwrap());
        assert_eq!(Pitch::B, Pitch::from_char(&'B').unwrap());

        assert_eq!(None, Pitch::from_char(&'w'));
        assert_eq!(None, Pitch::from_char(&'1'));
    }

    #[test]
    fn pitch_insensitivity() {
        assert_eq!(Pitch::C, Pitch::from_char(&'C').unwrap());
        assert_eq!(Pitch::B, Pitch::from_char(&'b').unwrap());
        assert_eq!(Pitch::B, Pitch::from_char(&'B').unwrap());
    }

    #[test]
    fn accidental_from() {
        assert_eq!(Accidental::Sharp, Accidental::from_str("#").unwrap());
        assert_eq!(Accidental::DoubleSharp, Accidental::from_str("##").unwrap());
        assert_eq!(Accidental::Flat, Accidental::from_str("b").unwrap());

        assert_eq!(Accidental::DoubleFlat, Accidental::from_str("bb").unwrap());
        assert_eq!(Accidental::DoubleFlat, Accidental::from_str("bB").unwrap());
        assert_eq!(Accidental::DoubleFlat, Accidental::from_str("Bb").unwrap());
        assert_eq!(Accidental::DoubleFlat, Accidental::from_str("BB").unwrap());

        assert_eq!(None, Accidental::from_str("b#"));
        assert_eq!(None, Accidental::from_str("#b"));

        assert_eq!(None, Accidental::from_str(""));
        assert_eq!(None, Accidental::from_str("nonsense"));
    }

    #[test]
    fn alterations_struct_fns() {
        let mut a = Alterations::new();

        assert_eq!(None, a.get_note(&AlteredInterval::Ninth));

        a.set_note(&ChordNoteAlter { interval: AlteredInterval::Ninth, accidental: Accidental::Sharp });

        assert_eq!(Some(Accidental::Sharp), a.get_accidental(&AlteredInterval::Ninth));

        a.set_note(&ChordNoteAlter { interval: AlteredInterval::Ninth, accidental: Accidental::Flat });

        assert_eq!(Some(Accidental::Flat), a.get_accidental(&AlteredInterval::Ninth));
        assert_eq!(1, a.alters.len());
    }

    #[test]
    fn interval_from() {
        assert_eq!(AlteredInterval::Second, AlteredInterval::from_usize(2).unwrap());
        assert_eq!(AlteredInterval::Thirteenth, AlteredInterval::from_usize(13).unwrap());
        assert_eq!(AlteredInterval::Eleventh, AlteredInterval::from_usize(11).unwrap());

        assert_eq!(None, AlteredInterval::from_usize(0));
        assert_eq!(None, AlteredInterval::from_usize(999));
    }
}