1#[doc(inline)]
5pub use anyhow::Context;
6
7#[cfg(not(feature = "std"))]
8#[doc(inline)]
9pub use self::no_std::{Error, Result};
10#[cfg(feature = "std")]
11#[doc(inline)]
12pub use anyhow::{Error, Result};
13
14#[cfg(not(feature = "std"))]
15pub(crate) mod no_std {
16 use core::fmt;
17
18 use crate::alloc;
19 use crate::build;
20 use crate::compile;
21 use crate::runtime;
22 #[cfg(test)]
23 use crate::tests;
24
25 pub type Result<T, E = Error> = core::result::Result<T, E>;
30
31 #[derive(Debug)]
34 pub struct Error {
35 kind: ErrorKind,
36 }
37
38 impl Error {
39 pub fn msg<M>(message: M) -> Self
41 where
42 M: fmt::Display + fmt::Debug + Send + Sync + 'static,
43 {
44 Self {
45 kind: ErrorKind::Custom(anyhow::Error::msg(message)),
46 }
47 }
48 }
49
50 impl From<alloc::Error> for Error {
51 fn from(error: alloc::Error) -> Self {
52 Self {
53 kind: ErrorKind::Alloc(error),
54 }
55 }
56 }
57
58 impl From<compile::ContextError> for Error {
59 fn from(error: compile::ContextError) -> Self {
60 Self {
61 kind: ErrorKind::Context(error),
62 }
63 }
64 }
65
66 impl From<compile::Error> for Error {
67 fn from(error: compile::Error) -> Self {
68 Self {
69 kind: ErrorKind::Compile(error),
70 }
71 }
72 }
73
74 impl From<build::BuildError> for Error {
75 fn from(error: build::BuildError) -> Self {
76 Self {
77 kind: ErrorKind::Build(error),
78 }
79 }
80 }
81
82 impl From<runtime::VmError> for Error {
83 fn from(error: runtime::VmError) -> Self {
84 Self {
85 kind: ErrorKind::Vm(error),
86 }
87 }
88 }
89
90 impl From<runtime::RuntimeError> for Error {
91 fn from(error: runtime::RuntimeError) -> Self {
92 Self {
93 kind: ErrorKind::Runtime(error),
94 }
95 }
96 }
97
98 impl From<anyhow::Error> for Error {
99 fn from(error: anyhow::Error) -> Self {
100 Self {
101 kind: ErrorKind::Custom(error),
102 }
103 }
104 }
105
106 #[cfg(test)]
107 impl From<tests::TestError> for Error {
108 fn from(error: tests::TestError) -> Self {
109 Self {
110 kind: ErrorKind::Test(error),
111 }
112 }
113 }
114
115 impl fmt::Display for Error {
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 match &self.kind {
118 ErrorKind::Alloc(error) => error.fmt(f),
119 ErrorKind::Context(error) => error.fmt(f),
120 ErrorKind::Compile(error) => error.fmt(f),
121 ErrorKind::Build(error) => error.fmt(f),
122 ErrorKind::Runtime(error) => error.fmt(f),
123 ErrorKind::Vm(error) => error.fmt(f),
124 ErrorKind::Custom(error) => error.fmt(f),
125 #[cfg(test)]
126 ErrorKind::Test(error) => error.fmt(f),
127 }
128 }
129 }
130
131 #[derive(Debug)]
132 enum ErrorKind {
133 Alloc(alloc::Error),
134 Context(compile::ContextError),
135 Compile(compile::Error),
136 Build(build::BuildError),
137 Vm(runtime::VmError),
138 Runtime(runtime::RuntimeError),
139 Custom(anyhow::Error),
140 #[cfg(test)]
141 Test(tests::TestError),
142 }
143
144 impl core::error::Error for Error {
145 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
146 match &self.kind {
147 ErrorKind::Alloc(error) => Some(error),
148 ErrorKind::Context(error) => Some(error),
149 ErrorKind::Compile(error) => Some(error),
150 ErrorKind::Build(error) => Some(error),
151 ErrorKind::Vm(error) => Some(error),
152 ErrorKind::Runtime(error) => Some(error),
153 ErrorKind::Custom(error) => Some(error.as_ref()),
154 #[cfg(test)]
155 ErrorKind::Test(error) => Some(error),
156 }
157 }
158 }
159}