indexbus_platform_core/
errors.rs

1//! Error types and `Result` aliases for `indexbus-platform-core`.
2
3/// Result type used by this crate.
4pub type Result<T> = core::result::Result<T, Error>;
5use core::fmt;
6
7/// Errors produced by platform lifecycle helpers.
8///
9/// ## Error semantics
10///
11/// This crate intentionally keeps errors lightweight and human-readable:
12///
13/// - Most errors represent configuration validation failures or child thread failures.
14/// - [`Error`] values are suitable for logging and surface-level reporting; they are not
15///   intended for programmatic branching beyond "ok vs error".
16#[derive(Debug)]
17pub enum Error {
18    /// A simple, human-readable error message.
19    Message(String),
20}
21
22impl Error {
23    /// Construct an [`Error::Message`].
24    pub fn msg(message: impl Into<String>) -> Self {
25        Self::Message(message.into())
26    }
27
28    pub(crate) fn panic_message(panic: Box<dyn core::any::Any + Send>) -> Self {
29        if let Some(s) = panic.downcast_ref::<&str>() {
30            return Self::msg(format!("thread panicked: {s}"));
31        }
32        if let Some(s) = panic.downcast_ref::<String>() {
33            return Self::msg(format!("thread panicked: {s}"));
34        }
35        Self::msg("thread panicked".to_string())
36    }
37}
38
39impl fmt::Display for Error {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Error::Message(message) => write!(f, "{message}"),
43        }
44    }
45}
46
47#[cfg(feature = "std")]
48impl std::error::Error for Error {}