use crate::errors::GitignoreError;
use serde::Deserialize;
use std::{borrow::Cow, fs::File, io::Write, path::Path};
pub static GITIGNORE_FILENAME: &str = ".gitignore";
#[derive(Deserialize, Default)]
pub struct Gitignore;
impl Gitignore {
pub fn new() -> Self {
Self::default()
}
pub fn exists_at(path: &Path) -> bool {
let mut path = Cow::from(path);
if path.is_dir() {
path.to_mut().push(GITIGNORE_FILENAME);
}
path.exists()
}
pub fn write_to(self, path: &Path) -> Result<(), GitignoreError> {
let mut path = Cow::from(path);
if path.is_dir() {
path.to_mut().push(GITIGNORE_FILENAME);
}
let mut file = File::create(&path)?;
Ok(file.write_all(self.template().as_bytes())?)
}
fn template(&self) -> String {
"outputs/\n".to_string()
}
}