2 Commits

Author SHA1 Message Date
2e974e0503 Merge pull request 'patch resolver' (#18) from release/0.5.1 into main
All checks were successful
Release / Build and Release (push) Successful in 56s
Reviewed-on: #18
2026-03-20 10:54:51 +01:00
8dc96d4605 fixed resolver issue 2026-03-20 10:54:38 +01:00
2 changed files with 271 additions and 65 deletions

2
Cargo.lock generated
View File

@@ -914,7 +914,7 @@ dependencies = [
[[package]] [[package]]
name = "mould" name = "mould"
version = "0.5.0" version = "0.5.1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",

View File

@@ -6,83 +6,289 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
/// Logic for determining which files to parse and where to save the results. pub struct Rule {
pub struct TemplateResolver; pub template_suffix: &'static str,
pub active_suffix: &'static str,
pub is_exact_match: bool,
}
impl TemplateResolver { pub const RULES: &[Rule] = &[
/// Determines the template and output paths based on the provided input. // Exact matches
/// Rule {
/// If an output path is explicitly provided via CLI arguments, it is used. template_suffix: "compose.yml",
/// Otherwise, the resolver applies a set of heuristic rules to find a matching pairing. active_suffix: "compose.override.yml",
pub fn resolve( is_exact_match: true,
input: &Path, },
output_override: Option<PathBuf>, Rule {
) -> (PathBuf, PathBuf) { template_suffix: "compose.yaml",
if let Some(out) = output_override { active_suffix: "compose.override.yaml",
return (input.to_path_buf(), out); is_exact_match: true,
},
Rule {
template_suffix: "docker-compose.yml",
active_suffix: "docker-compose.override.yml",
is_exact_match: true,
},
Rule {
template_suffix: "docker-compose.yaml",
active_suffix: "docker-compose.override.yaml",
is_exact_match: true,
},
// Pattern matches
Rule {
template_suffix: ".env.example",
active_suffix: ".env",
is_exact_match: false,
},
Rule {
template_suffix: ".env.template",
active_suffix: ".env",
is_exact_match: false,
},
Rule {
template_suffix: ".example.json",
active_suffix: ".json",
is_exact_match: false,
},
Rule {
template_suffix: ".template.json",
active_suffix: ".json",
is_exact_match: false,
},
Rule {
template_suffix: ".example.yml",
active_suffix: ".yml",
is_exact_match: false,
},
Rule {
template_suffix: ".template.yml",
active_suffix: ".yml",
is_exact_match: false,
},
Rule {
template_suffix: ".example.yaml",
active_suffix: ".yaml",
is_exact_match: false,
},
Rule {
template_suffix: ".template.yaml",
active_suffix: ".yaml",
is_exact_match: false,
},
Rule {
template_suffix: ".example.toml",
active_suffix: ".toml",
is_exact_match: false,
},
Rule {
template_suffix: ".template.toml",
active_suffix: ".toml",
is_exact_match: false,
},
Rule {
template_suffix: ".example.xml",
active_suffix: ".xml",
is_exact_match: false,
},
Rule {
template_suffix: ".template.xml",
active_suffix: ".xml",
is_exact_match: false,
},
Rule {
template_suffix: ".example.ini",
active_suffix: ".ini",
is_exact_match: false,
},
Rule {
template_suffix: ".template.ini",
active_suffix: ".ini",
is_exact_match: false,
},
Rule {
template_suffix: ".example.properties",
active_suffix: ".properties",
is_exact_match: false,
},
Rule {
template_suffix: ".template.properties",
active_suffix: ".properties",
is_exact_match: false,
},
];
pub const DEFAULT_CANDIDATES: &[&str] = &[
".env.example",
"compose.yml",
"docker-compose.yml",
".env.template",
"compose.yaml",
"docker-compose.yaml",
];
/// Helper to automatically determine the output file path based on common naming conventions.
pub fn determine_output_path(input: &Path) -> PathBuf {
let file_name = input.file_name().unwrap_or_default().to_string_lossy();
for rule in RULES {
if rule.is_exact_match {
if file_name == rule.template_suffix {
return input.with_file_name(rule.active_suffix);
} }
} else if file_name == rule.template_suffix {
// Apply automatic discovery rules based on file name patterns. return input.with_file_name(rule.active_suffix);
if let Some((template, output)) = Self::discover_pairing(input) { } else if let Some(base) = file_name.strip_suffix(rule.template_suffix) {
(template, output) return input.with_file_name(format!("{}{}", base, rule.active_suffix));
} else {
// Fallback: If no pairing is found, use the input as both
// the template source and the save target.
(input.to_path_buf(), input.to_path_buf())
} }
} }
/// Attempts to find a known template/active pairing for a given file path. input.with_extension(format!(
/// "{}.out",
/// Naming Rules Applied: input.extension().unwrap_or_default().to_string_lossy()
/// 1. `.env.example` <-> `.env` (Standard environment file pattern). ))
/// 2. `compose.yml` -> `compose.override.yml` (Docker Compose convention). }
/// 3. `<name>.template.<ext>` -> `<name>.<ext>` (General template pattern).
/// 4. `<name>.<ext>.example` -> `<name>.<ext>` (General example pattern).
fn discover_pairing(path: &Path) -> Option<(PathBuf, PathBuf)> {
let file_name = path.file_name()?.to_str()?;
// Rule 1: Standard .env pairing /// Discovers common configuration template files in the current directory.
if file_name == ".env" || file_name == ".env.example" { pub fn find_input_file() -> Option<PathBuf> {
let dir = path.parent().unwrap_or_else(|| Path::new(".")); // Priority 1: Exact matches for well-known defaults
return Some((dir.join(".env.example"), dir.join(".env"))); for &name in DEFAULT_CANDIDATES {
let path = PathBuf::from(name);
if path.exists() {
return Some(path);
}
} }
// Rule 2: Docker Compose pairing // Priority 2: Pattern matches
if file_name == "docker-compose.yml" || file_name == "docker-compose.yaml" || file_name == "compose.yml" { if let Ok(entries) = std::fs::read_dir(".") {
let dir = path.parent().unwrap_or_else(|| Path::new(".")); let mut fallback = None;
let override_file = if file_name == "compose.yml" { for entry in entries.flatten() {
"compose.override.yml" let name = entry.file_name();
} else { let name_str = name.to_string_lossy();
"docker-compose.override.yml"
}; for rule in RULES {
return Some((path.to_path_buf(), dir.join(override_file))); if !rule.is_exact_match && name_str.ends_with(rule.template_suffix) {
if name_str.contains(".env") || name_str.contains("compose") {
return Some(entry.path());
} }
if fallback.is_none() {
// Rule 3: .template or .example suffix removal fallback = Some(entry.path());
if file_name.contains(".template.") {
let output_name = file_name.replace(".template.", ".");
return Some((path.to_path_buf(), path.with_file_name(output_name)));
} }
break;
if file_name.ends_with(".example") {
let output_name = &file_name[..file_name.len() - 8];
return Some((path.to_path_buf(), path.with_file_name(output_name)));
} }
}
// Inverse Rule 3: If running against the active file, look for the template counterpart. }
let template_candidates = [ if let Some(path) = fallback {
format!("{}.example", file_name), return Some(path);
file_name.replace('.', ".template."),
];
for t in template_candidates {
let p = path.with_file_name(t);
if p.exists() {
return Some((p, path.to_path_buf()));
} }
} }
None None
}
/// Resolves the active and template paths given an input path.
/// Returns `(active_path, template_path)`.
pub fn resolve_paths(input: &Path) -> (Option<PathBuf>, Option<PathBuf>) {
let file_name = input.file_name().unwrap_or_default().to_string_lossy();
// Check if the input matches any known template pattern
let mut is_template = false;
for rule in RULES {
if rule.is_exact_match {
if file_name == rule.template_suffix {
is_template = true;
break;
}
} else if file_name.ends_with(rule.template_suffix) {
is_template = true;
break;
}
}
// Fallback template detection
if !is_template && (file_name.contains(".example") || file_name.contains(".template")) {
is_template = true;
}
if is_template {
let expected_active = determine_output_path(input);
let active = if expected_active.exists() {
Some(expected_active)
} else {
None
};
(active, Some(input.to_path_buf()))
} else {
// Input is treated as the active config
let active = Some(input.to_path_buf());
let mut template = None;
// Try to reverse match rules to find a template
for rule in RULES {
if rule.is_exact_match {
if file_name == rule.active_suffix {
let t = input.with_file_name(rule.template_suffix);
if t.exists() {
template = Some(t);
break;
}
}
} else if file_name.ends_with(rule.active_suffix) {
if file_name == rule.active_suffix {
let t = input.with_file_name(rule.template_suffix);
if t.exists() {
template = Some(t);
break;
}
} else if let Some(base) = file_name.strip_suffix(rule.active_suffix) {
let t = input.with_file_name(format!("{}{}", base, rule.template_suffix));
if t.exists() {
template = Some(t);
break;
}
}
}
}
// Fallback reverse detection
if template.is_none() {
let possible_templates = [
format!("{}.example", file_name),
format!("{}.template", file_name),
];
for t in possible_templates {
let p = input.with_file_name(t);
if p.exists() {
template = Some(p);
break;
}
}
}
(active, template)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_determine_output_path() {
assert_eq!(
determine_output_path(Path::new(".env.example")),
PathBuf::from(".env")
);
assert_eq!(
determine_output_path(Path::new("compose.yml")),
PathBuf::from("compose.override.yml")
);
assert_eq!(
determine_output_path(Path::new("config.template.json")),
PathBuf::from("config.json")
);
assert_eq!(
determine_output_path(Path::new("config.example")),
PathBuf::from("config.example.out")
);
} }
} }