Skip to main content

weirdnet_audio_engine/
route_exec.rs

1//! The router as a local executor of a `weirdnet_audio_route` graph (feature `route`).
2
3use crate::router::{Route, RouterControl, RouterError};
4use crate::talkback::db_to_gain;
5use weirdnet_audio_route::{Executor, Graph, PortId, RouteError};
6
7impl Executor for RouterControl {
8    /// Routes every enabled connection whose two ports are endpoints of this router (the port id is the endpoint id), with the
9    /// connection's gain. The graph owns the routes between its ports: those it no longer has go, and routes with an end the graph
10    /// does not know (the router's own wiring, such as a bus to the device) stay. Connections with an end elsewhere are left for a
11    /// remote executor and listed by [`RouterControl::remote_connections`]. A refused graph leaves the router as it was.
12    fn apply(&mut self, graph: &Graph) -> Result<(), RouteError> {
13        let known = |id: &str| graph.port(&PortId::new(id)).is_some();
14        let mut routes: Vec<Route> = self.routes().iter().filter(|r| !(known(&r.from) && known(&r.to))).cloned().collect();
15        let mut skipped = Vec::new();
16        for c in graph.connections() {
17            if !c.enabled {
18                continue;
19            }
20            if self.kind(c.from.as_str()).is_some() && self.kind(c.to.as_str()).is_some() {
21                routes.push(Route { from: c.from.as_str().to_string(), to: c.to.as_str().to_string(), gain: db_to_gain(c.gain_db) });
22            } else {
23                skipped.push(c.id.0.clone());
24            }
25        }
26        self.set_routes(routes).map_err(|e| match e {
27            RouterError::Cycle(_) => RouteError::Cycle,
28            RouterError::Unknown(id) => RouteError::UnknownPort(PortId::new(&id)),
29            RouterError::NotAnOutput(_) | RouterError::NotAnInput(_) => RouteError::WrongDirection,
30            RouterError::Duplicate(..) => RouteError::Duplicate,
31            other => RouteError::Denied(other.to_string()),
32        })?;
33        self.skipped = skipped;
34        Ok(())
35    }
36}
37
38impl RouterControl {
39    /// The ids of the connections the last [`Executor::apply`] left alone because an end is not here.
40    pub fn remote_connections(&self) -> &[String] {
41        &self.skipped
42    }
43}