helm-schema-ir 0.0.7

Generate an accurate JSON schema for any helm chart
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
use std::collections::{BTreeMap, BTreeSet, HashMap};

use crate::abstract_value::AbstractValue;
use crate::bound_value_analysis::{GetBinding, GetBindingPlan};
use crate::fragment_assignment::AssignmentKind;
use crate::helper_meta::HelperOutputMeta;
use crate::scalar_value::{ScalarValueDispatch, TruthCondition};
use helm_schema_core::{Guard, Predicate};

mod branch_join;

use branch_join::{
    joined_branch_outcomes, joined_scalar_dispatch_arms, joined_truthy_reduction_arms,
};

#[derive(Clone, Debug, Default)]
pub(crate) struct SymbolicLocalState {
    pub(crate) range_domains: HashMap<String, Vec<String>>,
    pub(crate) get_bindings: HashMap<String, GetBinding>,
    pub(crate) fragment_values: HashMap<String, AbstractValue>,
    pub(crate) default_paths: HashMap<String, BTreeSet<helm_schema_core::ValuesPath>>,
    pub(crate) output_meta:
        HashMap<String, BTreeMap<helm_schema_core::ValuesPath, HelperOutputMeta>>,
    /// Runtime scalar values retained independently of fragment provenance.
    /// Branch joins guard each alternative by the branch that assigned it,
    /// so later helper conditions consume the same value semantics Helm does.
    pub(crate) scalar_dispatches: HashMap<String, ScalarValueDispatch>,
    /// Sufficient conditions under which a monotone local accumulator is
    /// nonempty. An explicit [`Predicate::False`] is the empty seed; a
    /// missing entry means the local's truthiness is not structurally known.
    pub(crate) truthy_reductions: HashMap<String, Predicate>,
    /// Locals whose branch predicate exceeded the bounded stamping budget.
    /// Their truthiness must abstain instead of falling back to the local's
    /// unguarded value paths.
    pub(crate) truthiness_abstentions: BTreeSet<String>,
    /// Locals that received a reassignment which does not imply truthiness
    /// (an explicit falsy write, or a condition-valued write). Such a write
    /// makes the accumulator last-write-wins instead of monotone, so a
    /// range exit must drop the local's reduction rather than read it
    /// existentially. Straight-line `if`/`else` joins stay exact and keep
    /// consuming the reduction. A declaration seeds a fresh accumulator and
    /// resets the mark.
    pub(crate) truthiness_clears: BTreeSet<String>,
    /// Values paths defaulted by structural `set X "K" (X.K | default V)`
    /// helper mutations that have already run in source order.
    pub(crate) chart_value_defaults: BTreeSet<helm_schema_core::ValuesPath>,
    /// Locals bound to a type descriptor. Each described path retains the
    /// predicates under which that path supplied the selected value.
    pub(crate) typeof_sources:
        HashMap<String, BTreeMap<helm_schema_core::ValuesPath, HelperOutputMeta>>,
    /// Locals bound to a total integer cast of one values path
    /// (`$replicas := int (default 1 .Values.controller.replicas)`):
    /// comparisons on the local may strengthen through the raw-integer
    /// sound subsets exactly as the inline cast expression would.
    pub(crate) int_cast_sources: HashMap<String, IntCastSource>,
    /// Range variables bound to the MEMBER identity of a directly ranged
    /// path (`$v` in `range $k, $v := .Values.x` holds each `x.*` value).
    /// Conditions and assignments resolve through these; hole rendering
    /// does not, so member reads do not manufacture placed rows.
    pub(crate) range_member_values: HashMap<String, AbstractValue>,
    /// For a range variable bound over a local-dict OVERLAY (`$services :=
    /// .Values.service.additionalServices` + `set $services "default"
    /// (omit …)`): one literal entry the range DEFINITELY iterates on
    /// every render. Unfaithful member conditions re-decode under this
    /// binding as a sound subset (positive-polarity consumers only).
    pub(crate) definite_range_member_values: HashMap<String, AbstractValue>,
    /// Locals whose CURRENT value came from a guarded self-advance
    /// (`$x = index $x $k` under a `hasKey $x $k` presence conjunct): one
    /// traversal step into a member. The branch join keeps the advanced
    /// (deepest) value instead of widening to a choice — facts derived
    /// from the advanced identity carry its presence guard, which only
    /// holds at runtime when the advance really happened.
    pub(crate) traversal_advances: BTreeSet<String>,
    local_scopes: Vec<LocalScopeFrame>,
}

