tfparser_core/terragrunt/context.rs
1//! Per-resolution context handed to [`crate::terragrunt::TerragruntResolver::resolve`].
2//!
3//! `TgContext` mirrors [14-terragrunt.md § 2]'s shape and is a read-only
4//! input bundle: workspace root, env-var policy, depth caps. The mutable
5//! resolution state (memo, include stack) lives inside the resolver's
6//! per-call scratchpad — see [`crate::terragrunt::resolver`].
7//!
8//! [14-terragrunt.md § 2]: ../../../specs/14-terragrunt.md
9
10use std::{collections::BTreeSet, path::Path, sync::Arc};
11
12use crate::eval::EnvVarMode;
13
14/// Read-only inputs to a single [`crate::terragrunt::TerragruntResolver::resolve`] call.
15///
16/// `allowed_env` is wrapped in `Arc<BTreeSet<Arc<str>>>` per
17/// [14-terragrunt.md § 9 CLAUDE.md anchoring] so it's cheap to share across
18/// `rayon` worker threads. `max_include_depth` defaults to 32, matching the
19/// spec's pin.
20#[derive(Clone, Debug)]
21#[non_exhaustive]
22pub struct TgContext {
23 /// Canonical absolute workspace root. Every Terragrunt-function-resolved
24 /// path must remain underneath this; the resolver enforces it via the
25 /// internal `canonicalize_inside` helper.
26 pub workspace_root: Arc<Path>,
27
28 /// Pinned environment name (e.g. `Some("staging")`). Surfaces as
29 /// `terraform.workspace` in downstream evaluator scopes; the resolver
30 /// itself does **not** consult it for `get_env`.
31 pub environment: Option<Arc<str>>,
32
33 /// How `get_env(name, default?)` reads the process environment. Default
34 /// is [`EnvVarMode::Strict`] with an empty allowlist (off by default).
35 pub env_var_mode: EnvVarMode,
36
37 /// Allowlist of environment-variable names visible to `get_env`. Same
38 /// shape as [`crate::eval::EvalContext`]'s field; kept separate to
39 /// allow tighter Terragrunt-only policy.
40 pub allowed_env: Arc<BTreeSet<Arc<str>>>,
41
42 /// Maximum include-chain depth before [`crate::terragrunt::TerragruntError::DepthExceeded`]
43 /// fires. Default: 32.
44 pub max_include_depth: u32,
45}
46
47impl TgContext {
48 /// Construct a `TgContext` with the spec defaults: env-var mode strict
49 /// with empty allowlist, `max_include_depth = 32`.
50 #[must_use]
51 pub fn new(workspace_root: Arc<Path>) -> Self {
52 Self {
53 workspace_root,
54 environment: None,
55 env_var_mode: EnvVarMode::default(),
56 allowed_env: Arc::new(BTreeSet::new()),
57 max_include_depth: 32,
58 }
59 }
60}