#[derive(Debug, Clone, Default)]
pub struct TemplateContext {
pub target: Option<String>,
pub module: Option<String>,
pub file: Option<String>,
pub branch: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TemplateWarning {
UnknownVariable { name: String, field: Option<String> },
GitBranchDetectionFailed { error: String },
}
impl std::fmt::Display for TemplateWarning {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TemplateWarning::UnknownVariable { name, field: None } => {
write!(f, "Unknown template variable: {{{{{}}}}}", name)
}
TemplateWarning::UnknownVariable {
name,
field: Some(field),
} => {
write!(
f,
"Unknown template variable in {}: {{{{{}}}}}",
field, name
)
}
TemplateWarning::GitBranchDetectionFailed { error } => {
write!(f, "Git branch detection failed: {}", error)
}
}
}
}
#[derive(Debug, Clone, Default)]
pub struct TemplateValidation {
pub warnings: Vec<TemplateWarning>,
pub uses_branch: bool,
}
impl TemplateValidation {
pub fn has_unknown_variables(&self) -> bool {
self.warnings
.iter()
.any(|w| matches!(w, TemplateWarning::UnknownVariable { .. }))
}
pub fn unknown_variable_names(&self) -> Vec<String> {
let mut names: Vec<String> = self
.warnings
.iter()
.filter_map(|w| match w {
TemplateWarning::UnknownVariable { name, .. } => Some(name.clone()),
_ => None,
})
.collect();
names.sort();
names.dedup();
names
}
}