fix(domain): rename from_str to parse on Note and Chord

This commit is contained in:
2026-04-08 01:33:19 +02:00
parent 422afbb3f2
commit 98370df27a
2 changed files with 18 additions and 11 deletions

View File

@@ -9,7 +9,7 @@ pub struct Chord {
}
impl Chord {
pub fn from_str(s: &str) -> Option<Self> {
pub fn parse(s: &str) -> Option<Self> {
let (root, consumed) = Note::parse_prefix(s)?;
let descriptor = if consumed < s.len() {
Some(s[consumed..].to_string())
@@ -42,7 +42,7 @@ impl From<Chord> for String {
impl TryFrom<String> for Chord {
type Error = String;
fn try_from(s: String) -> Result<Self, Self::Error> {
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"));
}
}

View File

@@ -60,7 +60,7 @@ impl Note {
}
}
pub fn from_str(s: &str) -> Option<Note> {
pub fn parse(s: &str) -> Option<Note> {
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);
}
}