use std::error::Error as StdError;
use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum XbrlError {
#[error("malformed document: {0}")]
Malformed(#[source] Box<dyn StdError + Send + Sync + 'static>),
#[error("not an XBRL document: {0}")]
NotXbrl(String),
#[error("field `{field}` ({concept}): cannot read {value:?} as {target_type}")]
ValueConversion {
field: String,
concept: String,
value: String,
target_type: &'static str,
},
}
impl XbrlError {
pub(crate) fn malformed(source: impl Into<Box<dyn StdError + Send + Sync + 'static>>) -> Self {
XbrlError::Malformed(source.into())
}
}
pub type Result<T> = std::result::Result<T, XbrlError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_conversion_error_names_the_field_the_concept_and_the_value() {
let err = XbrlError::ValueConversion {
field: "assets".to_string(),
concept: "us-gaap:Assets".to_string(),
value: "N/A".to_string(),
target_type: "f64",
};
assert_eq!(
err.to_string(),
"field `assets` (us-gaap:Assets): cannot read \"N/A\" as f64"
);
}
#[test]
fn a_malformed_document_keeps_its_cause() {
let err = XbrlError::malformed("unexpected end of input");
assert!(err.source().is_some());
assert!(err.to_string().contains("unexpected end of input"));
}
}