Skip to main content

xarf/
error.rs

1//! Error and result types for the XARF crate.
2
3use thiserror::Error;
4
5/// Top-level error type for the XARF crate. Returned by parsing, validation,
6/// and v3-conversion entry points whose failure modes are fatal.
7#[derive(Debug, Error)]
8pub enum XarfError {
9    /// The input was not syntactically valid JSON (or not a JSON object).
10    #[error("invalid JSON: {0}")]
11    InvalidJson(String),
12
13    /// Schema-validation failure with a structured list of issues. Non-fatal
14    /// failures (recoverable into a [`crate::ParseResult`]) are exposed as
15    /// `errors` on the result; this variant only surfaces when no result can
16    /// be produced.
17    #[error("schema validation failed ({} error(s))", .0.len())]
18    Validation(Vec<ValidationError>),
19
20    /// A bundled schema failed to parse or compile. This should never happen
21    /// for a published release of the crate; it indicates a programmer error.
22    #[error("schema error: {0}")]
23    Schema(String),
24
25    /// A v3 report could not be converted to v4 (e.g. unknown `ReportType`,
26    /// missing source identifier, malformed reporter email).
27    #[error("v3 conversion failed: {0}")]
28    V3Conversion(String),
29
30    /// Evidence payload encoding/decoding or size-limit failure.
31    #[error("evidence error: {0}")]
32    Evidence(String),
33}
34
35/// A single structured validation issue.
36///
37/// `field` is a JSON-path-style descriptor of the offending location (empty
38/// string for whole-document failures). `message` is a human-readable
39/// description.
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct ValidationError {
42    pub field: String,
43    pub message: String,
44}
45
46impl ValidationError {
47    pub fn new(field: impl Into<String>, message: impl Into<String>) -> Self {
48        Self {
49            field: field.into(),
50            message: message.into(),
51        }
52    }
53}
54
55/// A non-fatal warning surfaced alongside successful validation results.
56#[derive(Debug, Clone, PartialEq, Eq)]
57pub struct ValidationWarning {
58    pub field: String,
59    pub message: String,
60}
61
62impl ValidationWarning {
63    pub fn new(field: impl Into<String>, message: impl Into<String>) -> Self {
64        Self {
65            field: field.into(),
66            message: message.into(),
67        }
68    }
69}
70
71/// Informational metadata about absent optional/recommended fields.
72/// Produced only when callers request `show_missing_optional`.
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub struct ValidationInfo {
75    pub field: String,
76    pub message: String,
77}
78
79impl ValidationInfo {
80    pub fn new(field: impl Into<String>, message: impl Into<String>) -> Self {
81        Self {
82            field: field.into(),
83            message: message.into(),
84        }
85    }
86}
87
88pub type Result<T> = std::result::Result<T, XarfError>;