Skip to main content

openbim_ilcd_epd/
error.rs

1use std::{error::Error, fmt};
2
3/// Stable category for an ILCD+EPD parse failure.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum ParseErrorKind {
6    /// Input exceeds the configured byte budget.
7    InputTooLarge,
8    /// Input is not UTF-8 XML.
9    InvalidUtf8,
10    /// XML is not well formed.
11    MalformedXml,
12    /// A document type declaration is forbidden.
13    DoctypeForbidden,
14    /// XML exceeds the configured node budget.
15    NodeLimitExceeded,
16    /// XML exceeds the configured element-depth budget.
17    DepthLimitExceeded,
18    /// One element exceeds the configured attribute budget.
19    AttributeLimitExceeded,
20    /// The root is not an ILCD process dataset.
21    UnexpectedRoot,
22    /// The namespaced ILCD+EPD version marker is absent.
23    MissingFormatVersion,
24    /// The adapter does not support the declared ILCD+EPD version.
25    UnsupportedFormatVersion,
26    /// The process dataset has no ILCD UUID.
27    MissingUuid,
28    /// The process dataset UUID is not in canonical UUID form.
29    InvalidUuid,
30    /// The process dataset has no non-empty base name.
31    MissingName,
32    /// An EPD module attribute is not an ISO 22057 module code.
33    InvalidInformationModule,
34}
35
36/// ILCD+EPD parse failure with a stable category and diagnostic detail.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct ParseError {
39    kind: ParseErrorKind,
40    detail: String,
41}
42
43impl ParseError {
44    pub(crate) fn new(kind: ParseErrorKind, detail: impl Into<String>) -> Self {
45        Self {
46            kind,
47            detail: detail.into(),
48        }
49    }
50
51    /// Stable failure category.
52    #[must_use]
53    pub const fn kind(&self) -> ParseErrorKind {
54        self.kind
55    }
56
57    /// Human-readable failure detail. Do not parse this string as an API.
58    #[must_use]
59    pub fn detail(&self) -> &str {
60        &self.detail
61    }
62}
63
64impl fmt::Display for ParseError {
65    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
66        write!(
67            formatter,
68            "ILCD+EPD parse error ({:?}): {}",
69            self.kind, self.detail
70        )
71    }
72}
73
74impl Error for ParseError {}