From a7c020f7856de6f3155fd44d4fcd69391091b5f3 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Tue, 27 Sep 2022 08:43:12 +0100 Subject: [PATCH] add note --- src/note.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/note.rs diff --git a/src/note.rs b/src/note.rs new file mode 100644 index 0000000..0284302 --- /dev/null +++ b/src/note.rs @@ -0,0 +1,63 @@ +use core::fmt; +use std::fmt::Formatter; + +#[derive(Clone, Debug, PartialEq)] +pub enum RelativeNote { + C, + CSharp, + D, + DSharp, + E, + F, + FSharp, + G, + GSharp, + A, + ASharp, + B, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct Note { + relative: RelativeNote, + octave: u8, // upper limit? 8? +} + +impl fmt::Display for Note { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!( + f, + "{}{}", + match self.relative { + RelativeNote::C => "C", + RelativeNote::CSharp => "C#", + RelativeNote::D => "D", + RelativeNote::DSharp => "D#", + RelativeNote::E => "E", + RelativeNote::F => "F", + RelativeNote::FSharp => "F#", + RelativeNote::G => "G", + RelativeNote::GSharp => "G#", + RelativeNote::A => "A", + RelativeNote::ASharp => "A#", + RelativeNote::B => "B", + }, + self.octave + ) + } +} + +#[cfg(test)] +mod test { + use crate::note::{Note, RelativeNote}; + + #[test] + fn c4() { + assert_eq!(Note { relative: RelativeNote::C, octave: 4 }.to_string(), "C4"); + } + + #[test] + fn a_sharp_0() { + assert_eq!(Note { relative: RelativeNote::ASharp, octave: 0 }.to_string(), "A#0"); + } +}