use crate::log;
use crate::{marker::DataMarkerId, prelude::*};
use core::fmt;
use displaydoc::Display;
#[derive(Clone, Copy, Eq, PartialEq, Display, Debug)]
#[non_exhaustive]
pub enum DataErrorKind {
#[displaydoc("Missing data for marker")]
MarkerNotFound,
#[displaydoc("Missing data for identifier")]
IdentifierNotFound,
#[displaydoc("Invalid request")]
InvalidRequest,
#[displaydoc("The data for two markers is not consistent: {0:?} (were they generated in different datagen invocations?)")]
InconsistentData(DataMarkerInfo),
#[displaydoc("Downcast: expected {0}, found")]
Downcast(&'static str),
#[displaydoc("Deserialize")]
Deserialize,
#[displaydoc("Custom")]
Custom,
#[displaydoc("I/O: {0:?}")]
#[cfg(feature = "std")]
Io(std::io::ErrorKind),
}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[non_exhaustive]
pub struct DataError {
pub kind: DataErrorKind,
pub marker: Option<DataMarkerId>,
pub str_context: Option<&'static str>,
pub silent: bool,
}
impl fmt::Display for DataError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ICU4X data error")?;
if self.kind != DataErrorKind::Custom {
write!(f, ": {}", self.kind)?;
}
if let Some(marker) = self.marker {
write!(f, " (marker: {marker:?})")?;
}
if let Some(str_context) = self.str_context {
write!(f, ": {str_context}")?;
}
Ok(())
}
}
impl DataErrorKind {
#[inline]
pub const fn into_error(self) -> DataError {
DataError {
kind: self,
marker: None,
str_context: None,
silent: false,
}
}
#[inline]
pub const fn with_marker(self, marker: DataMarkerInfo) -> DataError {
self.into_error().with_marker(marker)
}
#[inline]
pub const fn with_str_context(self, context: &'static str) -> DataError {
self.into_error().with_str_context(context)
}
#[inline]
pub fn with_type_context<T>(self) -> DataError {
self.into_error().with_type_context::<T>()
}
#[inline]
pub fn with_req(self, marker: DataMarkerInfo, req: DataRequest) -> DataError {
self.into_error().with_req(marker, req)
}
}
impl DataError {
#[inline]
pub const fn custom(str_context: &'static str) -> Self {
Self {
kind: DataErrorKind::Custom,
marker: None,
str_context: Some(str_context),
silent: false,
}
}
#[inline]
pub const fn with_marker(self, marker: DataMarkerInfo) -> Self {
Self {
kind: self.kind,
marker: Some(marker.id),
str_context: self.str_context,
silent: self.silent,
}
}
#[inline]
pub const fn with_str_context(self, context: &'static str) -> Self {
Self {
kind: self.kind,
marker: self.marker,
str_context: Some(context),
silent: self.silent,
}
}
#[inline]
pub fn with_type_context<T>(self) -> Self {
if !self.silent {
log::warn!("{self}: Type context: {}", core::any::type_name::<T>());
}
self.with_str_context(core::any::type_name::<T>())
}
pub fn with_req(mut self, marker: DataMarkerInfo, req: DataRequest) -> Self {
if req.metadata.silent {
self.silent = true;
}
if !self.silent && self.kind != DataErrorKind::MarkerNotFound {
log::warn!("{self} (marker: {marker:?}, request: {})", req.id);
}
self.with_marker(marker)
}
#[cfg(feature = "std")]
pub fn with_path_context(self, _path: &std::path::Path) -> Self {
if !self.silent {
log::warn!("{self} (path: {_path:?})");
}
self
}
#[cfg_attr(not(feature = "logging"), allow(unused_variables))]
#[inline]
pub fn with_display_context<D: fmt::Display + ?Sized>(self, context: &D) -> Self {
if !self.silent {
log::warn!("{self}: {context}");
}
self
}
#[cfg_attr(not(feature = "logging"), allow(unused_variables))]
#[inline]
pub fn with_debug_context<D: fmt::Debug + ?Sized>(self, context: &D) -> Self {
if !self.silent {
log::warn!("{self}: {context:?}");
}
self
}
#[inline]
pub(crate) fn for_type<T>() -> DataError {
DataError {
kind: DataErrorKind::Downcast(core::any::type_name::<T>()),
marker: None,
str_context: None,
silent: false,
}
}
}
impl core::error::Error for DataError {}
#[cfg(feature = "std")]
impl From<std::io::Error> for DataError {
fn from(e: std::io::Error) -> Self {
log::warn!("I/O error: {e}");
DataErrorKind::Io(e.kind()).into_error()
}
}
pub trait ResultDataError<T>: Sized {
fn allow_identifier_not_found(self) -> Result<Option<T>, DataError>;
}
impl<T> ResultDataError<T> for Result<T, DataError> {
fn allow_identifier_not_found(self) -> Result<Option<T>, DataError> {
match self {
Ok(t) => Ok(Some(t)),
Err(DataError {
kind: DataErrorKind::IdentifierNotFound,
..
}) => Ok(None),
Err(e) => Err(e),
}
}
}