Skip to main content

parse_yaml_at_path_as

Function parse_yaml_at_path_as 

Source
pub fn parse_yaml_at_path_as<T>(files: &[RenderedFile], path: &str) -> T
Expand description

Locate the RenderedFile in files whose sandboxed relative RenderedFile::path byte-equals path, then parse its RenderedFile::contents as a caller-typed T YAML document via serde_yaml::from_str::<T>. Panics on either short-circuit arm — the leaf is absent ({path} present) or the contents do not parse as T at the passed serde::Deserialize boundary ({path} parses as YAML: {err}).

The caller-typed peer to parse_yaml_at_path — same leaf-locate + serde_yaml::from_str two-step, extended one axis- arity by letting the caller pin the parse target through the standard serde::Deserialize surface every renderer already carries on its emit-side serde::Serialize-derived body type. Where parse_yaml_at_path closes the untyped serde_yaml::Value-arity every renderer-neutral kube_* / mapping_* navigator downstream keys off, this closes the paired typed-arity every renderer-native emit-side test-arm keys off — caixa-helm’s ChartYaml-typed Chart.yaml readback, a future caixa-flux HelmRelease-typed CR readback once the crate carries its own #[derive(Deserialize)] mirror of the FluxCD HelmRelease body, a future caixa-mesh Aplicacao-typed mesh-CR readback once the mesh.pleme.io/v1alpha1 CR body’s typed-mirror lands (MESH-COMPOSITION §III.2 #5), the caixa-teia per-tofu-manifest TfLock-typed readback once the lockfile-typed-mirror lands.

Every per-artifact typed-parse readback across caixa-helm’s render_chart_for_servico lareira-<nome> chart-directory emit previously carried the same four-line

let chart_file = find_file_by_path(&dir.files, HELM_CHART_YAML_FILENAME)
    .unwrap(); // or .expect("Chart.yaml present")
let chart: ChartYaml =
    serde_yaml::from_str(&chart_file.contents).unwrap();

combinator around a two-token semantic payload (the <FILENAME> filename constant + the <T> parse-target type) around the same two-hop locate-then-parse readback intent “read the typed body at this leaf”. Every routed consumer now folds those four lines onto parse_yaml_at_path_as::<T>(&files, <FILENAME>) — the leaf-navigate step, the parse step, and the two .expect/.unwrap_or_else(|| panic!(...)) panics happen once inside the helper, the caller keeps its downstream per-body-field assertions (chart.api_version == "v2", chart.dependencies[0].name == DEFAULT_LIBRARY_NAME, etc.) unchanged.

Panic messages are composed from the passed path verbatim — the missing-leaf arm produces "{path} present", the parse-failure arm produces "{path} parses as YAML: {err}". These match the canonical shape the routed test-side sites carried inline ("Chart.yaml present" on the .expect-shaped variant), and the parse-failure arm now includes the underlying serde_yaml::Error for far-easier root-cause naming than the prior serde_yaml::from_str(...).unwrap() shape (which produced the canonical Result::unwrap panic naming nothing about which leaf, which parse-target type, or which per-field boundary the parser tripped over).

Every future per-artifact typed-parse readback (a future caixa-flux per-CR-trio typed-mirror readback whose test-side asserts on the HelmRelease.spec.chart.spec.chart per-field body through a typed struct rather than kube_spec_str_field(&parsed, ...); a future caixa-mesh per-Aplicacao-CR body typed-mirror readback whose test-side asserts on the Aplicacao.spec.membros typed vector rather than kube_spec_seq_field(&parsed, ...); a future caixa-arch per- flake typed-mirror readback whose test-side asserts on the emitted flake.nix frontmatter through a typed NixFlakeFrontmatter struct) reaches this same helper by construction, with no inline find_file_by_path(...).expect(...) + serde_yaml::from_str::<T>(...) .expect(...) two-step and no drift surface on the parse-target, panic-message-composition, or filename-axis-key axes.

Composition-symmetric with the sibling parse_yaml_at_path on the untyped-parse arm: the two together bracket every leaf-locate paired with a serde_yaml-parse readback the substrate output containers admit — parse_yaml_at_path on the serde_yaml::Value-arity (renderer-neutral navigation through kube_* / mapping_* primitives), parse_yaml_at_path_as on the caller-typed-arity (renderer-native per-body-field assertion through the typed struct the emit-side already carries).