Skip to content

Commit

Permalink
Add stubbed command for notes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwellmattryan committed Oct 31, 2024
1 parent 55b779f commit be9bdd2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use async_trait::async_trait;

use crate::{commands::midi::MidiCommand, error::FNoteResult};
use crate::{
commands::{midi::MidiCommand, note::NoteCommand},
error::FNoteResult,
};

/// Represents an `fnote` program command.
#[async_trait]
Expand All @@ -16,15 +19,19 @@ pub trait Command {
version = env!("CARGO_PKG_VERSION"),
)]
pub enum FNoteCommand {
/// Extracts the frequency and musical note from a MIDI note number (0-127).
/// Extracts the frequency and music note from a MIDI note number (0-127).
Midi(MidiCommand),

/// Extracts the frequency and MIDI note number from a music note (e.g. C5).
Note(NoteCommand),
}

#[async_trait]
impl Command for FNoteCommand {
async fn run(&self) -> FNoteResult<()> {
match self {
Self::Midi(cmd) => cmd.run().await,
Self::Note(cmd) => cmd.run().await,
}
}
}
2 changes: 1 addition & 1 deletion src/commands/midi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
error::{FNoteError, FNoteResult},
};

/// Extracts the frequency and musical note from a MIDI note number (0-127).
/// Extracts the frequency and music note from a MIDI note number (0-127).
#[derive(structopt::StructOpt)]
pub struct MidiCommand {
/// The MIDI note number to use.
Expand Down
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub(crate) mod midi;
pub(crate) mod note;
34 changes: 34 additions & 0 deletions src/commands/note.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use async_trait::async_trait;
use regex::Regex;

use crate::{
cli::Command,
error::{FNoteError, FNoteResult},
};

/// Extracts the frequency and MIDI note number from a music note (e.g. C5).
#[derive(structopt::StructOpt)]
pub struct NoteCommand {
/// The music note to use.
#[structopt(parse(try_from_str = try_music_note_from_str))]
pub musical_note: String,
}

#[async_trait]
impl Command for NoteCommand {
async fn run(&self) -> FNoteResult<()> {
println!("Converting music note: {:?}", self.musical_note);

Ok(())
}
}

/// Parses a music note from a string.
fn try_music_note_from_str(arg: &str) -> FNoteResult<String> {
let regex = Regex::new(r"^[A-Ga-g][#b]?[0-9]$").unwrap();
if regex.is_match(arg) {
Ok(arg.to_string())
} else {
Err(FNoteError::InvalidMusicNote(arg.to_string()))
}
}
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ pub enum FNoteError {

#[error("Invalid MIDI note number \"{0}\"")]
InvalidMidiNoteNumber(String),

#[error("Invalid music note \"{0}\"")]
InvalidMusicNote(String),
}

0 comments on commit be9bdd2

Please sign in to comment.