Compare commits

..

No commits in common. "72f5bdfd3dbd4e6876ad24b37e25406067bfc109" and "9269b0023522d78e9ec9ba7eb62a09f6ca6bb50e" have entirely different histories.

7 changed files with 68 additions and 52 deletions

View file

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

View file

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

View file

View file

@ -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; use clap::Parser;
/// Cli entry point mod instrument;
#[derive(Clone, Parser)] mod score;
#[cfg_attr(debug_assertions, derive(Debug))] pub use instrument::Instrument;
pub enum BngCli { pub use score::Score;
/// 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(Parser)]
#[derive(Clone, Parser)]
#[cfg_attr(debug_assertions, derive(Debug))] #[cfg_attr(debug_assertions, derive(Debug))]
pub struct PlayOpts { pub(super) struct Args {
input: PathBuf, #[arg(short, long, value_parser = Instrument::from_str)]
} instrument: Option<Instrument>,
#[cfg(feature = "save")]
/// [`BngCli`] "export" command options #[arg(short, long)]
#[derive(Clone, Parser)] output: Option<PathBuf>,
#[cfg_attr(debug_assertions, derive(Debug))] #[cfg(feature = "play")]
pub struct ExportOpts { #[arg(short, long)]
/// Input file (written song file) silent: bool,
input: PathBuf, score: Score,
/// 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,
} }

21
src/cli/instrument.rs Normal file
View file

@ -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<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
}))
}
}

21
src/cli/score.rs Normal file
View file

@ -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<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,10 +1,11 @@
use anyhow::Result;
use clap::Parser; use clap::Parser;
mod bng;
mod cli; mod cli;
fn main() { fn main() -> Result<()> {
let args = cli::BngCli::parse(); let args = cli::Args::try_parse()?;
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
println!("{:?}", args); println!("{:?}", args);
Ok(())
} }