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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#[macro_use]
extern crate failure;
extern crate parity_wasm as wasm;
extern crate twiggy_ir as ir;
use std::fmt;
use std::io;
use std::str::FromStr;
#[derive(Debug)]
pub struct Error {
inner: Box<ErrorInner>,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.inner)
}
}
impl failure::Fail for Error {
fn cause(&self) -> Option<&failure::Fail> {
self.inner.cause()
}
fn backtrace(&self) -> Option<&failure::Backtrace> {
self.inner.backtrace()
}
}
#[derive(Debug, Fail)]
enum ErrorInner {
#[fail(display = "{}", _0)]
Msg(String),
#[fail(display = "I/O error: {}", _0)]
Io(#[cause] io::Error),
#[fail(display = "WASM error: {}", _0)]
Wasm(#[cause] wasm::elements::Error),
#[fail(display = "formatting error: {}", _0)]
Fmt(#[cause] fmt::Error),
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error {
inner: Box::new(ErrorInner::Io(e)),
}
}
}
impl From<wasm::elements::Error> for Error {
fn from(e: wasm::elements::Error) -> Error {
Error {
inner: Box::new(ErrorInner::Wasm(e)),
}
}
}
impl From<fmt::Error> for Error {
fn from(e: fmt::Error) -> Error {
Error {
inner: Box::new(ErrorInner::Fmt(e)),
}
}
}
impl Error {
pub fn with_msg<S: Into<String>>(msg: S) -> Error {
Error {
inner: Box::new(ErrorInner::Msg(msg.into())),
}
}
}
#[test]
fn size_of_error_is_one_word() {
use std::mem;
assert_eq!(mem::size_of::<Error>(), mem::size_of::<usize>());
}
pub trait Analyze {
type Data: Emit;
fn analyze(items: &mut ir::Items) -> Result<Self::Data, Error>;
}
#[derive(Clone, Copy, Debug)]
pub enum OutputFormat {
Text,
Json,
}
impl Default for OutputFormat {
fn default() -> OutputFormat {
OutputFormat::Text
}
}
impl FromStr for OutputFormat {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"text" => Ok(OutputFormat::Text),
"json" => Ok(OutputFormat::Json),
_ => Err(Error::with_msg(format!("Unknown output format: {}", s))),
}
}
}
pub trait Emit {
fn emit(
&self,
items: &ir::Items,
destination: &mut io::Write,
format: OutputFormat,
) -> Result<(), Error> {
match format {
OutputFormat::Text => self.emit_text(items, destination),
OutputFormat::Json => self.emit_json(items, destination),
}
}
fn emit_text(&self, items: &ir::Items, destination: &mut io::Write) -> Result<(), Error>;
fn emit_json(&self, items: &ir::Items, destination: &mut io::Write) -> Result<(), Error>;
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}