Skip to main content

bynk_emit/emitter/compose/
closure.rs

1//! A context's in-process closure: the units whose providers its compose would
2//! instantiate (back-half paydown 1C.1a, Decision C of #4031).
3//!
4//! The walk reads the provider graph, whose acyclicity the earlier checks
5//! establish, so it runs only on an otherwise clean program. [`super::plan`]
6//! records its results as facts (`bare_kv`, `bare_singletons`), and the Bundle
7//! build's env threading calls [`native_platforms_of_context`] directly.
8
9use std::collections::{BTreeMap, BTreeSet, HashMap};
10
11use bynk_check::firstparty::Platform;
12use bynk_check::project_model::handler_cross_caps;
13use bynk_check::symbols::UnitTable;
14
15use crate::emit_model::{
16    ProviderNamespaces, instantiate_provider_ts_expr, parse_bare_singleton_marker,
17};
18
19/// v0.19 (decision 0017): the native platforms a context's **in-process
20/// closure** commits it to: every unit whose provider its compose would
21/// instantiate (local providers' `given` recursion plus the capabilities its
22/// handlers reference), mapped through [`bynk_check::firstparty::platform_of`].
23/// Each platform carries an exemplar unit for the diagnostic message. Service
24/// `consumes` edges (RPC under `workers`) do not contribute: only the
25/// provider-instantiation walk, which is in-process by construction.
26#[allow(clippy::too_many_arguments)]
27pub fn native_platforms_of_context(
28    ctx: &str,
29    table: &UnitTable,
30    unit_tables: &HashMap<String, UnitTable>,
31    unit_consumes: &HashMap<String, Vec<String>>,
32    unit_consumes_aliases: &HashMap<String, HashMap<String, String>>,
33    unit_flattened: &HashMap<String, HashMap<String, String>>,
34    // S4a (#1966): count derived capabilities' base units. `true` for what
35    // threads `env`; `false` for whether the *bare* platform resource (the
36    // `KV` namespace) is used, which a named instance alone must not imply.
37    with_derived: bool,
38) -> BTreeMap<Platform, String> {
39    let referenced = referenced_units_of_context(
40        ctx,
41        table,
42        unit_tables,
43        unit_consumes,
44        unit_consumes_aliases,
45        unit_flattened,
46        with_derived,
47    );
48    let mut out = BTreeMap::new();
49    for unit in referenced {
50        if let Some(p) = bynk_check::firstparty::platform_of(&unit) {
51            out.entry(p).or_insert(unit);
52        }
53    }
54    out
55}
56
57/// #3300: the one-per-Worker capabilities (`Version`) context `ctx`'s closure
58/// reaches bare, as `(unit, capability)`, in order: the bare-resources walk's
59/// [`crate::emit_model::bare_singleton_marker`]s.
60pub fn bare_singletons_of_context(
61    ctx: &str,
62    table: &UnitTable,
63    unit_tables: &HashMap<String, UnitTable>,
64    unit_consumes: &HashMap<String, Vec<String>>,
65    unit_consumes_aliases: &HashMap<String, HashMap<String, String>>,
66    unit_flattened: &HashMap<String, HashMap<String, String>>,
67) -> Vec<(String, String)> {
68    referenced_units_of_context(
69        ctx,
70        table,
71        unit_tables,
72        unit_consumes,
73        unit_consumes_aliases,
74        unit_flattened,
75        false,
76    )
77    .iter()
78    .filter_map(|entry| {
79        parse_bare_singleton_marker(entry).map(|(u, c)| (u.to_string(), c.to_string()))
80    })
81    .collect()
82}
83
84/// The units (and, on the bare walk, bare-singleton markers) context `ctx`'s
85/// providers and cross-context capabilities instantiate.
86fn referenced_units_of_context(
87    ctx: &str,
88    table: &UnitTable,
89    unit_tables: &HashMap<String, UnitTable>,
90    unit_consumes: &HashMap<String, Vec<String>>,
91    unit_consumes_aliases: &HashMap<String, HashMap<String, String>>,
92    unit_flattened: &HashMap<String, HashMap<String, String>>,
93    with_derived: bool,
94) -> BTreeSet<String> {
95    // Arc F slice 2 (#1452): repointed at the tree-native twin; only the
96    // `referenced` side effect matters here, the returned `TsExpr` is
97    // discarded.
98    let mut referenced: BTreeSet<String> = BTreeSet::new();
99    let namespaces = if with_derived {
100        ProviderNamespaces::Bundle
101    } else {
102        ProviderNamespaces::BareResources
103    };
104    for cap in table.providers.keys() {
105        let _ = instantiate_provider_ts_expr(
106            ctx,
107            cap,
108            unit_tables,
109            unit_consumes,
110            unit_consumes_aliases,
111            unit_flattened,
112            namespaces,
113            None,
114            None,
115            &mut referenced,
116        );
117    }
118    let consumed = unit_consumes.get(ctx).cloned().unwrap_or_default();
119    let aliases = unit_consumes_aliases.get(ctx).cloned().unwrap_or_default();
120    let flattened = unit_flattened.get(ctx).cloned().unwrap_or_default();
121    for (key, cctx) in handler_cross_caps(table, ctx, &consumed, &aliases, &flattened) {
122        let _ = instantiate_provider_ts_expr(
123            &cctx,
124            &key,
125            unit_tables,
126            unit_consumes,
127            unit_consumes_aliases,
128            unit_flattened,
129            namespaces,
130            None,
131            None,
132            &mut referenced,
133        );
134    }
135    if with_derived {
136        referenced.extend(table.derived.values().map(|d| d.base_unit.clone()));
137    }
138    referenced
139}