restructure cli for single file use
This commit is contained in:
parent
9269b00235
commit
eb58d0a92f
7 changed files with 51 additions and 65 deletions
2
src/bng.rs
Normal file
2
src/bng.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
mod instrument;
|
||||
mod score;
|
5
src/bng/instrument.rs
Normal file
5
src/bng/instrument.rs
Normal 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
0
src/bng/score.rs
Normal file
66
src/cli.rs
66
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<Instrument>,
|
||||
#[cfg(feature = "save")]
|
||||
#[arg(short, long)]
|
||||
output: Option<PathBuf>,
|
||||
#[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,
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}))
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}))
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
|
||||
mod bng;
|
||||
mod cli;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
|
|
Loading…
Reference in a new issue