-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dca1e1
commit 55b779f
Showing
8 changed files
with
88 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use async_trait::async_trait; | ||
|
||
use crate::{commands::midi::MidiCommand, error::FNoteResult}; | ||
|
||
/// Represents an `fnote` program command. | ||
#[async_trait] | ||
pub trait Command { | ||
/// Executes the corresponding command. | ||
async fn run(&self) -> FNoteResult<()>; | ||
} | ||
|
||
/// Commands supported by `fnote`. | ||
#[derive(structopt::StructOpt)] | ||
#[structopt( | ||
author = "Matthew Maxwell", | ||
version = env!("CARGO_PKG_VERSION"), | ||
)] | ||
pub enum FNoteCommand { | ||
/// Extracts the frequency and musical note from a MIDI note number (0-127). | ||
Midi(MidiCommand), | ||
} | ||
|
||
#[async_trait] | ||
impl Command for FNoteCommand { | ||
async fn run(&self) -> FNoteResult<()> { | ||
match self { | ||
Self::Midi(cmd) => cmd.run().await, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 musical note from a MIDI note number (0-127). | ||
#[derive(structopt::StructOpt)] | ||
pub struct MidiCommand { | ||
/// The MIDI note number to use. | ||
#[structopt(parse(try_from_str = try_midi_note_number_from_str))] | ||
pub midi_note_number: usize, | ||
} | ||
|
||
#[async_trait] | ||
impl Command for MidiCommand { | ||
async fn run(&self) -> FNoteResult<()> { | ||
println!("Converting MIDI note number: {:?}", self.midi_note_number); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// Parses a MIDI note number from a string. | ||
fn try_midi_note_number_from_str(arg: &str) -> FNoteResult<usize> { | ||
let regex = Regex::new(r"^(?:\d|[1-9]\d|1[01]\d|12[0-7])$").unwrap(); | ||
if regex.is_match(arg) { | ||
Ok(arg.parse::<usize>().unwrap()) | ||
} else { | ||
Err(FNoteError::InvalidMidiNoteNumber(arg.to_string())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod midi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
pub(crate) mod error; | ||
pub mod cli; | ||
pub mod error; | ||
|
||
pub(crate) mod commands; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
use crate::error::FNoteResult; | ||
|
||
mod error; | ||
use fnote::{ | ||
cli::{Command, FNoteCommand}, | ||
error::{FNoteError, FNoteResult}, | ||
}; | ||
use structopt::StructOpt; | ||
|
||
#[tokio::main] | ||
async fn main() -> FNoteResult<()> { | ||
Ok(()) | ||
match FNoteCommand::from_args().run().await { | ||
Ok(_) => Ok(()), | ||
Err(_) => panic!("{:?}", FNoteError::UnknownCommand), | ||
} | ||
} |