/// The values path (and optional literal-integer `default` fallback) behind
/// a local's `int`/`int64` cast binding. The fallback matters for
/// soundness: a raw `0` at the path is numerically empty, so `default`
/// substitutes the literal before the comparison runs.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct IntCastSource {
    pub(crate) path: helm_schema_core::ValuesPath,
    pub(crate) default_int: Option<i64>,
}

#[derive(Clone, Debug, Default)]
struct LocalScopeFrame {
    previous_values: HashMap<String, VariableLocalState>,
}

#[derive(Clone, Debug, Default)]
struct VariableLocalState {
    range_domain: Option<Vec<String>>,
    get_binding: Option<GetBinding>,
    fragment_value: Option<AbstractValue>,
    traversal_advanced: bool,
    default_paths: Option<BTreeSet<helm_schema_core::ValuesPath>>,
    output_meta: Option<BTreeMap<helm_schema_core::ValuesPath, HelperOutputMeta>>,
    scalar_dispatch: Option<ScalarValueDispatch>,
    truthy_reduction: Option<Predicate>,
    truthiness_abstained: bool,
    truthiness_cleared: bool,
    typeof_source: Option<BTreeMap<helm_schema_core::ValuesPath, HelperOutputMeta>>,
    int_cast_source: Option<IntCastSource>,
    range_member_value: Option<AbstractValue>,
    definite_range_member_value: Option<AbstractValue>,
}

impl SymbolicLocalState {
    pub(crate) fn join_branch_outcomes(&mut self, entry: &Self, outcomes: &[Self]) {
        *self = joined_branch_outcomes(entry, outcomes);
    }

    /// Rebuild scalar locals from the mutually exclusive arms of one exact
    /// `if` chain. Ordinary local facts join by equality or value choice;
    /// scalar values retain the condition that selected each reassignment.
    pub(crate) fn join_scalar_dispatch_arms(
        &mut self,
        entry: &Self,
        arms: &[(TruthCondition, Self)],
        has_unconditional_else: bool,
    ) {
        if let Some(joined) = joined_scalar_dispatch_arms(entry, arms, has_unconditional_else) {
            self.scalar_dispatches = joined;
        }
    }

    /// Rebuild local truthiness from the mutually exclusive arms of one
    /// exact `if` chain. Every arm, including the implicit fallthrough,
    /// contributes its selection condition so an untouched entry reduction
    /// cannot erase a kill-switch reassignment at the union.
    pub(crate) fn join_truthy_reduction_arms(
        &mut self,
        entry: &Self,
        arms: &[(TruthCondition, Self)],
        has_unconditional_else: bool,
    ) {
        if let Some(joined) = joined_truthy_reduction_arms(entry, arms, has_unconditional_else) {
            self.truthy_reductions.extend(joined);
        }
    }

