restructure cli for single file use

This commit is contained in:
Ponj 2024-09-04 12:04:15 -04:00
parent 9269b00235
commit eb58d0a92f
Signed by: p6nj
GPG key ID: 6FED68D87C479A59
7 changed files with 51 additions and 65 deletions

2
src/bng.rs Normal file
View file

@ -0,0 +1,2 @@
mod instrument;
mod score;

5
src/bng/instrument.rs Normal file
View file

@ -0,0 +1,5 @@
use derived_deref::Deref;
use fasteval::Instruction;
#[derive(Deref)]
pub struct Instrument(Instruction);

0
src/bng/score.rs Normal file
View file

View file

@ -1,28 +1,48 @@
use std::str::FromStr; use std::path::PathBuf;
cfg_if! {
if #[cfg(feature = "save")] {
use std::path::PathBuf;
}
}
use cfg_if::cfg_if;
use clap::Parser; use clap::Parser;
mod instrument; /// Cli entry point
mod score; #[derive(Clone, Parser)]
pub use instrument::Instrument;
pub use score::Score;
#[derive(Parser)]
#[cfg_attr(debug_assertions, derive(Debug))] #[cfg_attr(debug_assertions, derive(Debug))]
pub(super) struct Args { pub enum BngCli {
#[arg(short, long, value_parser = Instrument::from_str)] /// Play the song through default sink
instrument: Option<Instrument>, Play(PlayOpts),
#[cfg(feature = "save")] /// Export the song to a sound file
#[arg(short, long)] Export(ExportOpts),
output: Option<PathBuf>, /// List supported sound file extensions and instrument / song available expressions
#[cfg(feature = "play")] #[command(subcommand)]
#[arg(short, long)] List(ListOpts),
silent: bool, }
score: Score,
/// [`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,
} }

View file

@ -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<Self, Self::Err> {
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
}))
}
}

View file

@ -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<Self, Self::Err> {
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
}))
}
}

View file

@ -1,6 +1,7 @@
use anyhow::Result; use anyhow::Result;
use clap::Parser; use clap::Parser;
mod bng;
mod cli; mod cli;
fn main() -> Result<()> { fn main() -> Result<()> {