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
This commit is contained in:
Dandaedre 2026-10-15 02:35:49 +02:00
parent 6004d8cf8d
commit ff2e39e135
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
3 changed files with 130 additions and 0 deletions

View File

@ -111,6 +111,16 @@ pub struct Surface {
input_port: String,
#[serde(skip_serializing_if = "Option::is_none")]
output_port: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
usb_vendor_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
usb_product_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
usb_serial: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
usb_path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
manual_backend_id: Option<String>,
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::<FaderId, _>(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,

View File

@ -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 [

View File

@ -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