    /// Conjoin `condition` onto every truthiness reduction this branch
    /// CHANGED relative to `entry`: the reassigned truthiness holds only
    /// where the branch ran, so the cross-branch union becomes the exact
    /// disjunction of guarded arms (the range-sentinel flag pattern).
    ///
    /// Bounded on both sides: an approximate arm condition would only
    /// poison every consumer into abstention, and unbounded conjoining at
    /// nested joins grows reductions combinatorially, so oversized results
    /// drop the reduction instead of letting an unstamped branch claim
    /// escape through a later join.
    pub(crate) fn conjoin_changed_truthy_reductions(
        &mut self,
        entry: &Self,
        condition: &Predicate,
    ) {
        const MAX_STAMPED_GUARDS: usize = 6;
        if matches!(condition, Predicate::True) || condition.contains_approximation() {
            return;
        }
        // A range exit reads each reduction existentially ("some iteration
        // made the accumulator truthy"), which is exact only for monotone
        // accumulators. A falsy-capable reassignment inside the body makes
        // the final value last-write-wins, so the reduction abstains
        // instead of quantifying: `[{enabled: true}, {enabled: false}]`
        // ends the sentinel falsy while a member still satisfies ∃.
        if matches!(
            single_term(condition),
            Some(Predicate::Guard(Guard::Range { .. }))
        ) {
            let clears = &self.truthiness_clears;
            self.truthy_reductions
                .retain(|variable, _| !clears.contains(variable));
        }
        let mut dropped = Vec::new();
        self.truthy_reductions.retain(|variable, reduction| {
            let entry_reduction = entry.truthy_reductions.get(variable);
            if entry_reduction == Some(reduction)
                || matches!(reduction, Predicate::False)
                || reduction.contains_approximation()
            {
                return true;
            }
            if predicate_guard_count(condition) + predicate_guard_count(reduction)
                <= MAX_STAMPED_GUARDS
            {
                *reduction = quantify_range_member_reduction(condition, reduction)
                    .map(Predicate::from)
                    .unwrap_or_else(|| Predicate::all(vec![condition.clone(), reduction.clone()]));
                return true;
            }
            let changed = entry_reduction
                .and_then(|entry| changed_truthy_reduction(entry, reduction))
                .unwrap_or_else(|| reduction.clone());
            if predicate_implies(&changed, condition) {
                return true;
            }
            let stamped =
                if let Some(quantified) = quantify_range_member_reduction(condition, &changed) {
                    Predicate::from(quantified)
                } else {
                    if predicate_guard_count(condition) + predicate_guard_count(&changed)
                        > MAX_STAMPED_GUARDS
                    {
                        dropped.push(variable.clone());
                        return false;
                    }
                    Predicate::all(vec![condition.clone(), changed])
                };
            if predicate_guard_count(&stamped) > MAX_STAMPED_GUARDS {
                dropped.push(variable.clone());
                return false;
            }
            *reduction = entry_reduction.map_or(stamped.clone(), |entry| {
                union_truthy_reductions(entry, &stamped)
            });
            true
        });
        for variable in dropped {
            self.truthiness_abstentions.insert(variable);
        }
    }

    pub(crate) fn enter_local_scope(&mut self) {
        self.local_scopes.push(LocalScopeFrame::default());
    }

    pub(crate) fn exit_local_scope(&mut self) {
        let Some(scope) = self.local_scopes.pop() else {
            return;
        };
        for (variable, previous) in scope.previous_values {
            self.restore_variable_state(&variable, previous);
        }
    }

    pub(crate) fn apply_get_binding(&mut self, plan: GetBindingPlan) {
        self.record_binding_shadow(plan.kind, &plan.variable);
        self.set_get_binding(plan.variable, plan.binding);
    }

    pub(crate) fn bind_fragment_value(
        &mut self,
        kind: AssignmentKind,
        variable: String,
        binding: Option<AbstractValue>,
    ) {
        self.record_binding_shadow(kind, &variable);
        self.set_fragment_value(variable, binding);
    }

    pub(crate) fn mark_traversal_advance(&mut self, variable: &str) {
        self.traversal_advances.insert(variable.to_string());
    }

    pub(crate) fn insert_range_domain(&mut self, variable: String, literals: Vec<String>) {
        self.record_scope_shadow(&variable);
        self.clear_variable(&variable);
        self.range_domains.insert(variable, literals);
    }

    pub(crate) fn set_chart_value_defaults(
        &mut self,
        defaults: BTreeSet<helm_schema_core::ValuesPath>,
    ) {
        self.chart_value_defaults = defaults;
    }

    pub(crate) fn append_chart_value_defaults(
        &mut self,
        defaults: &mut BTreeSet<helm_schema_core::ValuesPath>,
    ) {
        self.chart_value_defaults.append(defaults);
    }

    /// Record the pre-write state of `variable` into the current scope frame
    /// so `exit_local_scope` restores it. `:=` always shadows; `=` writes
    /// through to the existing binding (the write survives scope exit), so it
    /// shadows only when the variable has no current value — Go templates
    /// treat that as a fresh declaration.
    fn record_binding_shadow(&mut self, kind: AssignmentKind, variable: &str) {
        if matches!(kind, AssignmentKind::Assignment) && self.variable_has_current_value(variable) {
            return;
        }
        self.record_scope_shadow(variable);
    }

