1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! Error-handling utilities and types for use in
//! [Calypso](https://github.com/calypso-lang/calypso).
#![doc(html_root_url = "https://calypso-lang.github.io/rustdoc/calypso_error/index.html")]
#![warn(clippy::pedantic)]

use std::fmt::{Debug, Display};

use thiserror::Error;

/// The Calypso error type.
#[derive(Error, Debug)]
pub enum CalError {
    /// IO errors
    #[error("i/o error")]
    Io(#[from] std::io::Error),
    /// UTF-8 decoding errors
    #[error("utf-8 decoding error")]
    FromUtf8(#[from] std::string::FromUtf8Error),
    /// Formatting errors
    #[error("formatting error")]
    Fmt(#[from] std::fmt::Error),
    /// Any other error, using [`anyhow`]
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

impl CalError {
    /// Try to downcast the error into a concrete type, if the error is a
    /// [`CalError::Other`].
    ///
    /// # Errors
    ///
    /// `self` is returned if the error could not be downcast.
    pub fn try_downcast<E>(self) -> Result<E, Self>
    where
        E: Display + Debug + Send + Sync + 'static,
    {
        if let CalError::Other(err) = self {
            let x = err.downcast()?;
            Ok(x)
        } else {
            Err(self)
        }
    }

    /// Try to downcast a reference to the error into a reference to a concrete
    /// type, if the error is a [`CalError::Other`].
    #[must_use]
    pub fn try_downcast_ref<E>(&self) -> Option<&E>
    where
        E: Display + Debug + Send + Sync + 'static,
    {
        if let CalError::Other(err) = self {
            err.downcast_ref()
        } else {
            None
        }
    }

    /// Try to downcast a mutable reference to the error into a mutable
    /// reference to a concrete type, if the error is a [`CalError::Other`].
    pub fn try_downcast_mut<E>(&mut self) -> Option<&mut E>
    where
        E: Display + Debug + Send + Sync + 'static,
    {
        if let CalError::Other(err) = self {
            err.downcast_mut()
        } else {
            None
        }
    }
}

/// A handy alias for [`Result<T, CalError>`], genericized over `T`.
pub type CalResult<T> = Result<T, CalError>;