use std::fmt;
#[derive(Debug)]
#[non_exhaustive]
pub struct PreparedToolsError {
repr: PreparedToolsErrorRepr,
}
#[derive(Debug)]
enum PreparedToolsErrorRepr {
Tools {
source: Box<dyn std::error::Error + Send + Sync>,
},
Picker {
source: Box<dyn std::error::Error + Send + Sync>,
},
#[cfg(test)]
Index {
source: Box<dyn std::error::Error + Send + Sync>,
},
}
impl PreparedToolsError {
pub(crate) fn tools(
source: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> PreparedToolsError {
PreparedToolsError {
repr: PreparedToolsErrorRepr::Tools {
source: source.into(),
},
}
}
pub(crate) fn picker(
source: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> PreparedToolsError {
PreparedToolsError {
repr: PreparedToolsErrorRepr::Picker {
source: source.into(),
},
}
}
#[cfg(test)]
pub(crate) fn index(
source: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> PreparedToolsError {
PreparedToolsError {
repr: PreparedToolsErrorRepr::Index {
source: source.into(),
},
}
}
}
impl fmt::Display for PreparedToolsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.repr {
PreparedToolsErrorRepr::Tools { .. } => f.write_str("assemble the live tool registry"),
PreparedToolsErrorRepr::Picker { .. } => f.write_str("build the tool picker index"),
#[cfg(test)]
PreparedToolsErrorRepr::Index { .. } => f.write_str("rebuild the tool picker index"),
}
}
}
impl std::error::Error for PreparedToolsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.repr {
PreparedToolsErrorRepr::Tools { source }
| PreparedToolsErrorRepr::Picker { source } => Some(source.as_ref()),
#[cfg(test)]
PreparedToolsErrorRepr::Index { source } => Some(source.as_ref()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PreparedToolsErrorKind {
Tools,
Picker,
Index,
}
impl PreparedToolsError {
#[must_use]
pub fn kind(&self) -> PreparedToolsErrorKind {
match &self.repr {
PreparedToolsErrorRepr::Tools { .. } => PreparedToolsErrorKind::Tools,
PreparedToolsErrorRepr::Picker { .. } => PreparedToolsErrorKind::Picker,
#[cfg(test)]
PreparedToolsErrorRepr::Index { .. } => PreparedToolsErrorKind::Index,
}
}
}