Skip to main content

tilo_actions/
row_status.rs

1//! What a published row's actions are like now: a service or a surface
2//! says of one row's state (a process that cannot be killed, a window only a
3//! foreign reader may look at) what its published spec carries.
4
5use crate::{Availability, ResourceSpec};
6
7/// One published row's action as its publisher says it stands now: a
8/// process that cannot be killed, a window only a foreign reader may look at.
9/// Applied to the row's published spec with [`apply_row_statuses`].
10#[derive(Clone, Debug, PartialEq)]
11pub struct RowStatus {
12    /// The id of the row's action, as it publishes it.
13    pub action: String,
14    /// What it is like now: unavailable, with why.
15    pub availability: Availability,
16}
17
18/// Lay `statuses` over `resources` and every child, each row's actions by
19/// id: what a service or surface publishes about one row of its state, the
20/// same shape the app framework's `Publishes::row_status` gives. A status
21/// naming an action the row does not offer changes nothing.
22pub fn apply_row_statuses(
23    resources: &mut [ResourceSpec],
24    mut statuses: impl FnMut(&str) -> Vec<RowStatus>,
25) {
26    fn walk(resource: &mut ResourceSpec, statuses: &mut impl FnMut(&str) -> Vec<RowStatus>) {
27        for status in statuses(resource.id.as_str()) {
28            for spec in resource
29                .actions
30                .iter_mut()
31                .filter(|spec| spec.id == status.action)
32            {
33                spec.type_mut().contract.availability = status.availability.clone();
34            }
35        }
36        for child in &mut resource.children {
37            walk(child, statuses);
38        }
39    }
40    for resource in resources {
41        walk(resource, &mut statuses);
42    }
43}