rune/
support.rs

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! This module is primarily provided to support test cases and examples. It is
//! not intended for end-users and might change at any time.

#[doc(inline)]
pub use anyhow::Context;

#[cfg(not(feature = "std"))]
#[doc(inline)]
pub use self::no_std::{Error, Result};
#[cfg(feature = "std")]
#[doc(inline)]
pub use anyhow::{Error, Result};

#[cfg(not(feature = "std"))]
pub(crate) mod no_std {
    use core::fmt;

    use crate::alloc;
    use crate::build;
    use crate::compile;
    use crate::runtime;
    #[cfg(test)]
    use crate::tests;

    /// Type alias for for results which uses [`Error`] by default.
    ///
    /// For errors which aren't automatically captures, you should map them
    /// using [`Error::msg`].
    pub type Result<T, E = Error> = core::result::Result<T, E>;

    /// Error kind which supports capturing any toplevel errors produced by
    /// Rune.
    #[derive(Debug)]
    pub struct Error {
        kind: ErrorKind,
    }

    impl Error {
        /// Create a new error object from a printable error message.
        pub fn msg<M>(message: M) -> Self
        where
            M: fmt::Display + fmt::Debug + Send + Sync + 'static,
        {
            Self {
                kind: ErrorKind::Custom(anyhow::Error::msg(message)),
            }
        }
    }

    impl From<alloc::Error> for Error {
        fn from(error: alloc::Error) -> Self {
            Self {
                kind: ErrorKind::Alloc(error),
            }
        }
    }

    impl From<compile::ContextError> for Error {
        fn from(error: compile::ContextError) -> Self {
            Self {
                kind: ErrorKind::Context(error),
            }
        }
    }

    impl From<compile::Error> for Error {
        fn from(error: compile::Error) -> Self {
            Self {
                kind: ErrorKind::Compile(error),
            }
        }
    }

    impl From<build::BuildError> for Error {
        fn from(error: build::BuildError) -> Self {
            Self {
                kind: ErrorKind::Build(error),
            }
        }
    }

    impl From<runtime::VmError> for Error {
        fn from(error: runtime::VmError) -> Self {
            Self {
                kind: ErrorKind::Vm(error),
            }
        }
    }

    impl From<runtime::RuntimeError> for Error {
        fn from(error: runtime::RuntimeError) -> Self {
            Self {
                kind: ErrorKind::Runtime(error),
            }
        }
    }

    impl From<anyhow::Error> for Error {
        fn from(error: anyhow::Error) -> Self {
            Self {
                kind: ErrorKind::Custom(error),
            }
        }
    }

    #[cfg(test)]
    impl From<tests::TestError> for Error {
        fn from(error: tests::TestError) -> Self {
            Self {
                kind: ErrorKind::Test(error),
            }
        }
    }

    impl fmt::Display for Error {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match &self.kind {
                ErrorKind::Alloc(error) => error.fmt(f),
                ErrorKind::Context(error) => error.fmt(f),
                ErrorKind::Compile(error) => error.fmt(f),
                ErrorKind::Build(error) => error.fmt(f),
                ErrorKind::Runtime(error) => error.fmt(f),
                ErrorKind::Vm(error) => error.fmt(f),
                ErrorKind::Custom(error) => error.fmt(f),
                #[cfg(test)]
                ErrorKind::Test(error) => error.fmt(f),
            }
        }
    }

    #[derive(Debug)]
    enum ErrorKind {
        Alloc(alloc::Error),
        Context(compile::ContextError),
        Compile(compile::Error),
        Build(build::BuildError),
        Vm(runtime::VmError),
        Runtime(runtime::RuntimeError),
        Custom(anyhow::Error),
        #[cfg(test)]
        Test(tests::TestError),
    }

    impl core::error::Error for Error {
        fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
            match &self.kind {
                ErrorKind::Alloc(error) => Some(error),
                ErrorKind::Context(error) => Some(error),
                ErrorKind::Compile(error) => Some(error),
                ErrorKind::Build(error) => Some(error),
                ErrorKind::Vm(error) => Some(error),
                ErrorKind::Runtime(error) => Some(error),
                ErrorKind::Custom(error) => Some(error.as_ref()),
                #[cfg(test)]
                ErrorKind::Test(error) => Some(error),
            }
        }
    }
}