diff --git a/crates/domain/src/chord.rs b/crates/domain/src/chord.rs index 5182662..1a4a63d 100644 --- a/crates/domain/src/chord.rs +++ b/crates/domain/src/chord.rs @@ -9,7 +9,7 @@ pub struct Chord { } impl Chord { - pub fn from_str(s: &str) -> Option { + pub fn parse(s: &str) -> Option { let (root, consumed) = Note::parse_prefix(s)?; let descriptor = if consumed < s.len() { Some(s[consumed..].to_string()) @@ -42,7 +42,7 @@ impl From for String { impl TryFrom for Chord { type Error = String; fn try_from(s: String) -> Result { - Chord::from_str(&s).ok_or_else(|| format!("invalid chord: {}", s)) + Chord::parse(&s).ok_or_else(|| format!("invalid chord: {}", s)) } } @@ -52,21 +52,21 @@ mod tests { #[test] fn parse_simple() { - let c = Chord::from_str("Em").unwrap(); + let c = Chord::parse("Em").unwrap(); assert_eq!(c.root, crate::Note::E); assert_eq!(c.descriptor.as_deref(), Some("m")); } #[test] fn parse_no_descriptor() { - let c = Chord::from_str("G").unwrap(); + let c = Chord::parse("G").unwrap(); assert_eq!(c.root, crate::Note::G); assert!(c.descriptor.is_none()); } #[test] fn parse_flat_root() { - let c = Chord::from_str("Bb").unwrap(); + let c = Chord::parse("Bb").unwrap(); assert_eq!(c.root, crate::Note::ASharpBFlat); assert!(c.descriptor.is_none()); } @@ -82,4 +82,11 @@ mod tests { let c = Chord { root: crate::Note::ASharpBFlat, descriptor: None }; assert_eq!(c.name(false), "Bb"); } + + #[test] + fn parse_flat_with_descriptor() { + let c = Chord::parse("Bbm").unwrap(); + assert_eq!(c.root, crate::Note::ASharpBFlat); + assert_eq!(c.descriptor.as_deref(), Some("m")); + } } diff --git a/crates/domain/src/note.rs b/crates/domain/src/note.rs index 93cd244..9f0b9b9 100644 --- a/crates/domain/src/note.rs +++ b/crates/domain/src/note.rs @@ -60,7 +60,7 @@ impl Note { } } - pub fn from_str(s: &str) -> Option { + pub fn parse(s: &str) -> Option { let (note, consumed) = Self::parse_prefix(s)?; if consumed == s.len() { Some(note) } else { None } } @@ -86,7 +86,7 @@ mod tests { #[test] fn parse_cb_enharmonic() { - assert_eq!(Note::from_str("Cb"), Some(Note::B)); + assert_eq!(Note::parse("Cb"), Some(Note::B)); } #[test] @@ -103,9 +103,9 @@ mod tests { #[test] fn parse_note() { - assert_eq!(Note::from_str("F#"), Some(Note::FSharpGFlat)); - assert_eq!(Note::from_str("Gb"), Some(Note::FSharpGFlat)); - assert_eq!(Note::from_str("A"), Some(Note::A)); - assert_eq!(Note::from_str("X"), None); + assert_eq!(Note::parse("F#"), Some(Note::FSharpGFlat)); + assert_eq!(Note::parse("Gb"), Some(Note::FSharpGFlat)); + assert_eq!(Note::parse("A"), Some(Note::A)); + assert_eq!(Note::parse("X"), None); } }