laser_dac/
error.rs

1//! Crate-level error types.
2
3use std::error::Error as StdError;
4
5/// Crate-level error type.
6#[derive(thiserror::Error, Debug)]
7pub enum Error {
8    /// An error with context describing what operation failed.
9    #[error("{context}: {source}")]
10    Context {
11        context: String,
12        #[source]
13        source: Box<dyn StdError + Send + Sync>,
14    },
15
16    /// A simple error message.
17    #[error("{0}")]
18    Message(String),
19}
20
21impl Error {
22    /// Create a simple message error.
23    pub fn msg(msg: impl Into<String>) -> Self {
24        Self::Message(msg.into())
25    }
26
27    /// Create an error with context wrapping another error.
28    pub fn context(
29        context: impl Into<String>,
30        source: impl StdError + Send + Sync + 'static,
31    ) -> Self {
32        Self::Context {
33            context: context.into(),
34            source: Box::new(source),
35        }
36    }
37}
38
39/// Crate-level result type.
40pub type Result<T> = std::result::Result<T, Error>;