|
| 1 | +/* |
| 2 | + Appellation: chord <module> |
| 3 | + Contrib: FL03 <jo3mccain@icloud.com> |
| 4 | + Description: A chord is any set of notes played simultaneously; for our considerations, allow a chord to represent the alphabet of a Turing machine or automata. |
| 5 | +
|
| 6 | +*/ |
| 7 | +use crate::cmp::Note; |
| 8 | +use crate::ArrayLike; |
| 9 | +use serde::{Deserialize, Serialize}; |
| 10 | +use smart_default::SmartDefault; |
| 11 | +use strum::{Display, EnumString, EnumVariantNames}; |
| 12 | + |
| 13 | +/// [Chord] is a wrapper for a [Vec] of [Note] |
| 14 | +#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] |
| 15 | +pub struct Chord(Vec<Note>); |
| 16 | + |
| 17 | +impl Chord { |
| 18 | + pub fn new(chord: impl IntoIterator<Item = Note>) -> Self { |
| 19 | + Self(Vec::from_iter(chord)) |
| 20 | + } |
| 21 | + pub fn chord(&self) -> &Self { |
| 22 | + self |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl ArrayLike for Chord { |
| 27 | + type Data = Note; |
| 28 | + |
| 29 | + fn content(&self) -> &Vec<Self::Data> { |
| 30 | + &self.0 |
| 31 | + } |
| 32 | + |
| 33 | + fn mut_content(&mut self) -> &mut Vec<Self::Data> { |
| 34 | + &mut self.0 |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl IntoIterator for Chord { |
| 39 | + type Item = Note; |
| 40 | + |
| 41 | + type IntoIter = std::vec::IntoIter<Self::Item>; |
| 42 | + |
| 43 | + fn into_iter(self) -> Self::IntoIter { |
| 44 | + self.0.into_iter() |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +pub trait ChordFactor { |
| 49 | + fn factor(&self) -> &ChordFactors |
| 50 | + where |
| 51 | + Self: Sized; |
| 52 | + fn note(&self) -> &Note |
| 53 | + where |
| 54 | + Self: Sized; |
| 55 | +} |
| 56 | + |
| 57 | +#[derive( |
| 58 | + Clone, |
| 59 | + Debug, |
| 60 | + Deserialize, |
| 61 | + Display, |
| 62 | + EnumString, |
| 63 | + EnumVariantNames, |
| 64 | + Eq, |
| 65 | + Hash, |
| 66 | + Ord, |
| 67 | + PartialEq, |
| 68 | + PartialOrd, |
| 69 | + Serialize, |
| 70 | + SmartDefault, |
| 71 | +)] |
| 72 | +#[repr(i64)] |
| 73 | +#[strum(serialize_all = "snake_case")] |
| 74 | +pub enum ChordFactors { |
| 75 | + #[default] |
| 76 | + Root(Note) = 0, |
| 77 | + Third(Note) = 1, |
| 78 | + Fifth(Note) = 2, |
| 79 | +} |
| 80 | + |
| 81 | +pub type Root = Note; |
| 82 | +pub type Third = Note; |
| 83 | +pub type Fifth = Note; |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod test { |
| 87 | + use super::*; |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_chords() { |
| 91 | + let a = vec![0.into(), 3.into(), 8.into()]; |
| 92 | + let mut b = Chord::default(); |
| 93 | + assert!(b.is_empty()); |
| 94 | + b.append(&mut a.clone()); |
| 95 | + assert_eq!(b.len(), 3); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_chord_factors() { |
| 100 | + let a = ChordFactors::default(); |
| 101 | + assert_eq!(a, ChordFactors::Root(Default::default())) |
| 102 | + } |
| 103 | +} |
0 commit comments