use miette::Diagnostic;
use thiserror::Error;
use crate::{
builder::{BuildError, Builder, InitError},
date::{parse, today},
workspace::Workspace,
};
#[derive(Debug, Error, Diagnostic)]
#[error(transparent)]
#[diagnostic(transparent)]
pub enum ErrorSource {
Date(#[from] crate::date::Error),
Init(#[from] InitError),
Build(#[from] BuildError),
}
#[derive(Debug, Error, Diagnostic)]
#[error("failed to preview")]
#[diagnostic(
code(changelogging::commands::preview),
help("see the report for more information")
)]
pub struct Error {
#[source]
#[diagnostic_source]
pub source: ErrorSource,
}
impl Error {
pub fn new(source: ErrorSource) -> Self {
Self { source }
}
pub fn date(error: crate::date::Error) -> Self {
Self::new(error.into())
}
pub fn init(error: InitError) -> Self {
Self::new(error.into())
}
pub fn build(error: BuildError) -> Self {
Self::new(error.into())
}
}
pub fn preview<S: AsRef<str>>(workspace: Workspace<'_>, date: Option<S>) -> Result<(), Error> {
let date = match date {
Some(string) => parse(string).map_err(Error::date)?,
None => today(),
};
let builder = Builder::from_workspace(workspace, date).map_err(Error::init)?;
builder.preview().map_err(Error::build)?;
Ok(())
}