Expand description
HCL language item traversal.
Each method of the Visit
trait is a hook that can be overridden to customize the behavior
when visiting the corresponding type of language item. By default, every method recursively
visits the substructure of the AST by invoking the right visitor method of each of its fields.
The API is modeled after syn::visit
. For a
mutable alternative, see hcl_edit::visit_mut
.
§Examples
Collect all referenced variables from a HCL document:
use hcl_edit::expr::Expression;
use hcl_edit::structure::Body;
use hcl_edit::visit::{visit_expr, Visit};
use std::collections::HashSet;
use std::str::FromStr;
#[derive(Default)]
struct VariableNameVisitor {
variable_names: HashSet<String>,
}
impl Visit for VariableNameVisitor {
fn visit_expr(&mut self, expr: &Expression) {
if let Expression::Variable(var) = expr {
self.variable_names.insert(var.to_string());
} else {
// Recurse further down the AST.
visit_expr(self, expr);
}
}
}
let input = r#"
// A service definition.
service {
fullname = "${namespace}/${name}"
health_endpoint = "${base_url}/health"
}
"#;
let body = input.parse::<Body>()?;
let mut visitor = VariableNameVisitor::default();
visitor.visit_body(&body);
let expected = HashSet::from(["namespace".into(), "name".into(), "base_url".into()]);
assert_eq!(visitor.variable_names, expected);
Traits§
- Visit
- Traversal to walk a shared borrow of an HCL language item.
Functions§
- visit_
array - visit_
attr - visit_
binary_ op - visit_
block - visit_
block_ label - visit_
body - visit_
conditional - visit_
directive - visit_
element - visit_
else_ template_ expr - visit_
expr - visit_
for_ cond - visit_
for_ directive - visit_
for_ expr - visit_
for_ intro - visit_
for_ template_ expr - visit_
func_ args - visit_
func_ call - visit_
func_ name - visit_
heredoc_ template - visit_
if_ directive - visit_
if_ template_ expr - visit_
interpolation - visit_
object - visit_
object_ item - visit_
object_ key - visit_
object_ value - visit_
parenthesis - visit_
string_ template - visit_
structure - visit_
template - visit_
traversal - visit_
traversal_ operator - visit_
unary_ op