use crate::OrthoResult;
use camino::Utf8PathBuf;
pub trait PostMergeHook: Sized {
fn post_merge(&mut self, ctx: &PostMergeContext) -> OrthoResult<()>;
}
#[derive(Debug, Clone, Default)]
pub struct PostMergeContext {
prefix: String,
loaded_files: Vec<Utf8PathBuf>,
has_cli_input: bool,
}
impl PostMergeContext {
#[must_use]
pub fn new(prefix: impl Into<String>) -> Self {
Self {
prefix: prefix.into(),
loaded_files: Vec::new(),
has_cli_input: false,
}
}
pub fn with_file(&mut self, path: Utf8PathBuf) -> &mut Self {
self.loaded_files.push(path);
self
}
#[expect(
clippy::missing_const_for_fn,
reason = "Not const for API consistency with `with_file`, which cannot be const due to Vec::push"
)]
pub fn with_cli_input(&mut self) -> &mut Self {
self.has_cli_input = true;
self
}
#[must_use]
pub fn prefix(&self) -> &str {
&self.prefix
}
#[must_use]
pub fn loaded_files(&self) -> &[Utf8PathBuf] {
&self.loaded_files
}
#[must_use]
pub const fn has_cli_input(&self) -> bool {
self.has_cli_input
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn context_new_creates_empty_context() {
let ctx = PostMergeContext::new("TEST_");
assert_eq!(ctx.prefix(), "TEST_");
assert!(ctx.loaded_files().is_empty());
assert!(!ctx.has_cli_input());
}
#[test]
fn context_with_file_adds_path() {
let mut ctx = PostMergeContext::new("TEST_");
ctx.with_file(Utf8PathBuf::from("/etc/test.toml"))
.with_file(Utf8PathBuf::from("/home/user/.test.toml"));
assert_eq!(ctx.loaded_files().len(), 2);
assert_eq!(
ctx.loaded_files().first().map(|p| p.as_str()),
Some("/etc/test.toml")
);
assert_eq!(
ctx.loaded_files().get(1).map(|p| p.as_str()),
Some("/home/user/.test.toml")
);
}
#[test]
fn context_with_cli_input_sets_flag() {
let mut ctx = PostMergeContext::new("TEST_");
ctx.with_cli_input();
assert!(ctx.has_cli_input());
}
#[test]
fn context_builders_chain() {
let mut ctx = PostMergeContext::new("APP_");
ctx.with_file(Utf8PathBuf::from("/etc/app.toml"))
.with_cli_input()
.with_file(Utf8PathBuf::from("~/.app.toml"));
assert_eq!(ctx.prefix(), "APP_");
assert_eq!(ctx.loaded_files().len(), 2);
assert!(ctx.has_cli_input());
}
}