Debian Codegraph @

crates/wasm-mutate/src/error.rs

raw
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 pub fn new(kind: ErrorKind) -> Self {
11 kind.into()
12 }
13
14 /// Construct a new parse error.
15 pub fn parse(err: wasmparser::BinaryReaderError) -> Self {
16 err.into()
17 }
18
19 /// Construct a "no mutations applicable" error.
20 pub fn no_mutations_applicable() -> Self {
21 ErrorKind::NoMutationsApplicable.into()
22 }
23
24 /// Construct an "out of fuel" error.
25 pub fn out_of_fuel() -> Self {
26 ErrorKind::OutOfFuel.into()
27 }
28
29 /// Construct an unsupported error.
30 pub fn unsupported(msg: impl Into<String>) -> Self {
31 ErrorKind::Unsupported(msg.into()).into()
32 }
33
34 /// Construct another kind of `Error`.
35 pub fn other(err: impl Into<String>) -> Self {
36 ErrorKind::Other(err.into()).into()
37 }
38
39 /// Get the kind of error that this is.
40 pub fn kind(&self) -> &ErrorKind {
41 &*self.kind
42 }
43 }
44
45 impl From<ErrorKind> for Error {
46 fn from(kind: ErrorKind) -> Self {
47 Error {
48 kind: Box::new(kind),
49 }
50 }
51 }
52
53 impl From<wasmparser::BinaryReaderError> for Error {
54 fn from(e: wasmparser::BinaryReaderError) -> Self {
55 ErrorKind::Parse(e).into()
56 }
57 }
58
59 impl<E> From<wasm_encoder::reencode::Error<E>> for Error
60 where
61 E: Into<Error> + std::fmt::Display,
62 {
63 fn from(e: wasm_encoder::reencode::Error<E>) -> Self {
64 match e {
65 wasm_encoder::reencode::Error::ParseError(e) => Error::parse(e),
66 wasm_encoder::reencode::Error::UserError(e) => e.into(),
67 other => Error::other(other.to_string()),
68 }
69 }
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::BinaryReaderError),
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>>;