Skip to main content

helm_schema_ir/contract/
finalized.rs

1use std::collections::{BTreeMap, BTreeSet};
2
3use super::{ContractDocument, ContractUse};
4use crate::contract_signal_builder::derive_schema_signals_from_contract_parts;
5use helm_schema_core::ContractSchemaSignals;
6
7/// Finalized contract artifact derived from one canonical normalized contract.
8///
9/// Stable inspection rows and schema-lowering signals come from the same
10/// normalized contract uses, so downstream callers do not need to re-finalize
11/// a [`super::ContractIr`] separately or hop through another wrapper type.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct FinalizedContract {
14    uses: Vec<ContractUse>,
15    schema_signals: ContractSchemaSignals,
16}
17
18impl FinalizedContract {
19    #[expect(
20        clippy::too_many_arguments,
21        reason = "each parameter is one interpreter fact channel; a struct would mirror the same fields without adding an invariant"
22    )]
23    pub(in crate::contract) fn new(
24        normalized_uses: Vec<ContractUse>,
25        type_hints: &BTreeMap<String, BTreeSet<String>>,
26        guarded_type_hints: &BTreeMap<String, BTreeSet<String>>,
27        fallback_type_hints: &BTreeMap<String, BTreeSet<String>>,
28        guarded_fallback_type_hints: &BTreeMap<String, BTreeSet<String>>,
29        shape_erased_value_paths: &BTreeSet<String>,
30        string_contract_value_paths: &BTreeSet<String>,
31        range_modes: &crate::range_modes::RangeModes,
32        values_default_sources: BTreeSet<crate::ValuesDefaultSource>,
33        values_root_overlay_prefixes: BTreeSet<String>,
34        values_program_wrappers: BTreeSet<helm_schema_core::ValuesProgramWrapper>,
35        values_program_wrapper_exclusions: BTreeSet<String>,
36        fail_conditions: &[crate::eval_effect::FailCapture],
37        dependency_values_root_fragments: &BTreeSet<String>,
38    ) -> Self {
39        let schema_signals = derive_schema_signals_from_contract_parts(
40            &normalized_uses,
41            type_hints,
42            guarded_type_hints,
43            fallback_type_hints,
44            guarded_fallback_type_hints,
45            shape_erased_value_paths,
46            string_contract_value_paths,
47            range_modes,
48            fail_conditions,
49            dependency_values_root_fragments,
50        )
51        .with_values_default_sources(values_default_sources)
52        .with_root_overlay_fail_implications(values_root_overlay_prefixes)
53        .with_values_program_wrappers(values_program_wrappers)
54        .with_values_program_wrapper_exclusions(values_program_wrapper_exclusions);
55
56        Self {
57            uses: normalized_uses,
58            schema_signals,
59        }
60    }
61
62    /// Returns normalized contract uses in stable inspection order.
63    #[must_use]
64    pub fn uses(&self) -> &[ContractUse] {
65        &self.uses
66    }
67
68    /// Returns path-local facts prepared for schema lowering.
69    #[must_use]
70    pub fn schema_signals(&self) -> &ContractSchemaSignals {
71        &self.schema_signals
72    }
73
74    /// Builds the versioned inspection document for this contract.
75    #[must_use]
76    pub fn document(&self) -> ContractDocument {
77        ContractDocument::from_contract_uses(self.uses.clone())
78    }
79
80    /// Consumes the contract and returns its schema-lowering signals.
81    #[must_use]
82    pub fn into_schema_signals(self) -> ContractSchemaSignals {
83        self.schema_signals
84    }
85}