From d21a57f4d5a012c08396eee27e89e150945b4f0f Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 15 Oct 2026 01:10:24 +0200 Subject: [PATCH] feat(M8.1): expose validated mixer profile views Tests: cargo test --locked -p fader-config --test schema Lint: cargo clippy --locked -p fader-config --all-targets -- -D warnings --- config/src/profile.rs | 81 ++++++++++++++++++++++++++++++++++++++++-- config/tests/schema.rs | 21 +++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/config/src/profile.rs b/config/src/profile.rs index adfbf9c..413e369 100644 --- a/config/src/profile.rs +++ b/config/src/profile.rs @@ -1,8 +1,8 @@ use std::collections::{BTreeMap, BTreeSet}; use fader_core::{ - ApplicationId, ApplicationSelector, FaderId, GroupId, MetadataField, MidiChannel, MidiData, - ProfileId, SurfaceId, + ApplicationId, ApplicationMatcher, ApplicationSelector, FaderId, GroupId, MetadataField, + MidiChannel, MidiData, ProfileId, SurfaceId, }; use serde::{Deserialize, Serialize}; @@ -290,6 +290,83 @@ impl Audio { } } +impl Application { + /// Stable application identifier from the validated profile. + #[must_use] + pub fn id(&self) -> &str { + &self.id + } + + /// User-facing application label. + #[must_use] + pub fn label(&self) -> &str { + &self.label + } + + /// Compiles this validated application's selectors for live stream matching. + pub fn matcher(&self) -> Result { + let id = parse_id::(&self.id, "application.id")?; + let selectors = self + .selectors + .iter() + .map(Selector::compile) + .collect::, _>>()?; + ApplicationMatcher::new(id, selectors) + .map_err(|error| ConfigError::validation("application.selectors", error.to_string())) + } +} + +impl Selector { + fn compile(&self) -> Result { + let field = match self.field { + SelectorField::ApplicationName => MetadataField::ApplicationName, + SelectorField::ApplicationId => MetadataField::ApplicationId, + SelectorField::ProcessBinary => MetadataField::ProcessBinary, + SelectorField::MediaName => MetadataField::MediaName, + }; + match (&self.exact, &self.regex) { + (Some(pattern), None) => ApplicationSelector::exact(field, pattern), + (None, Some(pattern)) => ApplicationSelector::regex(field, pattern), + _ => { + return Err(ConfigError::validation( + "application.selector", + "exactly one of exact or regex is required", + )); + } + } + .map_err(|error| ConfigError::validation("application.selector", error.to_string())) + } +} + +impl Group { + /// Stable group identifier from the validated profile. + #[must_use] + pub fn id(&self) -> &str { + &self.id + } + + /// User-facing group label. + #[must_use] + pub fn label(&self) -> &str { + &self.label + } + + /// Stable application IDs included in this group. + #[must_use] + pub fn members(&self) -> &[String] { + &self.members + } + + /// Group gain semantics used when planning writes. + #[must_use] + pub const fn volume_mode(&self) -> fader_core::VolumeMode { + match self.volume_mode { + VolumeMode::Absolute => fader_core::VolumeMode::Absolute, + VolumeMode::Relative => fader_core::VolumeMode::Relative, + } + } +} + fn validate_version(version: u16) -> Result<(), ConfigError> { if version == SCHEMA_VERSION { Ok(()) diff --git a/config/tests/schema.rs b/config/tests/schema.rs index 012f8cd..72d6420 100644 --- a/config/tests/schema.rs +++ b/config/tests/schema.rs @@ -1,4 +1,5 @@ use fader_config::{DeviceProfile, PersistedState, Profile, StartupPolicy}; +use fader_core::{StreamMetadata, VolumeMode}; const EXAMPLE: &str = include_str!("../../examples/profiles/desktop.toml"); const ARCHITECTURE: &str = include_str!("../../architecture.md"); @@ -26,6 +27,26 @@ fn architecture_profile_parses_and_round_trips_semantically() { assert_eq!(profile, reparsed); } +#[test] +fn validated_profile_exposes_compiled_runtime_application_and_group_views() { + let profile = Profile::parse(EXAMPLE).expect("example profile must validate"); + let music = &profile.applications()[0]; + assert_eq!(music.id(), "music"); + assert_eq!(music.label(), "Music"); + assert!( + music + .matcher() + .expect("validated selector compiles") + .matches(&StreamMetadata::new().with_application_name("Music Player")) + ); + + let media = &profile.groups()[0]; + assert_eq!(media.id(), "media"); + assert_eq!(media.label(), "Media"); + assert_eq!(media.members(), ["music", "browser"]); + assert_eq!(media.volume_mode(), VolumeMode::Absolute); +} + #[test] fn rejects_unknown_fields_versions_and_bad_ids_with_context() { let unknown = EXAMPLE.replace("schema_version = 1", "schema_version = 1\nsurprise = true");