    fn record_scope_shadow(&mut self, variable: &str) {
        let previous = self.variable_state(variable);
        let Some(scope) = self.local_scopes.last_mut() else {
            return;
        };
        scope
            .previous_values
            .entry(variable.to_string())
            .or_insert(previous);
    }

    fn variable_state(&self, variable: &str) -> VariableLocalState {
        VariableLocalState {
            range_domain: self.range_domains.get(variable).cloned(),
            get_binding: self.get_bindings.get(variable).cloned(),
            fragment_value: self.fragment_values.get(variable).cloned(),
            traversal_advanced: self.traversal_advances.contains(variable),
            default_paths: self.default_paths.get(variable).cloned(),
            output_meta: self.output_meta.get(variable).cloned(),
            scalar_dispatch: self.scalar_dispatches.get(variable).cloned(),
            truthy_reduction: self.truthy_reductions.get(variable).cloned(),
            truthiness_abstained: self.truthiness_abstentions.contains(variable),
            truthiness_cleared: self.truthiness_clears.contains(variable),
            typeof_source: self.typeof_sources.get(variable).cloned(),
            int_cast_source: self.int_cast_sources.get(variable).cloned(),
            range_member_value: self.range_member_values.get(variable).cloned(),
            definite_range_member_value: self.definite_range_member_values.get(variable).cloned(),
        }
    }

    fn variable_has_current_value(&self, variable: &str) -> bool {
        self.range_domains.contains_key(variable)
            || self.get_bindings.contains_key(variable)
            || self.fragment_values.contains_key(variable)
            || self.default_paths.contains_key(variable)
            || self.output_meta.contains_key(variable)
            || self.scalar_dispatches.contains_key(variable)
            || self.truthy_reductions.contains_key(variable)
            || self.truthiness_abstentions.contains(variable)
            || self.typeof_sources.contains_key(variable)
            || self.int_cast_sources.contains_key(variable)
            || self.range_member_values.contains_key(variable)
    }

    fn set_get_binding(&mut self, variable: String, binding: GetBinding) {
        self.clear_variable(&variable);
        self.get_bindings.insert(variable, binding);
    }

    fn restore_variable_state(&mut self, variable: &str, previous: VariableLocalState) {
        restore_map_entry(&mut self.range_domains, variable, previous.range_domain);
        restore_map_entry(&mut self.get_bindings, variable, previous.get_binding);
        restore_map_entry(&mut self.fragment_values, variable, previous.fragment_value);
        if previous.traversal_advanced {
            self.traversal_advances.insert(variable.to_string());
        } else {
            self.traversal_advances.remove(variable);
        }
        restore_map_entry(&mut self.default_paths, variable, previous.default_paths);
        restore_map_entry(&mut self.output_meta, variable, previous.output_meta);
        restore_map_entry(
            &mut self.scalar_dispatches,
            variable,
            previous.scalar_dispatch,
        );
        restore_map_entry(
            &mut self.truthy_reductions,
            variable,
            previous.truthy_reduction,
        );
        if previous.truthiness_abstained {
            self.truthiness_abstentions.insert(variable.to_string());
        } else {
            self.truthiness_abstentions.remove(variable);
        }
        if previous.truthiness_cleared {
            self.truthiness_clears.insert(variable.to_string());
        } else {
            self.truthiness_clears.remove(variable);
        }
        restore_map_entry(&mut self.typeof_sources, variable, previous.typeof_source);
        restore_map_entry(
            &mut self.int_cast_sources,
            variable,
            previous.int_cast_source,
        );
        restore_map_entry(
            &mut self.range_member_values,
            variable,
            previous.range_member_value,
        );
        restore_map_entry(
            &mut self.definite_range_member_values,
            variable,
            previous.definite_range_member_value,
        );
    }

    fn set_fragment_value(&mut self, variable: String, binding: Option<AbstractValue>) {
        self.clear_variable(&variable);
        if let Some(binding) = binding {
            self.fragment_values.insert(variable, binding);
        }
    }

