From 046453c7f1c22b5b132db40c68ae43ae36bd4adc Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 26 Dec 2017 13:38:07 +0300 Subject: [PATCH] refactor to well-defined source input --- build/src/main.rs | 29 ++++++++++++++++++----------- build/src/source.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 build/src/source.rs diff --git a/build/src/main.rs b/build/src/main.rs index 0c6c406..f57dd50 100644 --- a/build/src/main.rs +++ b/build/src/main.rs @@ -5,6 +5,8 @@ extern crate wasm_utils; extern crate clap; extern crate parity_wasm; +mod source; + use std::{fs, io}; use std::io::Write; use std::path::PathBuf; @@ -28,21 +30,21 @@ impl From for Error { } } -pub fn wasm_path(target_dir: &str, bin_name: &str) -> String { - let mut path = PathBuf::from(target_dir); - path.push(format!("{}.wasm", bin_name)); +pub fn wasm_path(input: &source::SourceInput) -> String { + let mut path = PathBuf::from(input.target_dir()); + path.push(format!("{}.wasm", input.bin_name())); path.to_string_lossy().to_string() } -pub fn process_output(target_dir: &str, bin_name: &str) -> Result<(), Error> { - let mut cargo_path = PathBuf::from(target_dir); - let wasm_name = bin_name.to_string().replace("-", "_"); +pub fn process_output(input: &source::SourceInput) -> Result<(), Error> { + let mut cargo_path = PathBuf::from(input.target_dir()); + let wasm_name = input.bin_name().to_string().replace("-", "_"); cargo_path.push("wasm32-unknown-emscripten"); cargo_path.push("release"); cargo_path.push(format!("{}.wasm", wasm_name)); - let mut target_path = PathBuf::from(target_dir); - target_path.push(format!("{}.wasm", bin_name)); + let mut target_path = PathBuf::from(input.target_dir()); + target_path.push(format!("{}.wasm", input.bin_name())); fs::copy(cargo_path, target_path)?; Ok(()) @@ -83,10 +85,11 @@ fn main() { let target_dir = matches.value_of("target").expect("is required; qed"); let wasm_binary = matches.value_of("wasm").expect("is required; qed"); + let source_input = source::SourceInput::new(target_dir, wasm_binary); - process_output(target_dir, wasm_binary).expect("Failed to process cargo target directory"); + process_output(&source_input).expect("Failed to process cargo target directory"); - let path = wasm_path(target_dir, wasm_binary); + let path = wasm_path(&source_input); let mut module = parity_wasm::deserialize_file(&path).unwrap(); @@ -132,6 +135,7 @@ mod tests { use std::fs; use super::process_output; + use super::source::SourceInput; #[test] fn processes_cargo_output() { @@ -148,7 +152,10 @@ mod tests { f.write(b"\0asm").expect("write file failed"); } - process_output(&tmp_dir.path().to_string_lossy(), "example-wasm").expect("process output failed"); + let path = tmp_dir.path().to_string_lossy(); + let input = SourceInput::new(&path, "example-wasm"); + + process_output(&input).expect("process output failed"); assert!( fs::metadata(tmp_dir.path().join("example-wasm.wasm")).expect("metadata failed").is_file() diff --git a/build/src/source.rs b/build/src/source.rs new file mode 100644 index 0000000..f933aac --- /dev/null +++ b/build/src/source.rs @@ -0,0 +1,44 @@ +//! Configuration of source binaries + +/// Target configiration of previous build step +#[derive(Debug)] +pub enum SourceTarget { + Emscripten, + Unknown, +} + +/// Configuration of previous build step (cargo compilation) +#[derive(Debug)] +pub struct SourceInput<'a> { + target_dir: &'a str, + bin_name: &'a str, + target: SourceTarget, +} + +impl<'a> SourceInput<'a> { + pub fn new<'b>(target_dir: &'b str, bin_name: &'b str) -> SourceInput<'b> { + SourceInput { + target_dir: target_dir, + bin_name: bin_name, + target: SourceTarget::Emscripten, + } + } + + pub fn unknown(mut self) -> Self { + self.target = SourceTarget::Unknown; + self + } + + pub fn emscripten(mut self) -> Self { + self.target = SourceTarget::Emscripten; + self + } + + pub fn target_dir(&self) -> &str { + &self.target_dir + } + + pub fn bin_name(&self) -> &str { + &self.bin_name + } +} \ No newline at end of file