feat: add list and check incompatible args

This commit is contained in:
zawz 2023-09-06 15:27:52 +02:00
parent 9b97114dcd
commit f01e867914
6 changed files with 56 additions and 18 deletions

View file

@ -6,6 +6,9 @@ use std::path::PathBuf;
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
pub struct Cli { pub struct Cli {
#[clap(value_parser)] #[clap(value_parser)]
pub map_file: PathBuf, pub map_file: Option<PathBuf>,
/// List devices and exit
#[clap(long, short, action)]
pub list: bool,
} }

View file

@ -55,17 +55,14 @@ impl TryFrom<DeviceConfigSerializer> for DeviceConfig {
fn try_from(v: DeviceConfigSerializer) -> Result<Self, Self::Error> { fn try_from(v: DeviceConfigSerializer) -> Result<Self, Self::Error> {
Ok(DeviceConfig { Ok(DeviceConfig {
identifier: { identifier: {
if v.name.is_some() { match (v.name, v.regex, v.addr) {
Identifier::Name(v.name.unwrap()) (Some(_), Some(_), _ ) => return Err(Error::IncompatibleArgs("name","regex")),
} (Some(_), None , Some(_)) => return Err(Error::IncompatibleArgs("name","addr")),
else if v.regex.is_some() { (None , Some(_), Some(_)) => return Err(Error::IncompatibleArgs("regex","addr")),
Identifier::Regex(regex::Regex::new(&v.regex.unwrap())?) (Some(n), None, None ) => Identifier::Name(n),
} (None, Some(r), None ) => Identifier::Regex(regex::Regex::new(&r)?),
else if v.addr.is_some() { (None, None , Some(a)) => Identifier::Addr(a),
Identifier::Addr(v.addr.unwrap()) (None, None, None ) => Identifier::All,
}
else {
Identifier::All
} }
}, },
max_connections: v.max_connections, max_connections: v.max_connections,

View file

@ -31,6 +31,10 @@ pub enum Error {
RemapTooBig(f64), RemapTooBig(f64),
#[error("remap value is too low. Minimum value is {}", i64::MIN)] #[error("remap value is too low. Minimum value is {}", i64::MIN)]
RemapTooLow(f64), RemapTooLow(f64),
#[error("'{0}' and '{1}' are incompatible")]
IncompatibleArgs(&'static str, &'static str),
#[error("no map file was provided")]
NoArgument,
#[error("pipe error")] #[error("pipe error")]
Pipe, Pipe,
#[error("unknown error")] #[error("unknown error")]

View file

@ -21,13 +21,29 @@ use cli::Cli;
fn main() { fn main() {
let c = Cli::parse(); let c = Cli::parse();
if c.list {
err_handle(run::list_devices());
return;
}
let map_file = err_handle(
c.map_file.ok_or(Error::NoArgument)
);
loop { loop {
match run_file(&c.map_file) { err_handle(
Ok(_) => (), run_file(&map_file)
Err(err) => { );
eprintln!("Error: {}", err); }
std::process::exit(1); }
}
fn err_handle<T,E>(r: Result<T, E>) -> T
where
E: std::fmt::Display
{
match r {
Ok(v) => v,
Err(err) => {
eprintln!("Error: {}", err);
std::process::exit(1);
} }
} }
} }

View file

@ -45,6 +45,15 @@ pub enum MidiPortHandler {
ALSA(MidiPort<alsa::DeviceAddr>), ALSA(MidiPort<alsa::DeviceAddr>),
} }
impl std::fmt::Display for MidiPortHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MidiPortHandler::ALSA(p) => write!(f, "{} {}:{}", p.name, p.addr.client, p.addr.port),
_ => todo!(),
}
}
}
pub enum MidiHandler { pub enum MidiHandler {
ALSA(MidiInputAlsa), ALSA(MidiInputAlsa),
} }

View file

@ -24,6 +24,15 @@ pub fn cross_shell(cmd: &str) -> Vec<String> {
).collect() ).collect()
} }
pub fn list_devices() -> Result<(), Error> {
let input = MidiHandler::new("rmidimap")?;
let ports = input.ports()?;
for p in ports {
println!("{}", p);
}
Ok(())
}
pub fn run_config(conf: &Config) -> Result<(), Error> { pub fn run_config(conf: &Config) -> Result<(), Error> {
let cfevmap: Vec<DeviceRunItem> = conf.devices.iter().map(|x| let cfevmap: Vec<DeviceRunItem> = conf.devices.iter().map(|x|
(x, EventMap::from(x), (x, EventMap::from(x),