adapt cli to new yml system

This commit is contained in:
Ponj 2024-10-02 11:21:58 -04:00
parent 6f62bb8f40
commit 903e3ebc64
Signed by: p6nj
GPG key ID: 6FED68D87C479A59

View file

@ -1,5 +1,6 @@
use std::path::PathBuf; use std::{fmt::Display, fs::read_to_string, io, str::FromStr};
use amplify::{From, Wrapper};
use clap::Parser; use clap::Parser;
/// Cli entry point /// Cli entry point
@ -8,9 +9,9 @@ use clap::Parser;
pub enum BngCli { pub enum BngCli {
/// Play the song through default sink /// Play the song through default sink
Play(PlayOpts), Play(PlayOpts),
/// Export the song to a sound file /// Export the song to a sound FileContents
Export(ExportOpts), Export(ExportOpts),
/// List supported sound file extensions and instrument / song available expressions /// List supported sound FileContents extensions and instrument / song available expressions
#[command(subcommand)] #[command(subcommand)]
List(ListOpts), List(ListOpts),
} }
@ -19,24 +20,27 @@ pub enum BngCli {
#[derive(Clone, Parser)] #[derive(Clone, Parser)]
#[cfg_attr(debug_assertions, derive(Debug))] #[cfg_attr(debug_assertions, derive(Debug))]
pub struct PlayOpts { pub struct PlayOpts {
input: PathBuf, #[arg(value_parser = FileContents::from_str)]
input: FileContents,
} }
/// [`BngCli`] "export" command options /// [`BngCli`] "export" command options
#[derive(Clone, Parser)] #[derive(Clone, Parser)]
#[cfg_attr(debug_assertions, derive(Debug))] #[cfg_attr(debug_assertions, derive(Debug))]
pub struct ExportOpts { pub struct ExportOpts {
/// Input file (written song file) /// Input FileContents (written song FileContents)
input: PathBuf, #[arg(value_parser = FileContents::from_str)]
/// Output file (sound file) input: FileContents,
output: PathBuf, /// Output FileContents (sound FileContents)
#[arg(value_parser = AudioFileName::from_str)]
output: AudioFileName,
} }
/// [`BngCli`] "list" command sub-commands /// [`BngCli`] "list" command sub-commands
#[derive(Clone, Parser)] #[derive(Clone, Parser)]
#[cfg_attr(debug_assertions, derive(Debug))] #[cfg_attr(debug_assertions, derive(Debug))]
pub enum ListOpts { pub enum ListOpts {
/// List supported sound file extensions to export songs /// List supported sound FileContents extensions to export songs
#[command(subcommand)] #[command(subcommand)]
Extensions, Extensions,
/// List available math expressions for instrument definition /// List available math expressions for instrument definition
@ -46,3 +50,41 @@ pub enum ListOpts {
#[command(subcommand)] #[command(subcommand)]
Glyphs, Glyphs,
} }
#[derive(Clone, Wrapper, From)]
#[wrapper(Deref)]
#[cfg_attr(debug_assertions, derive(Debug))]
pub struct FileContents(String);
impl FromStr for FileContents {
type Err = io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
read_to_string(s).map(Into::into)
}
}
#[derive(Clone, Wrapper, From)]
#[wrapper(Deref)]
#[cfg_attr(debug_assertions, derive(Debug))]
pub struct AudioFileName(String);
#[derive(Debug)]
pub struct UnsupportedFileExtensionError;
impl std::error::Error for UnsupportedFileExtensionError {}
impl Display for UnsupportedFileExtensionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"The extension of the selected output sound file is not supported."
)
}
}
impl FromStr for AudioFileName {
type Err = UnsupportedFileExtensionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(s.to_owned().into())
}
}