From da38edbfd11d3da3c4767b42ca4826fcb04c9525 Mon Sep 17 00:00:00 2001 From: brevalferrari Date: Thu, 5 Jun 2025 17:39:37 +0200 Subject: [PATCH] create export file if not exists --- src/cli/cli.rs | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/cli/cli.rs b/src/cli/cli.rs index 43e9b0e..1d662b0 100644 --- a/src/cli/cli.rs +++ b/src/cli/cli.rs @@ -49,7 +49,7 @@ pub(super) enum Cli { pub(super) struct PlayOpts { /// Use this sheet music [default: stdin] #[command(flatten)] - input: InputGroup, + input: InputGroup, /// Set available notes ("a,b,c" for example) #[arg(short, long, value_delimiter = ',')] notes: Vec, @@ -89,7 +89,10 @@ impl PlayOpts { } } -impl InputGroup { +impl InputGroup +where + Self: Clone, +{ pub(super) fn get(&self) -> Box { self.input .as_ref() @@ -215,34 +218,44 @@ where #[derive(Debug, Parser, Clone)] #[group(required = false, multiple = false)] -pub(super) struct InputGroup { +pub(super) struct InputGroup { /// Set the path to your sheet music file [default: stdin] - input: Option, + input: Option>, /// Use this sheet music instead of reading from a file or stdin #[arg(short = 'c')] sheet_music_string: Option, } #[derive(Debug)] -struct ClonableFile(File); +pub(super) struct ClonableFile(File); -impl Clone for ClonableFile { +impl Clone for ClonableFile { fn clone(&self) -> Self { Self(self.0.try_clone().expect("cloning file handle")) } } -impl FromStr for ClonableFile { +impl FromStr for ClonableFile { type Err = io::Error; fn from_str(s: &str) -> Result { - File::open(s).map(Self) + match CREATE_IF_NOT_EXISTS { + true => File::create(s), + false => File::open(s), + } + .map(Self) + } +} + +impl From> for File { + fn from(value: ClonableFile) -> Self { + value.0 } } #[derive(Debug, Parser, Clone)] pub(super) struct ExportOpts { #[command(flatten)] - playopts: PlayOpts, + pub(super) playopts: PlayOpts, /// Audio format to use #[cfg_attr( feature = "mp3", @@ -253,14 +266,14 @@ pub(super) struct ExportOpts { cfg_attr(feature = "raw", arg(default_value = "raw mulaw")) )] #[arg(short, long, value_parser = audio_format_parser)] - format: AudioFormat, + pub(super) format: AudioFormat, /// Output file [default: stdout] - output: Option, + pub(super) output: Option>, } #[derive(Clone, EnumDiscriminants)] #[strum_discriminants(derive(EnumString, IntoStaticStr), strum(serialize_all = "lowercase"))] -enum AudioFormat { +pub(super) enum AudioFormat { Mp3 { bitrate: Bitrate, quality: Quality, @@ -397,7 +410,7 @@ fn audio_format_parser(input: &str) -> Result { #[derive(Debug, Parser, Clone, EnumString, Default)] #[strum(ascii_case_insensitive)] -enum RawAudioFormat { +pub(super) enum RawAudioFormat { ALaw, F32Be, F32Le,