    /// Binding a variable in one domain displaces whatever it held in every
    /// other domain (and any stale entry in its own).
    fn clear_variable(&mut self, variable: &str) {
        self.range_domains.remove(variable);
        self.get_bindings.remove(variable);
        self.fragment_values.remove(variable);
        self.traversal_advances.remove(variable);
        self.default_paths.remove(variable);
        self.output_meta.remove(variable);
        self.scalar_dispatches.remove(variable);
        self.truthy_reductions.remove(variable);
        self.truthiness_abstentions.remove(variable);
        self.truthiness_clears.remove(variable);
        self.typeof_sources.remove(variable);
        self.int_cast_sources.remove(variable);
        self.range_member_values.remove(variable);
        self.definite_range_member_values.remove(variable);
    }
}

fn restore_map_entry<T>(map: &mut HashMap<String, T>, variable: &str, value: Option<T>) {
    if let Some(value) = value {
        map.insert(variable.to_string(), value);
    } else {
        map.remove(variable);
    }
}

/// The lone non-trivial conjunct of a flat `And` formula, when there is
/// exactly one; a `False` conjunct or a second term abstains.
fn single_term(predicate: &Predicate) -> Option<&Predicate> {
    fn collect<'a>(predicate: &'a Predicate, term: &mut Option<&'a Predicate>) -> Option<()> {
        match predicate {
            Predicate::True => Some(()),
            Predicate::And(items) => {
                for item in items {
                    collect(item, term)?;
                }
                Some(())
            }
            Predicate::False => None,
            predicate => {
                if term.replace(predicate).is_some() {
                    return None;
                }
                Some(())
            }
        }
    }

    let mut term = None;
    collect(predicate, &mut term)?;
    term
}

fn quantify_range_member_reduction(condition: &Predicate, reduction: &Predicate) -> Option<Guard> {
    let Predicate::Guard(Guard::Range { path: range_path }) = single_term(condition)? else {
        return None;
    };
    let member_predicate = single_term(reduction)?;
    let Predicate::Guard(
        Guard::Eq {
            path: member_path, ..
        }
        | Guard::Truthy { path: member_path },
    ) = member_predicate
    else {
        return None;
    };
    let range_segments: Vec<&helm_schema_core::Segment> = range_path.segments().collect();
    let member_segments: Vec<&helm_schema_core::Segment> = member_path.segments().collect();
    let [wildcard, member] = member_segments.get(range_segments.len()..)? else {
        return None;
    };
    if !wildcard.is_each_member() || member_segments.get(..range_segments.len())? != range_segments
    {
        return None;
    }

    match member_predicate {
        Predicate::Guard(Guard::Eq { value, .. }) => Some(Guard::ContainsMemberEquals {
            path: range_path.clone(),
            member: member.literal()?.to_owned(),
            value: value.clone(),
        }),
        Predicate::Guard(Guard::Truthy { .. }) => Some(Guard::ContainsTruthyMember {
            path: range_path.clone(),
            member: member.literal()?.to_owned(),
        }),
        _ => None,
    }
}

fn predicate_guard_count(predicate: &Predicate) -> usize {
    match predicate {
        Predicate::True | Predicate::False | Predicate::Approximate { .. } => 0,
        Predicate::Guard(_) => 1,
        Predicate::Not(inner) => predicate_guard_count(inner),
        Predicate::And(items) | Predicate::Or(items) => {
            items.iter().map(predicate_guard_count).sum()
        }
    }
}

fn changed_truthy_reduction(entry: &Predicate, reduction: &Predicate) -> Option<Predicate> {
    if matches!(entry, Predicate::False) {
        return Some(reduction.clone());
    }
    let Predicate::Or(reduction_items) = reduction else {
        return None;
    };
    let entry_items = match entry {
        Predicate::Or(items) => items.as_slice(),
        predicate => std::slice::from_ref(predicate),
    };
    if !entry_items
        .iter()
        .all(|item| reduction_items.contains(item))
    {
        return None;
    }
    let changed = reduction_items
        .iter()
        .filter(|item| !entry_items.contains(item))
        .cloned()
        .collect::<Vec<_>>();
    Some(match changed.as_slice() {
        [] => Predicate::False,
        [predicate] => predicate.clone(),
        _ => Predicate::Or(changed),
    })
}

