use core::fmt;
use crate::spec::VersionId;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Unsupported {
engine: &'static str,
collation: String,
reason: &'static str,
},
VersionMismatch {
source: VersionId,
local: VersionId,
},
FeatureDisabled {
feature: &'static str,
},
InputLimit {
engine: &'static str,
reason: &'static str,
},
}
impl Error {
pub(crate) fn unsupported(
engine: &'static str,
collation: impl Into<String>,
reason: &'static str,
) -> Self {
Self::Unsupported {
engine,
collation: collation.into(),
reason,
}
}
#[cfg(feature = "oracle-uca")]
pub(crate) fn input_limit(engine: &'static str, reason: &'static str) -> Self {
Self::InputLimit { engine, reason }
}
#[must_use]
pub const fn is_fallback_required(&self) -> bool {
matches!(
self,
Self::Unsupported { .. }
| Self::VersionMismatch { .. }
| Self::FeatureDisabled { .. }
| Self::InputLimit { .. }
)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unsupported {
engine,
collation,
reason,
} => write!(
f,
"unsupported {engine} collation \"{collation}\": {reason}; compare on the source instead"
),
Self::VersionMismatch { source, local } => write!(
f,
"collation data version mismatch: source {source}, local {local}; compare on the source instead"
),
Self::FeatureDisabled { feature } => {
write!(
f,
"backend disabled at compile time; enable feature {feature:?}"
)
}
Self::InputLimit { engine, reason } => write!(
f,
"{engine} collation cannot compare this input: {reason}; compare on the source instead"
),
}
}
}
impl core::error::Error for Error {}