Skip to main content

presolve_compiler/
context_projection.rs

1//! V2 Context token and provider inspection over canonical ASM entities.
2use crate::{ContextEntity, ContextId, ProviderEntity, SemanticType, SerializationCompatibility};
3use serde::Serialize;
4pub const CONTEXT_PROJECTION_SCHEMA_VERSION: u32 = 1;
5#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
6pub struct ContextTokenRecordV1 {
7    pub context: String,
8    pub owner: String,
9    pub has_default: bool,
10    pub codec_eligible: bool,
11}
12#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
13pub struct ContextProviderRecordV1 {
14    pub provider: String,
15    pub context: String,
16    pub owner: String,
17    pub reactive_value_expression: String,
18}
19#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
20pub struct ContextProjectionV1 {
21    pub schema_version: u32,
22    pub tokens: Vec<ContextTokenRecordV1>,
23    pub providers: Vec<ContextProviderRecordV1>,
24}
25#[must_use]
26pub fn build_context_projection_v1(
27    contexts: &std::collections::BTreeMap<ContextId, ContextEntity>,
28    providers: &std::collections::BTreeMap<crate::ProviderId, ProviderEntity>,
29    types: &std::collections::BTreeMap<crate::SemanticId, SemanticType>,
30) -> ContextProjectionV1 {
31    let mut tokens = contexts
32        .values()
33        .map(|c| ContextTokenRecordV1 {
34            context: c.id.as_str().into(),
35            owner: c
36                .owner
37                .entity_id()
38                .map_or_else(String::new, ToString::to_string),
39            has_default: c.default_expression.is_some(),
40            codec_eligible: types.get(c.id.as_semantic_id()).is_some_and(|t| {
41                crate::serialization_compatibility(t) == SerializationCompatibility::Serializable
42            }),
43        })
44        .collect::<Vec<_>>();
45    tokens.sort_by(|a, b| a.context.cmp(&b.context));
46    let mut providers = providers
47        .values()
48        .map(|p| ContextProviderRecordV1 {
49            provider: p.id.as_str().into(),
50            context: p.context.as_str().into(),
51            owner: p
52                .owner
53                .entity_id()
54                .map_or_else(String::new, ToString::to_string),
55            reactive_value_expression: p.value_expression.to_string(),
56        })
57        .collect::<Vec<_>>();
58    providers.sort_by(|a, b| a.provider.cmp(&b.provider));
59    ContextProjectionV1 {
60        schema_version: CONTEXT_PROJECTION_SCHEMA_VERSION,
61        tokens,
62        providers,
63    }
64}