diff --git a/core/src/lib.rs b/core/src/lib.rs index 422dfd2..1bbc6bb 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -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}; diff --git a/core/src/routing.rs b/core/src/routing.rs new file mode 100644 index 0000000..6e28778 --- /dev/null +++ b/core/src/routing.rs @@ -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>, + faders: BTreeMap, +} + +impl Routing { + /// Validates all references before accepting the configuration graph. + pub fn new( + applications: BTreeSet, + groups: BTreeMap>, + faders: BTreeMap, + ) -> Result { + 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>, + ) -> Result, 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()) + } + } + } +} diff --git a/core/tests/routing.rs b/core/tests/routing.rs new file mode 100644 index 0000000..560a6b6 --- /dev/null +++ b/core/tests/routing.rs @@ -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() + ); +}