use std::path::Path;
use std::{fs, io};
pub use crate::generators::report::Outcome;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Ownership {
Generated,
Scaffold,
}
impl Ownership {
pub fn should_write(self, path: &Path, force: bool) -> bool {
match self {
Self::Generated => true,
Self::Scaffold => force || !path.exists(),
}
}
}
pub fn write_owned(
directory: &Path,
name: &str,
contents: &str,
ownership: Ownership,
force: bool,
) -> io::Result<Outcome> {
let path = directory.join(name);
if !ownership.should_write(&path, force) {
return Ok(Outcome::Preserved(path));
}
fs::write(&path, contents)?;
Ok(Outcome::Written(path))
}