diff --git a/src/bng.rs b/src/bng.rs deleted file mode 100644 index 6c2693a..0000000 --- a/src/bng.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod instrument; -mod score; diff --git a/src/bng/instrument.rs b/src/bng/instrument.rs deleted file mode 100644 index b8db277..0000000 --- a/src/bng/instrument.rs +++ /dev/null @@ -1,5 +0,0 @@ -use derived_deref::Deref; -use fasteval::Instruction; - -#[derive(Deref)] -pub struct Instrument(Instruction); diff --git a/src/bng/score.rs b/src/bng/score.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/cli.rs b/src/cli.rs index 5057a49..bc5587c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,48 +1,28 @@ -use std::path::PathBuf; +use std::str::FromStr; +cfg_if! { + if #[cfg(feature = "save")] { + use std::path::PathBuf; + } +} +use cfg_if::cfg_if; use clap::Parser; -/// Cli entry point -#[derive(Clone, Parser)] -#[cfg_attr(debug_assertions, derive(Debug))] -pub enum BngCli { - /// Play the song through default sink - Play(PlayOpts), - /// Export the song to a sound file - Export(ExportOpts), - /// List supported sound file extensions and instrument / song available expressions - #[command(subcommand)] - List(ListOpts), -} +mod instrument; +mod score; +pub use instrument::Instrument; +pub use score::Score; -/// [`BngCli`] "play" command options -#[derive(Clone, Parser)] +#[derive(Parser)] #[cfg_attr(debug_assertions, derive(Debug))] -pub struct PlayOpts { - input: PathBuf, -} - -/// [`BngCli`] "export" command options -#[derive(Clone, Parser)] -#[cfg_attr(debug_assertions, derive(Debug))] -pub struct ExportOpts { - /// Input file (written song file) - input: PathBuf, - /// Output file (sound file) - output: PathBuf, -} - -/// [`BngCli`] "list" command sub-commands -#[derive(Clone, Parser)] -#[cfg_attr(debug_assertions, derive(Debug))] -pub enum ListOpts { - /// List supported sound file extensions to export songs - #[command(subcommand)] - Extensions, - /// List available math expressions for instrument definition - #[command(subcommand)] - Math, - /// List available score glyphs and their meaning - #[command(subcommand)] - Glyphs, +pub(super) struct Args { + #[arg(short, long, value_parser = Instrument::from_str)] + instrument: Option, + #[cfg(feature = "save")] + #[arg(short, long)] + output: Option, + #[cfg(feature = "play")] + #[arg(short, long)] + silent: bool, + score: Score, } diff --git a/src/cli/instrument.rs b/src/cli/instrument.rs new file mode 100644 index 0000000..c9bfe64 --- /dev/null +++ b/src/cli/instrument.rs @@ -0,0 +1,21 @@ +use std::{fs::read_to_string, path::Path, str::FromStr}; + +use anyhow::{Context, Error}; +use derived_deref::Deref; + +#[derive(Clone, Deref)] +#[cfg_attr(debug_assertions, derive(Debug))] +pub struct Instrument(String); + +impl FromStr for Instrument { + type Err = Error; + fn from_str(value: &str) -> Result { + let maybe_a_path = Path::new(&value); + Ok(Instrument(if maybe_a_path.is_file() { + read_to_string(maybe_a_path) + .context("you gave me the path of a real file but reading it is hard!")? + } else { + value.to_owned() // hope it's a good one + })) + } +} diff --git a/src/cli/score.rs b/src/cli/score.rs new file mode 100644 index 0000000..a1fac87 --- /dev/null +++ b/src/cli/score.rs @@ -0,0 +1,21 @@ +use std::{fs::read_to_string, path::Path, str::FromStr}; + +use anyhow::{Context, Error}; +use derived_deref::Deref; + +#[derive(Clone, Deref)] +#[cfg_attr(debug_assertions, derive(Debug))] +pub struct Score(String); + +impl FromStr for Score { + type Err = Error; + fn from_str(value: &str) -> Result { + let maybe_a_path = Path::new(&value); + Ok(Score(if maybe_a_path.is_file() { + read_to_string(maybe_a_path) + .context("you gave me the path of a real file but reading it is hard!")? + } else { + value.to_owned() // hope it's a good one + })) + } +} diff --git a/src/main.rs b/src/main.rs index 615cdae..d3528bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,11 @@ +use anyhow::Result; use clap::Parser; -mod bng; mod cli; -fn main() { - let args = cli::BngCli::parse(); +fn main() -> Result<()> { + let args = cli::Args::try_parse()?; #[cfg(debug_assertions)] println!("{:?}", args); + Ok(()) }