Skip to main content

drft/rules/
untrackable_target.rs

1use crate::diagnostic::Diagnostic;
2use crate::graph::NodeType;
3use crate::rules::{Rule, RuleContext};
4
5pub struct UntrackableTargetRule;
6
7impl Rule for UntrackableTargetRule {
8    fn name(&self) -> &str {
9        "untrackable-target"
10    }
11
12    fn evaluate(&self, ctx: &RuleContext) -> Vec<Diagnostic> {
13        let graph = &ctx.graph.graph;
14
15        graph
16            .edges
17            .iter()
18            .filter_map(|edge| {
19                let node = graph.nodes.get(&edge.target)?;
20                if node.node_type != NodeType::Directory || node.hash.is_some() {
21                    return None;
22                }
23
24                Some(Diagnostic {
25                    rule: "untrackable-target".into(),
26                    message: "directory has no drft.toml — cannot track for staleness".into(),
27                    source: Some(edge.source.clone()),
28                    target: Some(edge.target.clone()),
29                    fix: Some(format!(
30                        "add a drft.toml to {t} to declare it as a graph",
31                        t = edge.target
32                    )),
33                    ..Default::default()
34                })
35            })
36            .collect()
37    }
38}