/src/wasm-tools/crates/wasm-mutate/src/error.rs
Line | Count | Source |
1 | | /// An error encountered when choosing or applying a Wasm mutation. |
2 | | #[derive(thiserror::Error, Debug)] |
3 | | #[error(transparent)] |
4 | | pub struct Error { |
5 | | kind: Box<ErrorKind>, |
6 | | } |
7 | | |
8 | | impl Error { |
9 | | /// Construct a new `Error` from an `ErrorKind`. |
10 | 0 | pub fn new(kind: ErrorKind) -> Self { |
11 | 0 | kind.into() |
12 | 0 | } |
13 | | |
14 | | /// Construct a new parse error. |
15 | 0 | pub fn parse(err: wasmparser::Error) -> Self { |
16 | 0 | err.into() |
17 | 0 | } |
18 | | |
19 | | /// Construct a "no mutations applicable" error. |
20 | 716 | pub fn no_mutations_applicable() -> Self { |
21 | 716 | ErrorKind::NoMutationsApplicable.into() |
22 | 716 | } |
23 | | |
24 | | /// Construct an "out of fuel" error. |
25 | 5 | pub fn out_of_fuel() -> Self { |
26 | 5 | ErrorKind::OutOfFuel.into() |
27 | 5 | } |
28 | | |
29 | | /// Construct an unsupported error. |
30 | 254 | pub fn unsupported(msg: impl Into<String>) -> Self { |
31 | 254 | ErrorKind::Unsupported(msg.into()).into() |
32 | 254 | } |
33 | | |
34 | | /// Construct another kind of `Error`. |
35 | 0 | pub fn other(err: impl Into<String>) -> Self { |
36 | 0 | ErrorKind::Other(err.into()).into() |
37 | 0 | } Unexecuted instantiation: <wasm_mutate::error::Error>::other::<alloc::string::String> Unexecuted instantiation: <wasm_mutate::error::Error>::other::<&str> |
38 | | |
39 | | /// Get the kind of error that this is. |
40 | 0 | pub fn kind(&self) -> &ErrorKind { |
41 | 0 | &*self.kind |
42 | 0 | } |
43 | | } |
44 | | |
45 | | impl From<ErrorKind> for Error { |
46 | 1.42k | fn from(kind: ErrorKind) -> Self { |
47 | 1.42k | Error { |
48 | 1.42k | kind: Box::new(kind), |
49 | 1.42k | } |
50 | 1.42k | } |
51 | | } |
52 | | |
53 | | impl From<wasmparser::Error> for Error { |
54 | 446 | fn from(e: wasmparser::Error) -> Self { |
55 | 446 | ErrorKind::Parse(e).into() |
56 | 446 | } |
57 | | } |
58 | | |
59 | | impl<E> From<wasm_encoder::reencode::Error<E>> for Error |
60 | | where |
61 | | E: Into<Error> + std::fmt::Display, |
62 | | { |
63 | 0 | fn from(e: wasm_encoder::reencode::Error<E>) -> Self { |
64 | 0 | match e { |
65 | 0 | wasm_encoder::reencode::Error::ParseError(e) => Error::parse(e), |
66 | 0 | wasm_encoder::reencode::Error::UserError(e) => e.into(), |
67 | 0 | other => Error::other(other.to_string()), |
68 | | } |
69 | 0 | } Unexecuted instantiation: <wasm_mutate::error::Error as core::convert::From<wasm_encoder::reencode::Error<wasm_mutate::error::Error>>>::from Unexecuted instantiation: <wasm_mutate::error::Error as core::convert::From<wasm_encoder::reencode::Error>>::from |
70 | | } |
71 | | |
72 | | impl From<std::convert::Infallible> for Error { |
73 | | fn from(i: std::convert::Infallible) -> Error { |
74 | | match i {} |
75 | | } |
76 | | } |
77 | | |
78 | | /// The kind of error. |
79 | | #[derive(thiserror::Error, Debug)] |
80 | | pub enum ErrorKind { |
81 | | /// Failed to parse the input Wasm module. |
82 | | #[error("Failed to parse the input Wasm module.")] |
83 | | Parse(#[from] wasmparser::Error), |
84 | | |
85 | | /// None of the available mutators are applicable to the input Wasm module |
86 | | #[error("There are not applicable mutations for the input Wasm module.")] |
87 | | NoMutationsApplicable, |
88 | | |
89 | | /// Ran out of fuel before a mutation could be applied successfully. |
90 | | #[error("Out of fuel")] |
91 | | OutOfFuel, |
92 | | |
93 | | /// The Wasm is using an unsupported feature. |
94 | | #[error("Unsupported: {0}")] |
95 | | Unsupported(String), |
96 | | |
97 | | /// Another error. |
98 | | #[error("{0}")] |
99 | | Other(String), |
100 | | } |
101 | | |
102 | | /// A `Result` type that is either `Ok(T)` or `Err(wasm_mutate::Error)`. |
103 | | pub type Result<T, E = Error> = std::result::Result<T, E>; |
104 | | |
105 | | /// A `Result` type for use with `wasm_encoder::reencode` |
106 | | pub type ReencodeResult<T, E = Error> = Result<T, wasm_encoder::reencode::Error<E>>; |