|
| 1 | +use error::Error; |
| 2 | +use parity_wasm::elements::{self, Serialize}; |
| 3 | +use wasm_snip; |
| 4 | + |
| 5 | +#[derive(Clone, Debug, StructOpt)] |
| 6 | +pub struct WasmSnipConfig { |
| 7 | + input: String, |
| 8 | + #[structopt(long = "output", short = "o")] |
| 9 | + pub(crate) output: Option<String>, |
| 10 | + functions: Vec<String>, |
| 11 | + #[structopt(long = "pattern", short = "p")] |
| 12 | + patterns: Vec<String>, |
| 13 | + #[structopt(long = "snip_rust_fmt_code")] |
| 14 | + snip_rust_fmt_code: bool, |
| 15 | + #[structopt(long = "snip_rust_panicking_code")] |
| 16 | + snip_rust_panicking_code: bool, |
| 17 | +} |
| 18 | + |
| 19 | +impl Into<wasm_snip::Options> for WasmSnipConfig { |
| 20 | + fn into(self) -> wasm_snip::Options { |
| 21 | + wasm_snip::Options { |
| 22 | + input: ::std::path::PathBuf::from(self.input), |
| 23 | + functions: self.functions, |
| 24 | + patterns: self.patterns, |
| 25 | + snip_rust_fmt_code: self.snip_rust_fmt_code, |
| 26 | + snip_rust_panicking_code: self.snip_rust_panicking_code, |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +pub(crate) fn run_wasm_snip(opts: WasmSnipConfig) -> Result<(), Error> { |
| 32 | + let module = wasm_snip::snip(opts.clone().into())?; |
| 33 | + |
| 34 | + if let Some(output) = opts.output { |
| 35 | + elements::serialize_to_file(output, module)?; |
| 36 | + } else { |
| 37 | + let stdout = ::std::io::stdout(); |
| 38 | + let mut stdout = stdout.lock(); |
| 39 | + module.serialize(&mut stdout)?; |
| 40 | + } |
| 41 | + Ok(()) |
| 42 | +} |
0 commit comments