use std::collections::BTreeMap;
use std::fs::File;
use crate::{Hook, Repo, Result, CI};
pub static PRE_COMMIT_CONFIG_PATH: &str = ".pre-commit-config.yaml";
#[serde_with::skip_serializing_none]
#[derive(serde::Serialize, serde::Deserialize, Debug, Eq, Ord, PartialEq, PartialOrd, Clone)]
pub struct PreCommitConfig {
ci: Option<CI>,
repos: Vec<Repo>,
default_install_hook_types: Option<Vec<String>>,
default_language_version: Option<BTreeMap<String, String>>,
default_stages: Option<Vec<String>>,
files: Option<String>,
exclude: Option<String>,
fail_fast: Option<bool>,
minimum_pre_commit_version: Option<String>,
}
impl PreCommitConfig {
#[must_use]
pub const fn new() -> Self {
Self {
ci: None,
repos: Vec::new(),
default_install_hook_types: None,
default_language_version: None,
default_stages: None,
files: None,
exclude: None,
fail_fast: None,
minimum_pre_commit_version: None,
}
}
pub fn read() -> Result<Self> {
let input = File::open(PRE_COMMIT_CONFIG_PATH)?;
Ok(serde_yaml::from_reader(input)?)
}
pub fn write(&self) -> Result<()> {
let output = File::create(PRE_COMMIT_CONFIG_PATH)?;
Ok(serde_yaml::to_writer(output, &self)?)
}
pub fn add_repo(&mut self, repo: Repo) {
self.repos.push(repo);
}
pub fn sort(&mut self) {
for repo in &mut self.repos {
repo.sort();
}
self.repos.sort();
self.repos.dedup();
}
pub fn install(&mut self) {
let mut repo = Repo::new(
env!("CARGO_PKG_REPOSITORY").to_string(),
env!("CARGO_PKG_VERSION").to_string(),
);
let hook = Hook::new(env!("CARGO_PKG_NAME").to_string());
repo.add_hook(hook);
self.add_repo(repo);
}
}
impl Default for PreCommitConfig {
fn default() -> Self {
Self::new()
}
}