feat(M1.3): resolve logical faders to unique streams
Rust CI fader_master passed CI.
MadGobblins/fader_master/pipeline/head This commit looks good Details

Tests: cargo test -p fader-core --test routing --locked first failed for missing Routing/Target; cargo test -p fader-core --all-targets --locked then passed (19 tests). cargo clippy -p fader-core --all-targets --locked -- -D warnings passed. Surface binding remains pending.
This commit is contained in:
Dandaedre 2026-10-05 08:15:35 +02:00
parent 0beaee783c
commit e0a309b565
3 changed files with 140 additions and 0 deletions

View File

@ -8,6 +8,7 @@
mod id;
mod matching;
mod revision;
mod routing;
mod state;
mod volume;
@ -17,5 +18,6 @@ pub use matching::{
SelectorKind, StreamHandle, StreamMetadata,
};
pub use revision::{BackendEpoch, Revision};
pub use routing::{Routing, RoutingError, Target};
pub use state::{Availability, Mute, ValueState};
pub use volume::{ChannelGainError, ChannelGains, Gain, GainError};

90
core/src/routing.rs Normal file
View File

@ -0,0 +1,90 @@
//! Validated logical targets and deterministic stream resolution.
use crate::{ApplicationId, FaderId, GroupId, StreamHandle};
use std::collections::{BTreeMap, BTreeSet};
use thiserror::Error;
/// A logical target; group nesting is deliberately not representable.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Target {
/// Every stream belonging to one configured application.
Application(ApplicationId),
/// The union of the streams belonging to member applications.
Group(GroupId),
}
/// A dangling configuration reference or unknown command target.
#[derive(Clone, Debug, Error, Eq, PartialEq)]
pub enum RoutingError {
/// Application is absent from the configured application set.
#[error("unknown application {0}")]
Application(ApplicationId),
/// Group is absent from the configured group map.
#[error("unknown group {0}")]
Group(GroupId),
/// Fader is absent from the configured fader map.
#[error("unknown fader {0}")]
Fader(FaderId),
}
/// Immutable, validated configuration graph. Live membership is supplied separately.
#[derive(Clone, Debug)]
pub struct Routing {
groups: BTreeMap<GroupId, Vec<ApplicationId>>,
faders: BTreeMap<FaderId, Target>,
}
impl Routing {
/// Validates all references before accepting the configuration graph.
pub fn new(
applications: BTreeSet<ApplicationId>,
groups: BTreeMap<GroupId, Vec<ApplicationId>>,
faders: BTreeMap<FaderId, Target>,
) -> Result<Self, RoutingError> {
for member in groups.values().flatten() {
if !applications.contains(member) {
return Err(RoutingError::Application(member.clone()));
}
}
for target in faders.values() {
match target {
Target::Application(id) if !applications.contains(id) => {
return Err(RoutingError::Application(id.clone()));
}
Target::Group(id) if !groups.contains_key(id) => {
return Err(RoutingError::Group(id.clone()));
}
_ => {}
}
}
Ok(Self { groups, faders })
}
/// Resolves one command to a deduplicated, deterministic stream set.
/// Missing live membership means offline, while unknown faders are errors.
pub fn resolve(
&self,
fader: &FaderId,
live: &BTreeMap<ApplicationId, BTreeSet<StreamHandle>>,
) -> Result<BTreeSet<StreamHandle>, RoutingError> {
let target = self
.faders
.get(fader)
.ok_or_else(|| RoutingError::Fader(fader.clone()))?;
match target {
Target::Application(id) => Ok(live.get(id).cloned().unwrap_or_default()),
Target::Group(id) => {
let members = self
.groups
.get(id)
.ok_or_else(|| RoutingError::Group(id.clone()))?;
Ok(members
.iter()
.filter_map(|member| live.get(member))
.flatten()
.copied()
.collect())
}
}
}
}

48
core/tests/routing.rs Normal file
View File

@ -0,0 +1,48 @@
use std::collections::{BTreeMap, BTreeSet};
use fader_core::{ApplicationId, BackendEpoch, FaderId, GroupId, Routing, StreamHandle, Target};
#[test]
fn overlapping_applications_resolve_once_and_offline_members_remain_valid() {
let browser = ApplicationId::new("browser").unwrap();
let music = ApplicationId::new("music").unwrap();
let group = GroupId::new("media").unwrap();
let fader = FaderId::new("master").unwrap();
let shared = StreamHandle::new(BackendEpoch::new(1), 9, 1);
let routing = Routing::new(
BTreeSet::from([browser.clone(), music.clone()]),
BTreeMap::from([(group.clone(), vec![browser.clone(), music.clone()])]),
BTreeMap::from([(fader.clone(), Target::Group(group))]),
)
.unwrap();
let mut live = BTreeMap::from([
(browser, BTreeSet::from([shared])),
(music.clone(), BTreeSet::from([shared])),
]);
assert_eq!(
routing.resolve(&fader, &live).unwrap(),
BTreeSet::from([shared])
);
live.remove(&music);
assert_eq!(
routing.resolve(&fader, &live).unwrap(),
BTreeSet::from([shared])
);
live.clear();
assert!(routing.resolve(&fader, &live).unwrap().is_empty());
}
#[test]
fn dangling_targets_are_rejected_before_routing() {
assert!(
Routing::new(
BTreeSet::new(),
BTreeMap::new(),
BTreeMap::from([(
FaderId::new("f").unwrap(),
Target::Application(ApplicationId::new("missing").unwrap())
)]),
)
.is_err()
);
}