use thiserror::Error;
use crate::issue::A3Issue;
#[derive(Debug, Error)]
pub enum A3Error {
#[error("Failed to parse JSON: {0}")]
Parse(#[from] serde_json::Error),
#[error("Failed to serialize to JSON: {0}")]
Serialize(serde_json::Error),
#[error("A3 validation failed with {} issue(s):\n{}", .0.len(), format_issues(.0))]
Validate(Vec<A3Issue>),
}
impl A3Error {
pub fn issues(&self) -> &[A3Issue] {
match self {
A3Error::Validate(issues) => issues,
_ => &[],
}
}
pub fn stage(&self) -> Option<u8> {
self.issues().first().map(A3Issue::stage)
}
}
fn format_issues(issues: &[A3Issue]) -> String {
issues
.iter()
.map(|i| format!(" {i}"))
.collect::<Vec<_>>()
.join("\n")
}