Skip to content

Commit c94efd7

Browse files
committed
cmd(snip): run wasm-snip command
1 parent be29cf3 commit c94efd7

File tree

6 files changed

+56
-14
lines changed

6 files changed

+56
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ slog-async = "2.3"
2424
structopt = "0.2"
2525
toml = "0.4"
2626
wasm-snip = "0.1.3"
27+
parity-wasm = "0.27.0"

src/command.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use parity_wasm::elements::{self, Serialize};
2+
13
use bindgen;
24
use build;
35
use console::style;
@@ -82,9 +84,9 @@ pub enum Command {
8284

8385
#[derive(Clone, Debug, StructOpt)]
8486
pub struct SnipOpitons {
85-
input: Option<String>,
87+
input: String,
8688
#[structopt(long = "output", short = "o")]
87-
output: Option<String>,
89+
pub(crate) output: Option<String>,
8890
functions: Vec<String>,
8991
#[structopt(long = "pattern", short = "p")]
9092
patterns: Vec<String>,
@@ -94,15 +96,14 @@ pub struct SnipOpitons {
9496
snip_rust_panicking_code: bool,
9597
}
9698

97-
impl From<wasm_snip::Options> for SnipOpitons {
98-
fn from(opts: wasm_snip::Options) -> Self {
99-
SnipOpitons {
100-
input: opts.input.to_str().map(::std::borrow::ToOwned::to_owned),
101-
output: Default::default(),
102-
functions: opts.functions,
103-
patterns: opts.patterns,
104-
snip_rust_fmt_code: opts.snip_rust_fmt_code,
105-
snip_rust_panicking_code: opts.snip_rust_panicking_code,
99+
impl Into<wasm_snip::Options> for SnipOpitons {
100+
fn into(self) -> wasm_snip::Options {
101+
wasm_snip::Options {
102+
input: ::std::path::PathBuf::from(self.input),
103+
functions: self.functions,
104+
patterns: self.patterns,
105+
snip_rust_fmt_code: self.snip_rust_fmt_code,
106+
snip_rust_panicking_code: self.snip_rust_panicking_code,
106107
}
107108
}
108109
}
@@ -156,9 +157,11 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error
156157
login(registry, scope, always_auth, auth_type, &log)
157158
}
158159

159-
Command::Snip(_opts) => {
160-
unimplemented!()
160+
Command::Snip(opts) => {
161+
info!(&log, "Running snip command...");
162+
snip(opts)
161163
}
164+
162165
};
163166

164167
match status {
@@ -348,3 +351,17 @@ fn set_crate_path(path: Option<String>) -> String {
348351

349352
crate_path
350353
}
354+
355+
fn snip(opts: SnipOpitons) -> Result<(), Error> {
356+
let opt = opts.clone();
357+
let module = wasm_snip::snip(opts.into())?;
358+
359+
if let Some(output) = opt.output {
360+
elements::serialize_to_file(output, module)?;
361+
} else {
362+
let stdout = ::std::io::stdout();
363+
let mut stdout = stdout.lock();
364+
module.serialize(&mut stdout)?;
365+
}
366+
Ok(())
367+
}

src/error.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Code related to error handling for wasm-pack
22
use serde_json;
3+
use parity_wasm;
34
use std::borrow::Cow;
45
use std::io;
56
use toml;
7+
use failure;
68

79
#[derive(Debug, Fail)]
810
pub enum Error {
@@ -66,3 +68,22 @@ impl From<toml::de::Error> for Error {
6668
Error::SerdeToml(e)
6769
}
6870
}
71+
72+
impl From<parity_wasm::elements::Error> for Error {
73+
fn from(error: parity_wasm::elements::Error) -> Self {
74+
Error::Cli {
75+
message: format!("{}", "There was an output Error"),
76+
stderr: format!("{}", error),
77+
}
78+
}
79+
}
80+
81+
impl From<failure::Error> for Error {
82+
fn from(error: failure::Error) -> Self {
83+
Error::Cli {
84+
message: format!("{}", "There was an wasm-snip Error"),
85+
stderr: format!("{}", error),
86+
}
87+
}
88+
}
89+

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern crate slog_async;
1616
extern crate slog_term;
1717
extern crate toml;
1818
extern crate wasm_snip;
19+
extern crate parity_wasm;
1920

2021
pub mod bindgen;
2122
pub mod build;

src/logger.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ fn log_file_path(cmd: &Command) -> PathBuf {
3434
Command::Init { path, .. } => path,
3535
Command::Pack { path } => path,
3636
Command::Publish { path } => path,
37-
Command::Snip(_) | Command::Login { .. } => &None,
37+
Command::Snip(opts) => &opts.output,
38+
Command::Login { .. } => &None,
3839
};
3940

4041
// If the path exists attempt to use it, if not default to the current directory

0 commit comments

Comments
 (0)