Add config parsing logic

This commit is contained in:
GHOSCHT 2024-03-11 19:40:46 +01:00
parent 6245babf6c
commit 326cc61874
Signed by: ghoscht
GPG key ID: 2C2C1C62A5388E82
3 changed files with 156 additions and 53 deletions

87
Cargo.lock generated
View file

@ -167,7 +167,7 @@ dependencies = [
"fnv", "fnv",
"ident_case", "ident_case",
"proc-macro2", "proc-macro2",
"quote 1.0.33", "quote 1.0.35",
"strsim", "strsim",
"syn 1.0.109", "syn 1.0.109",
] ]
@ -179,7 +179,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
dependencies = [ dependencies = [
"darling_core", "darling_core",
"quote 1.0.33", "quote 1.0.35",
"syn 1.0.109", "syn 1.0.109",
] ]
@ -228,7 +228,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e40a16955681d469ab3da85aaa6b42ff656b3c67b52e1d8d3dd36afe97fd462" checksum = "4e40a16955681d469ab3da85aaa6b42ff656b3c67b52e1d8d3dd36afe97fd462"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.33", "quote 1.0.35",
"syn 1.0.109", "syn 1.0.109",
] ]
@ -261,7 +261,7 @@ checksum = "55a5e644a80e6d96b2b4910fa7993301d7b7926c045b475b62202b20a36ce69e"
dependencies = [ dependencies = [
"darling", "darling",
"proc-macro2", "proc-macro2",
"quote 1.0.33", "quote 1.0.35",
"syn 1.0.109", "syn 1.0.109",
] ]
@ -320,8 +320,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.33", "quote 1.0.35",
"syn 2.0.29", "syn 2.0.52",
] ]
[[package]] [[package]]
@ -391,6 +391,19 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "int-enum"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2e6a45c18bc4873fef226acaabbc8cfe1e28c6778d6de4ea58c7cf903339db0"
dependencies = [
"proc-macro2",
"proc-macro2-diagnostics",
"quote 1.0.35",
"syn 2.0.52",
"version_check",
]
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.11.0" version = "0.11.0"
@ -519,7 +532,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.33", "quote 1.0.35",
"syn 1.0.109", "syn 1.0.109",
] ]
@ -553,6 +566,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"crossbeam-channel", "crossbeam-channel",
"delay_map", "delay_map",
"int-enum",
"itertools", "itertools",
"libpulse-binding", "libpulse-binding",
"midir", "midir",
@ -581,13 +595,26 @@ checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.66" version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "proc-macro2-diagnostics"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8"
dependencies = [
"proc-macro2",
"quote 1.0.35",
"syn 2.0.52",
"version_check",
"yansi",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "0.3.15" version = "0.3.15"
@ -596,9 +623,9 @@ checksum = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.33" version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -625,8 +652,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.33", "quote 1.0.35",
"syn 2.0.29", "syn 2.0.52",
] ]
[[package]] [[package]]
@ -671,18 +698,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.33", "quote 1.0.35",
"unicode-ident", "unicode-ident",
] ]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.29" version = "2.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.33", "quote 1.0.35",
"unicode-ident", "unicode-ident",
] ]
@ -711,8 +738,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b" checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.33", "quote 1.0.35",
"syn 2.0.29", "syn 2.0.52",
] ]
[[package]] [[package]]
@ -791,6 +818,12 @@ version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" checksum = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.87" version = "0.2.87"
@ -811,8 +844,8 @@ dependencies = [
"log", "log",
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote 1.0.33", "quote 1.0.35",
"syn 2.0.29", "syn 2.0.52",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -822,7 +855,7 @@ version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d" checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
dependencies = [ dependencies = [
"quote 1.0.33", "quote 1.0.35",
"wasm-bindgen-macro-support", "wasm-bindgen-macro-support",
] ]
@ -833,8 +866,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.33", "quote 1.0.35",
"syn 2.0.29", "syn 2.0.52",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -942,3 +975,9 @@ checksum = "7c2e3184b9c4e92ad5167ca73039d0c42476302ab603e2fec4487511f38ccefc"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "yansi"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c2861d76f58ec8fc95708b9b1e417f7b12fd72ad33c01fa6886707092dea0d3"

View file

@ -6,6 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
crossbeam-channel = "0.5.8" crossbeam-channel = "0.5.8"
delay_map = "0.3.0" delay_map = "0.3.0"
int-enum = "1.0.1"
itertools = "0.11.0" itertools = "0.11.0"
libpulse-binding = "2.28.1" libpulse-binding = "2.28.1"
midir = "0.9.1" midir = "0.9.1"

View file

@ -1,65 +1,128 @@
use int_enum::IntEnum;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, fmt::Debug}; use std::path::Path;
use std::process;
use std::{collections::BTreeMap, fmt::Debug, fs};
use toml::ser::Error; use toml::ser::Error;
pub fn default() -> Result<String, Error> { pub fn init(path: &str) -> Config {
let mut applications = BTreeMap::new(); if !Path::new(path).exists() {
applications.insert("Feishin".to_string(), "Feishin".to_string()); write_default(path);
applications.insert("Librewolf".to_string(), "Librewolf".to_string()); }
load(path)
}
fn serialize_default() -> Result<String, Error> {
let mut mpris = BTreeMap::new();
let mut keymap = BTreeMap::new(); let mut keymap = BTreeMap::new();
keymap.insert( keymap.insert(
NanoKeys::Param4Mute, NanoKeys::Param4Mute,
"program-mute/Feishin,Librewolf".to_string(), vec![
KeyMapVariant::Mpris {
ids: vec!["Feishin".to_string()],
action: MprisAction::PlayPause,
},
KeyMapVariant::PipeWire {
ids: vec![
"alsa_output.usb-0d8c_Generic_USB_Audio_Device-00.iec958-stereo".to_string(),
],
},
],
); );
let mut audioinputs = BTreeMap::new(); keymap.insert(
audioinputs.insert( NanoKeys::Play,
"headphones".to_string(), vec![KeyMapVariant::Exec {
"alsa_output.usb-Burr-Brown_from_TI_USB_Audio_CODEC-00.pro-output-0".to_string(), command: "echo hello".to_string(),
}],
); );
audioinputs.insert( mpris.insert("music".to_string(), "Feishin".to_string());
"speakers".to_string(),
"alsa_output.pci-0000_0c_00.4.analog-stereo".to_string(),
);
let audiooutputs = BTreeMap::new();
let conf: Config = Config { let conf: Config = Config {
general: General { general: General {
nano_kontrol_pattern: "nanoKONTROL2".to_string(), midi_device: "nanoKONTROL2".to_string(),
led_mode: LedMode::Toggle, led_mode: LedMode::Toggle,
}, },
applications,
keymap, keymap,
audioinputs,
audiooutputs,
}; };
toml::to_string(&conf) toml::to_string(&conf)
} }
fn deserialize(path: &str) -> Result<Config, toml::de::Error> {
let cfg_str = fs::read_to_string(path).unwrap();
toml::from_str::<Config>(&cfg_str)
}
pub fn load(path: &str) -> Config {
let cfg = deserialize(path);
match cfg {
Ok(expr) => expr,
Err(expr) => {
eprintln!("Failed loading config at {path}: {expr}");
process::exit(1);
}
}
}
pub fn write_default(path: &str) -> () {
let cfg_toml = serialize_default();
match cfg_toml {
Ok(cfg_toml_text) => {
let _ = fs::write(path, &cfg_toml_text);
}
Err(expr) => {
eprintln!("Failed serializing default config: {expr}");
process::exit(1);
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub enum MprisAction {
Play,
Pause,
PlayPause,
Stop,
Next,
Previous,
VOLUME,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum KeyMapVariant {
Mpris {
ids: Vec<String>,
action: MprisAction,
},
PipeWire {
ids: Vec<String>,
},
Exec {
command: String,
},
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Config { pub struct Config {
pub general: General, pub general: General,
pub audioinputs: BTreeMap<String, String>, pub keymap: BTreeMap<NanoKeys, Vec<KeyMapVariant>>,
pub audiooutputs: BTreeMap<String, String>,
pub applications: BTreeMap<String, String>,
pub keymap: BTreeMap<NanoKeys, String>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct General { pub struct General {
#[serde(rename = "nanoKONTROLPattern")] pub midi_device: String,
pub nano_kontrol_pattern: String,
#[serde(rename = "ledMode")]
pub led_mode: LedMode, pub led_mode: LedMode,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum LedMode { pub enum LedMode {
Default, Default,
Toggle, Toggle,
} }
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] #[repr(u8)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")] #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, IntEnum)]
pub enum NanoKeys { pub enum NanoKeys {
TrackPrev = 58, TrackPrev = 58,
TrackNext = 59, TrackNext = 59,