From ff2e39e1356d2e0b38346344110d43deeb5b7e0e Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 15 Oct 2026 02:35:49 +0200 Subject: [PATCH] feat(M5.3): validate MIDI surface identity hints Tests: cargo test --locked -p fader-config --test schema Lint: cargo clippy --locked -p fader-config --all-targets -- -D warnings --- config/src/profile.rs | 103 +++++++++++++++++++++++++++ config/tests/schema.rs | 17 +++++ examples/profiles/arch-pipewire.toml | 10 +++ 3 files changed, 130 insertions(+) diff --git a/config/src/profile.rs b/config/src/profile.rs index 413e369..ec3f233 100644 --- a/config/src/profile.rs +++ b/config/src/profile.rs @@ -111,6 +111,16 @@ pub struct Surface { input_port: String, #[serde(skip_serializing_if = "Option::is_none")] output_port: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + usb_vendor_id: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + usb_product_id: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + usb_serial: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + usb_path: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + manual_backend_id: Option, identity_policy: IdentityPolicy, motorized: bool, } @@ -259,6 +269,11 @@ impl Profile { &self.raw.groups } + /// Configured MIDI surfaces in declared order. + pub fn surfaces(&self) -> &[Surface] { + &self.raw.surfaces + } + /// Validated bindings. pub fn bindings(&self) -> &[Binding] { &self.raw.bindings @@ -367,6 +382,58 @@ impl Group { } } +impl Surface { + /// Stable user-assigned surface identifier. + #[must_use] + pub fn id(&self) -> &str { + &self.id + } + + /// User-facing surface label. + #[must_use] + pub fn label(&self) -> &str { + &self.label + } + + /// Stable portion of the expected MIDI input-port name. + #[must_use] + pub fn input_port(&self) -> &str { + &self.input_port + } + + /// Optional independently assigned output-port name. + #[must_use] + pub fn output_port(&self) -> Option<&str> { + self.output_port.as_deref() + } + + #[must_use] + pub fn usb_vendor_id(&self) -> Option<&str> { + self.usb_vendor_id.as_deref() + } + + #[must_use] + pub fn usb_product_id(&self) -> Option<&str> { + self.usb_product_id.as_deref() + } + + #[must_use] + pub fn usb_serial(&self) -> Option<&str> { + self.usb_serial.as_deref() + } + + #[must_use] + pub fn usb_path(&self) -> Option<&str> { + self.usb_path.as_deref() + } + + /// Explicit current backend selection used only to resolve ambiguity. + #[must_use] + pub fn manual_backend_id(&self) -> Option<&str> { + self.manual_backend_id.as_deref() + } +} + fn validate_version(version: u16) -> Result<(), ConfigError> { if version == SCHEMA_VERSION { Ok(()) @@ -459,6 +526,31 @@ fn validate_profile_members(raw: &RawProfile) -> Result<(), ConfigError> { "a motorized surface requires an output port", )); } + match (&surface.usb_vendor_id, &surface.usb_product_id) { + (Some(vendor), Some(product)) => { + validate_usb_id(vendor, &format!("surfaces[{index}].usb_vendor_id"))?; + validate_usb_id(product, &format!("surfaces[{index}].usb_product_id"))?; + } + (None, None) => {} + _ => { + return Err(ConfigError::validation( + format!("surfaces[{index}].usb_vendor_id"), + "USB vendor_id and product_id must be provided together", + )); + } + } + for (field, value) in [ + ("usb_serial", surface.usb_serial.as_deref()), + ("usb_path", surface.usb_path.as_deref()), + ("manual_backend_id", surface.manual_backend_id.as_deref()), + ] { + if value.is_some_and(str::is_empty) { + return Err(ConfigError::validation( + format!("surfaces[{index}].{field}"), + "must not be empty", + )); + } + } } let faders = unique_ids::(raw.faders.iter().map(|item| item.id.as_str()), "faders")?; @@ -483,6 +575,17 @@ fn validate_profile_members(raw: &RawProfile) -> Result<(), ConfigError> { validate_bindings(raw, &surfaces, &faders) } +fn validate_usb_id(value: &str, path: &str) -> Result<(), ConfigError> { + if value.len() == 4 && value.bytes().all(|byte| byte.is_ascii_hexdigit()) { + Ok(()) + } else { + Err(ConfigError::validation( + path, + "must contain exactly four hexadecimal digits", + )) + } +} + fn validate_selector( selector: &Selector, application: usize, diff --git a/config/tests/schema.rs b/config/tests/schema.rs index 4e741d8..fe602c3 100644 --- a/config/tests/schema.rs +++ b/config/tests/schema.rs @@ -73,6 +73,13 @@ fn arch_pipewire_profile_matches_reported_qobuz_metadata() { .with_process_binary("waterfox-bin") ) ); + + let surface = &profile.surfaces()[0]; + assert_eq!(surface.id(), "apc-mini"); + assert_eq!(surface.input_port(), "APC MINI:APC MINI MIDI 1"); + assert_eq!(surface.usb_vendor_id(), Some("09e8")); + assert_eq!(surface.usb_product_id(), Some("0028")); + assert_eq!(surface.usb_serial(), Some("Ver00.10")); } #[test] @@ -105,6 +112,16 @@ fn rejects_dangling_references_and_duplicate_physical_bindings() { assert_error_contains(Profile::parse(&duplicate), "bindings[1].message"); } +#[test] +fn rejects_incomplete_or_malformed_usb_surface_hints() { + let missing_product = ARCH_PIPEWIRE.replace("usb_product_id = \"0028\"\n", ""); + assert_error_contains(Profile::parse(&missing_product), "provided together"); + + let malformed_vendor = + ARCH_PIPEWIRE.replace("usb_vendor_id = \"09e8\"", "usb_vendor_id = \"9e8\""); + assert_error_contains(Profile::parse(&malformed_vendor), "four hexadecimal digits"); +} + #[test] fn validates_all_midi_data_ranges_and_action_capabilities() { for (needle, replacement, path) in [ diff --git a/examples/profiles/arch-pipewire.toml b/examples/profiles/arch-pipewire.toml index acf2af2..cd96262 100644 --- a/examples/profiles/arch-pipewire.toml +++ b/examples/profiles/arch-pipewire.toml @@ -28,3 +28,13 @@ id = "media" label = "Media group" members = ["music", "browser"] volume_mode = "absolute" + +[[surfaces]] +id = "apc-mini" +label = "APC MINI" +input_port = "APC MINI:APC MINI MIDI 1" +usb_vendor_id = "09e8" +usb_product_id = "0028" +usb_serial = "Ver00.10" +identity_policy = "require_unique" +motorized = false