cli files dispatch

This commit is contained in:
Ponj 2024-09-02 22:04:05 -04:00
parent 5e6044eaf5
commit c944db2ab7
Signed by: p6nj
GPG key ID: 6FED68D87C479A59
3 changed files with 48 additions and 50 deletions

View file

@ -1,14 +1,18 @@
use std::{fs::read_to_string, ops::Deref, path::Path, str::FromStr};
use std::str::FromStr;
cfg_if! {
if #[cfg(feature = "save")] {
use std::path::PathBuf;
}
}
use anyhow::{Context, Error};
use cfg_if::cfg_if;
use clap::Parser;
mod instrument;
mod score;
pub use instrument::Instrument;
pub use score::Score;
#[derive(Parser)]
#[cfg_attr(debug_assertions, derive(Debug))]
pub(super) struct Args {
@ -22,51 +26,3 @@ pub(super) struct Args {
silent: bool,
score: Score,
}
#[derive(Clone)]
#[cfg_attr(debug_assertions, derive(Debug))]
pub(super) 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
}))
}
}
impl Deref for Score {
type Target = <String as Deref>::Target;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Clone)]
#[cfg_attr(debug_assertions, derive(Debug))]
pub(super) 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
}))
}
}
impl Deref for Instrument {
type Target = <String as Deref>::Target;
fn deref(&self) -> &Self::Target {
&self.0
}
}

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