Adds some cli & Error handling
This commit is contained in:
parent
670b1540c1
commit
d094b29157
|
|
@ -19,3 +19,6 @@ Cargo.lock
|
|||
# Added by cargo
|
||||
|
||||
/target
|
||||
|
||||
# Tarpaulin
|
||||
tarpaulin-report.html
|
||||
|
|
|
|||
|
|
@ -13,10 +13,11 @@ reqwest = { version = "0.11.16", features = ["blocking"] }
|
|||
base64 = "0.21.0"
|
||||
lazy_static = "1.4.0"
|
||||
ratatui = "0.29.0"
|
||||
serde = "1.0.215"
|
||||
serde = { version = "1.0.215", features = ["derive", "alloc"] }
|
||||
thiserror = "2.0.4"
|
||||
tracing = "0.1.41"
|
||||
tokio = "1.42.0"
|
||||
ron = "0.8.1"
|
||||
|
||||
[[bin]]
|
||||
name = "rusty_midicommander"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
# RustyMIDICommander
|
||||
  
|
||||
|
||||
Rusty MIDI Commander is a lightweight and flexible Rust program for sending asynchronous commands in response to MIDI input.
|
||||
|
||||
|
|
@ -10,4 +11,4 @@ Rusty MIDI Commander makes it easy to build powerful and responsive MIDI control
|
|||
|
||||
# Installation
|
||||
Depends on:
|
||||
- `librust-alsa-dev`
|
||||
- `librust-alsa-sys-dev`
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="116" height="18" role="img" aria-label="test-coverage: 0%"><title>test-coverage: 0%</title><linearGradient id="bms-1031a6650666d425" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-1031a6650666d425"><rect width="116" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-1031a6650666d425)"><rect width="87" height="18" fill="#555"/><rect x="87" width="29" height="18" fill="#e05d44"/><rect width="116" height="18" fill="url(#bms-1031a6650666d425)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="445" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="770">test-coverage</text><text x="445" y="130" transform="scale(.1)" fill="#fff" textLength="770">test-coverage</text><text aria-hidden="true" x="1005" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="190">0%</text><text x="1005" y="130" transform="scale(.1)" fill="#fff" textLength="190">0%</text></g></svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="82" height="18" role="img" aria-label="test: passing"><title>test: passing</title><linearGradient id="bms-5402c39dce014bd3" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-5402c39dce014bd3"><rect width="82" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-5402c39dce014bd3)"><rect width="31" height="18" fill="#555"/><rect x="31" width="51" height="18" fill="#97ca00"/><rect width="82" height="18" fill="url(#bms-5402c39dce014bd3)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="165" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="210">test</text><text x="165" y="130" transform="scale(.1)" fill="#fff" textLength="210">test</text><text aria-hidden="true" x="555" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="410">passing</text><text x="555" y="130" transform="scale(.1)" fill="#fff" textLength="410">passing</text></g></svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="90" height="18" role="img" aria-label="version: 0.1.0"><title>version: 0.1.0</title><linearGradient id="bms-0431e50b7af7883f" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-0431e50b7af7883f"><rect width="90" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-0431e50b7af7883f)"><rect width="51" height="18" fill="#555"/><rect x="51" width="39" height="18" fill="#fe7d37"/><rect width="90" height="18" fill="url(#bms-0431e50b7af7883f)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="265" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="410">version</text><text x="265" y="130" transform="scale(.1)" fill="#fff" textLength="410">version</text><text aria-hidden="true" x="695" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="290">0.1.0</text><text x="695" y="130" transform="scale(.1)" fill="#fff" textLength="290">0.1.0</text></g></svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
|
|
@ -0,0 +1,83 @@
|
|||
//! CLI & config processing.
|
||||
//!
|
||||
//! Processes all arguments given by the CLI invocation via clap
|
||||
//! The usage of config will provide if used a way to have default values for all arguments
|
||||
|
||||
use std::{
|
||||
fs::{read_to_string, File},
|
||||
io::Write,
|
||||
};
|
||||
|
||||
use clap::Parser;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
use crate::RustyMidiError;
|
||||
|
||||
#[derive(Parser, Debug, Serialize, Deserialize, Default)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
/// A struct representing command line arguments for a CLI.
|
||||
pub struct Args {
|
||||
/// The midi interface to select
|
||||
pub midi_interface: Option<usize>,
|
||||
|
||||
/// Natural timeout delay
|
||||
pub delay: Option<usize>,
|
||||
|
||||
/// Config, holding another subset of values
|
||||
pub config: Option<String>,
|
||||
|
||||
/// Create a config file from all the given arguments
|
||||
pub save: Option<String>,
|
||||
}
|
||||
|
||||
/// Get a parsed Args from the Cli, this function will also perform the basic (save/load) and printing needed
|
||||
pub fn get_arguments_from_cli() -> Result<Args, RustyMidiError> {
|
||||
debug!("Loading CLI arguments");
|
||||
let cli = parse_cli_and_load()?;
|
||||
if let Some(save_path) = &cli.save {
|
||||
info!("Saving config as {}", save_path);
|
||||
File::create(save_path)?.write_all(
|
||||
ron::ser::to_string_pretty(&cli, ron::ser::PrettyConfig::default())?.as_bytes(),
|
||||
)?;
|
||||
}
|
||||
Ok(cli)
|
||||
}
|
||||
|
||||
/// Parse all cli arguments and loads a config from file if needed
|
||||
fn parse_cli_and_load() -> Result<Args, RustyMidiError> {
|
||||
let args_og = Args::parse();
|
||||
if let Some(path) = &args_og.config {
|
||||
match load_from_disk(path) {
|
||||
Ok(args_new) => Ok(update_non_default(args_og, args_new)),
|
||||
Err(e) => {
|
||||
warn!("The file {} coudln't be loaded: {}", path, e);
|
||||
Ok(args_og)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Ok(args_og)
|
||||
}
|
||||
}
|
||||
|
||||
/// Load a given config from disk and return it as a Config
|
||||
fn load_from_disk(path: &str) -> Result<Args, RustyMidiError> {
|
||||
Ok(ron::from_str(&read_to_string(path)?)?)
|
||||
}
|
||||
|
||||
/// Update a parameter
|
||||
macro_rules! update {
|
||||
($args_og:ident, $new_args:ident, $param:ident) => {
|
||||
$args_og.$param.or($new_args.$param)
|
||||
};
|
||||
}
|
||||
|
||||
/// Update all None parameters withthe config loaded ones
|
||||
fn update_non_default(args_og: Args, args_new: Args) -> Args {
|
||||
Args {
|
||||
midi_interface: update!(args_og, args_new, midi_interface),
|
||||
delay: update!(args_og, args_new, delay),
|
||||
config: update!(args_og, args_new, config),
|
||||
save: update!(args_og, args_new, save),
|
||||
}
|
||||
}
|
||||
22
src/error.rs
22
src/error.rs
|
|
@ -1,12 +1,30 @@
|
|||
//! Generic rusty midi commander error
|
||||
//! Generic Rusty MIDI Commander error
|
||||
//!
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
/// A generic error
|
||||
/// A generic error for the Rusty Midi Commander.
|
||||
///
|
||||
/// # Example Usage
|
||||
/// ```
|
||||
/// use rmc_lib::RustyMidiError;
|
||||
///
|
||||
/// match Some(reqwest::Error::new(reqwest::StatusCode::NotFound)) {
|
||||
/// None => println!("No error"),
|
||||
/// Some(err) => return Err(RustyMidiError::NetworkRequest(err)),
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Error, Debug)]
|
||||
pub enum RustyMidiError {
|
||||
#[error("network request failed {0}")]
|
||||
NetworkRequest(#[from] reqwest::Error),
|
||||
|
||||
#[error("an error occured while accessing a file {0}")]
|
||||
IO(#[from] std::io::Error),
|
||||
|
||||
#[error("an error occured while trying to deserialize the config file {0}")]
|
||||
RonSerialization(#[from] ron::de::SpannedError),
|
||||
|
||||
#[error("an error occured while trying to serialize the config file {0}")]
|
||||
RonDeSerialization(#[from] ron::Error),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#![warn(clippy::missing_docs_in_private_items)]
|
||||
// use midir::MidiOutput;
|
||||
pub mod cli;
|
||||
pub mod error;
|
||||
pub mod midi;
|
||||
pub mod projector;
|
||||
|
|
|
|||
38
src/main.rs
38
src/main.rs
|
|
@ -1,37 +1,37 @@
|
|||
//! In this organization, src/main.rs is the entry point for the program.
|
||||
//! This file should set up the MIDI input and output devices, and then
|
||||
//! start a loop to receive and process MIDI messages.
|
||||
//! It should also use the commands module to send commands asynchronously.
|
||||
//! use crate::midi::print_midi_interfaces;
|
||||
//! This file initializes MIDI input and output devices, then continually listens to and processes MIDI messages.
|
||||
//! It employs the `commands` module to send commands asynchronously. For all input MIDI devices present on the system,
|
||||
//! it registers a command with the input interface which will be executed when a MIDI message with the specified velocity
|
||||
//! is received from that device.
|
||||
|
||||
#![warn(clippy::missing_docs_in_private_items)]
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::{
|
||||
process::exit,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use clap::Parser;
|
||||
use rmc_lib::{
|
||||
cli,
|
||||
midi::{self, cleanup, connect_loop_with_callback, create_output_interface},
|
||||
projector,
|
||||
};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
/// Description of the cli
|
||||
struct Args {
|
||||
/// The midi interface to select
|
||||
midi_interface: Option<usize>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Parse Arguments
|
||||
let args = Args::parse();
|
||||
if let Args {
|
||||
midi_interface: None,
|
||||
} = args
|
||||
{
|
||||
let args = cli::get_arguments_from_cli().unwrap_or_else(|e| {
|
||||
println!("Coudln't read the command line {}", e);
|
||||
// TODO: Test this
|
||||
cli::Args::parse_from(vec!["-h"]);
|
||||
exit(1);
|
||||
});
|
||||
|
||||
// TODO: Cleanup all the following
|
||||
if args.midi_interface.is_none() {
|
||||
midi::print_midi_interfaces();
|
||||
return;
|
||||
}
|
||||
|
||||
println!("{:?}", args);
|
||||
let proj = Arc::new(Mutex::new(projector::panasonic::Projector::new(
|
||||
"127.0.0.1:8080",
|
||||
|
|
|
|||
Reference in New Issue