use crate::diagnostic::{Diagnostic, DiagnosticResult};
use crate::ui;
use std::path::Path;
mod artifact;
mod artifact_io;
mod artifact_normalize;
mod changelog;
pub use artifact::{read_clause, read_rfc, write_clause, write_rfc};
pub use artifact_normalize::{normalize_clause_value, normalize_rfc_value};
pub use changelog::{BumpLevel, ParsedChange, add_changelog_change, bump_rfc_version, today};
pub fn parse_changelog_change(change: &str) -> DiagnosticResult<ParsedChange> {
changelog::parse_changelog_change(change)
}
#[derive(Debug, Clone, Copy, Default)]
pub enum WriteOp {
#[default]
Execute,
Preview,
}
impl WriteOp {
pub fn from_dry_run(dry_run: bool) -> Self {
if dry_run {
WriteOp::Preview
} else {
WriteOp::Execute
}
}
pub fn is_preview(&self) -> bool {
matches!(self, WriteOp::Preview)
}
}
pub fn write_file(
path: &Path,
content: &str,
op: WriteOp,
display_path: Option<&Path>,
) -> DiagnosticResult<()> {
let output_path = display_path.unwrap_or(path);
match op {
WriteOp::Execute => {
std::fs::write(path, content).map_err(|err| {
Diagnostic::io_error("write file", err, output_path.display().to_string())
})?;
}
WriteOp::Preview => {
ui::dry_run_file_preview(output_path, content);
}
}
Ok(())
}
pub fn create_dir_all(
path: &Path,
op: WriteOp,
display_path: Option<&Path>,
) -> DiagnosticResult<()> {
let output_path = display_path.unwrap_or(path);
match op {
WriteOp::Execute => {
std::fs::create_dir_all(path).map_err(|err| {
Diagnostic::io_error("create directory", err, output_path.display().to_string())
})?;
}
WriteOp::Preview => {
ui::dry_run_mkdir(output_path);
}
}
Ok(())
}
pub fn delete_file(path: &Path, op: WriteOp, display_path: Option<&Path>) -> DiagnosticResult<()> {
let output_path = display_path.unwrap_or(path);
match op {
WriteOp::Execute => {
std::fs::remove_file(path).map_err(|err| {
Diagnostic::io_error("delete file", err, output_path.display().to_string())
})?;
}
WriteOp::Preview => {
ui::info(format!("[DRY RUN] Would delete: {}", output_path.display()));
}
}
Ok(())
}