fn union_truthy_reductions(left: &Predicate, right: &Predicate) -> Predicate {
    if matches!(left, Predicate::False) {
        return right.clone();
    }
    if matches!(right, Predicate::False) || left == right {
        return left.clone();
    }
    match left {
        Predicate::Or(predicates) => {
            let mut predicates = predicates.clone();
            if !predicates.contains(right) {
                predicates.push(right.clone());
            }
            Predicate::Or(predicates)
        }
        _ => Predicate::Or(vec![left.clone(), right.clone()]),
    }
}

fn predicate_implies(antecedent: &Predicate, consequent: &Predicate) -> bool {
    if antecedent.exactly_implies(consequent) {
        return true;
    }
    if let (Predicate::Or(antecedents), Predicate::Or(consequents)) = (antecedent, consequent) {
        return antecedents.iter().all(|antecedent| {
            consequents
                .iter()
                .any(|consequent| predicate_implies(antecedent, consequent))
        });
    }
    match consequent {
        Predicate::And(predicates) => predicates
            .iter()
            .all(|predicate| predicate_implies(antecedent, predicate)),
        Predicate::Or(predicates) => predicates
            .iter()
            .any(|predicate| predicate_implies(antecedent, predicate)),
        _ => match antecedent {
            Predicate::Or(predicates) => predicates
                .iter()
                .all(|predicate| predicate_implies(predicate, consequent)),
            Predicate::And(predicates) => predicates
                .iter()
                .any(|predicate| predicate_implies(predicate, consequent)),
            _ => leaf_predicate_implies(antecedent, consequent),
        },
    }
}

fn leaf_predicate_implies(antecedent: &Predicate, consequent: &Predicate) -> bool {
    let Some(present_path) = predicate_present_path(antecedent) else {
        return false;
    };
    match consequent {
        Predicate::Not(inner) => matches!(
            inner.as_ref(),
            Predicate::Guard(Guard::Absent { path })
                if path == present_path || path_is_strict_ancestor(path, present_path)
        ),
        Predicate::Guard(Guard::HasKey { path, key }) => {
            let mut key_path = path.clone();
            key_path.push(key);
            present_path == &key_path || present_path.is_descendant_of(&key_path)
        }
        Predicate::Guard(Guard::Truthy { path }) if path == present_path => match antecedent {
            Predicate::Guard(Guard::Eq { value, .. }) => guard_value_is_truthy(value),
            Predicate::Guard(Guard::MatchesPattern { pattern, .. }) => {
                regex::Regex::new(pattern).is_ok_and(|pattern| !pattern.is_match(""))
            }
            _ => false,
        },
        Predicate::Guard(Guard::Range { path } | Guard::Truthy { path }) => {
            path_is_strict_ancestor(path, present_path)
        }
        _ => false,
    }
}

fn predicate_present_path(predicate: &Predicate) -> Option<&helm_schema_core::ValuesPath> {
    match predicate {
        Predicate::Not(inner) => match inner.as_ref() {
            Predicate::Guard(Guard::Absent { path }) => Some(path),
            _ => None,
        },
        Predicate::Guard(
            Guard::Truthy { path }
            | Guard::Eq { path, .. }
            | Guard::MatchesPattern { path, .. }
            | Guard::NotMatchesPattern { path, .. }
            | Guard::TypeIs { path, .. },
        ) => Some(path),
        _ => None,
    }
}

fn path_is_strict_ancestor(
    parent: &helm_schema_core::ValuesPath,
    child: &helm_schema_core::ValuesPath,
) -> bool {
    child.is_descendant_of(parent)
}

fn guard_value_is_truthy(value: &helm_schema_core::GuardValue) -> bool {
    match value {
        helm_schema_core::GuardValue::String(text) => !text.is_empty(),
        helm_schema_core::GuardValue::Bool(value) => *value,
        helm_schema_core::GuardValue::Int(value) => *value != 0,
        helm_schema_core::GuardValue::Float(text) => {
            text.parse::<f64>().is_ok_and(|value| value != 0.0)
        }
        helm_schema_core::GuardValue::Null => false,
    }
}