From eb58d0a92fa18a0f801db743e80a13e918c34b52 Mon Sep 17 00:00:00 2001 From: p6nj Date: Wed, 4 Sep 2024 12:04:15 -0400 Subject: [PATCH] restructure cli for single file use --- src/bng.rs | 2 ++ src/bng/instrument.rs | 5 ++++ src/bng/score.rs | 0 src/cli.rs | 66 ++++++++++++++++++++++++++++--------------- src/cli/instrument.rs | 21 -------------- src/cli/score.rs | 21 -------------- src/main.rs | 1 + 7 files changed, 51 insertions(+), 65 deletions(-) create mode 100644 src/bng.rs create mode 100644 src/bng/instrument.rs create mode 100644 src/bng/score.rs delete mode 100644 src/cli/instrument.rs delete mode 100644 src/cli/score.rs diff --git a/src/bng.rs b/src/bng.rs new file mode 100644 index 0000000..6c2693a --- /dev/null +++ b/src/bng.rs @@ -0,0 +1,2 @@ +mod instrument; +mod score; diff --git a/src/bng/instrument.rs b/src/bng/instrument.rs new file mode 100644 index 0000000..b8db277 --- /dev/null +++ b/src/bng/instrument.rs @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..e69de29 diff --git a/src/cli.rs b/src/cli.rs index bc5587c..5057a49 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,28 +1,48 @@ -use std::str::FromStr; -cfg_if! { - if #[cfg(feature = "save")] { - use std::path::PathBuf; - } -} +use std::path::PathBuf; -use cfg_if::cfg_if; use clap::Parser; -mod instrument; -mod score; -pub use instrument::Instrument; -pub use score::Score; - -#[derive(Parser)] +/// Cli entry point +#[derive(Clone, Parser)] #[cfg_attr(debug_assertions, derive(Debug))] -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, +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), +} + +/// [`BngCli`] "play" command options +#[derive(Clone, 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, } diff --git a/src/cli/instrument.rs b/src/cli/instrument.rs deleted file mode 100644 index c9bfe64..0000000 --- a/src/cli/instrument.rs +++ /dev/null @@ -1,21 +0,0 @@ -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 deleted file mode 100644 index a1fac87..0000000 --- a/src/cli/score.rs +++ /dev/null @@ -1,21 +0,0 @@ -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 d3528bf..587a5fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use anyhow::Result; use clap::Parser; +mod bng; mod cli; fn main() -> Result<()> {