Skip to main content

helm_schema_core/
output_path.rs

1use std::collections::BTreeSet;
2
3use crate::YamlPath;
4
5/// Structural path segment for the value of a rendered mapping entry whose
6/// key is supplied by a template expression.
7pub const DYNAMIC_MAPPING_VALUE_SEGMENT: &str = "{*}";
8
9/// Reports whether `sources` contains a strict descendant of `path`.
10#[must_use]
11pub fn values_path_has_descendant(path: &str, sources: &BTreeSet<String>) -> bool {
12    sources
13        .iter()
14        .any(|source| values_path_is_descendant(source, path))
15}
16
17/// Reports whether `path` is a strict segmented descendant of `ancestor`.
18#[must_use]
19pub fn values_path_is_descendant(path: &str, ancestor: &str) -> bool {
20    let path = crate::split_value_path(path);
21    let ancestor = crate::split_value_path(ancestor);
22    path.len() > ancestor.len() && path.starts_with(&ancestor)
23}
24
25/// Appends a rendered YAML path relative to a base path.
26#[must_use]
27pub fn append_relative_path(base: &YamlPath, relative: &YamlPath) -> YamlPath {
28    let mut out = base.clone();
29    out.0.extend(relative.0.iter().cloned());
30    out
31}
32
33/// Marks the final path segment as a sequence-item collection slot.
34#[must_use]
35pub fn sequence_item_path(relative_path: &YamlPath) -> YamlPath {
36    let mut path = relative_path.clone();
37    if let Some(last) = path.0.last_mut() {
38        if !last.ends_with("[*]") {
39            last.push_str("[*]");
40        }
41    } else {
42        path.0.push("[*]".to_string());
43    }
44    path
45}
46
47/// Appends the structural dynamic-key value segment to a rendered YAML path.
48#[must_use]
49pub fn dynamic_mapping_value_path(relative_path: &YamlPath) -> YamlPath {
50    let mut path = relative_path.clone();
51    path.0.push(DYNAMIC_MAPPING_VALUE_SEGMENT.to_string());
52    path
53}