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)]
pub struct Cli {
#[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> {
Ok(DeviceConfig {
identifier: {
if v.name.is_some() {
Identifier::Name(v.name.unwrap())
}
else if v.regex.is_some() {
Identifier::Regex(regex::Regex::new(&v.regex.unwrap())?)
}
else if v.addr.is_some() {
Identifier::Addr(v.addr.unwrap())
}
else {
Identifier::All
match (v.name, v.regex, v.addr) {
(Some(_), Some(_), _ ) => return Err(Error::IncompatibleArgs("name","regex")),
(Some(_), None , Some(_)) => return Err(Error::IncompatibleArgs("name","addr")),
(None , Some(_), Some(_)) => return Err(Error::IncompatibleArgs("regex","addr")),
(Some(n), None, None ) => Identifier::Name(n),
(None, Some(r), None ) => Identifier::Regex(regex::Regex::new(&r)?),
(None, None , Some(a)) => Identifier::Addr(a),
(None, None, None ) => Identifier::All,
}
},
max_connections: v.max_connections,

View file

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

View file

@ -21,13 +21,29 @@ use cli::Cli;
fn main() {
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 {
match run_file(&c.map_file) {
Ok(_) => (),
Err(err) => {
eprintln!("Error: {}", err);
std::process::exit(1);
}
err_handle(
run_file(&map_file)
);
}
}
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>),
}
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 {
ALSA(MidiInputAlsa),
}

View file

@ -24,6 +24,15 @@ pub fn cross_shell(cmd: &str) -> Vec<String> {
).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> {
let cfevmap: Vec<DeviceRunItem> = conf.devices.iter().map(|x|
(x, EventMap::from(x),