Add rustfmt.toml from substrate repo (#161)

* Add rustfmt.toml from substrate repo

* Apply rustfmt to code base

* Fix formatting

* Move rustfmt job to the top
This commit is contained in:
Alexander Theißen
2021-07-27 14:46:28 +02:00
committed by GitHub
parent 77ad07e347
commit a0b548b37d
29 changed files with 1375 additions and 1196 deletions
+58 -39
View File
@@ -1,13 +1,12 @@
//! Experimental build tool for cargo
use pwasm_utils::{build, BuildError, SourceTarget, TargetRuntime, logger};
use pwasm_utils::{build, logger, BuildError, SourceTarget, TargetRuntime};
mod source;
use std::{fs, io};
use std::path::PathBuf;
use std::{fs, io, path::PathBuf};
use clap::{App, Arg, crate_version};
use clap::{crate_version, App, Arg};
use parity_wasm::elements;
#[derive(Debug)]
@@ -25,9 +24,17 @@ impl std::fmt::Display for Error {
match self {
Io(io) => write!(f, "Generic i/o error: {}", io),
FailedToCopy(msg) => write!(f, "{}. Have you tried to run \"cargo build\"?", msg),
Decoding(err, file) => write!(f, "Decoding error ({}). Must be a valid wasm file {}. Pointed wrong file?", err, file),
Encoding(err) => write!(f, "Encoding error ({}). Almost impossible to happen, no free disk space?", err),
Build(err) => write!(f, "Build error: {}", err)
Decoding(err, file) => write!(
f,
"Decoding error ({}). Must be a valid wasm file {}. Pointed wrong file?",
err, file
),
Encoding(err) => write!(
f,
"Encoding error ({}). Almost impossible to happen, no free disk space?",
err
),
Build(err) => write!(f, "Build error: {}", err),
}
}
}
@@ -41,21 +48,23 @@ pub fn wasm_path(input: &source::SourceInput) -> String {
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(
match input.target() {
SourceTarget::Emscripten => source::EMSCRIPTEN_TRIPLET,
SourceTarget::Unknown => source::UNKNOWN_TRIPLET,
}
);
cargo_path.push(match input.target() {
SourceTarget::Emscripten => source::EMSCRIPTEN_TRIPLET,
SourceTarget::Unknown => source::UNKNOWN_TRIPLET,
});
cargo_path.push("release");
cargo_path.push(format!("{}.wasm", wasm_name));
let mut target_path = PathBuf::from(input.target_dir());
target_path.push(format!("{}.wasm", input.final_name()));
fs::copy(cargo_path.as_path(), target_path.as_path())
.map_err(|io| Error::FailedToCopy(
format!("Failed to copy '{}' to '{}': {}", cargo_path.display(), target_path.display(), io)
))?;
fs::copy(cargo_path.as_path(), target_path.as_path()).map_err(|io| {
Error::FailedToCopy(format!(
"Failed to copy '{}' to '{}': {}",
cargo_path.display(),
target_path.display(),
io
))
})?;
Ok(())
}
@@ -121,13 +130,18 @@ fn do_main() -> Result<(), Error> {
let mut source_input = source::SourceInput::new(target_dir, wasm_binary);
let source_target_val = matches.value_of("source_target").unwrap_or_else(|| source::EMSCRIPTEN_TRIPLET);
let source_target_val =
matches.value_of("source_target").unwrap_or_else(|| source::EMSCRIPTEN_TRIPLET);
if source_target_val == source::UNKNOWN_TRIPLET {
source_input = source_input.unknown()
} else if source_target_val == source::EMSCRIPTEN_TRIPLET {
source_input = source_input.emscripten()
} else {
eprintln!("--target can be: '{}' or '{}'", source::EMSCRIPTEN_TRIPLET, source::UNKNOWN_TRIPLET);
eprintln!(
"--target can be: '{}' or '{}'",
source::EMSCRIPTEN_TRIPLET,
source::UNKNOWN_TRIPLET
);
::std::process::exit(1);
}
@@ -139,29 +153,34 @@ fn do_main() -> Result<(), Error> {
let path = wasm_path(&source_input);
let module = parity_wasm::deserialize_file(&path)
.map_err(|e| Error::Decoding(e, path.to_string()))?;
let module =
parity_wasm::deserialize_file(&path).map_err(|e| Error::Decoding(e, path.to_string()))?;
let runtime_type_version = if let (Some(runtime_type), Some(runtime_version))
= (matches.value_of("runtime_type"), matches.value_of("runtime_version")) {
let runtime_type_version = if let (Some(runtime_type), Some(runtime_version)) =
(matches.value_of("runtime_type"), matches.value_of("runtime_version"))
{
let mut ty: [u8; 4] = Default::default();
let runtime_bytes = runtime_type.as_bytes();
if runtime_bytes.len() != 4 {
panic!("--runtime-type should be equal to 4 bytes");
}
ty.copy_from_slice(runtime_bytes);
let version: u32 = runtime_version.parse()
.expect("--runtime-version should be a positive integer");
let version: u32 =
runtime_version.parse().expect("--runtime-version should be a positive integer");
Some((ty, version))
} else {
None
};
let public_api_entries: Vec<_> = matches.value_of("public_api")
let public_api_entries: Vec<_> = matches
.value_of("public_api")
.map(|val| val.split(',').collect())
.unwrap_or_default();
let target_runtime = match matches.value_of("target-runtime").expect("target-runtime has a default value; qed") {
let target_runtime = match matches
.value_of("target-runtime")
.expect("target-runtime has a default value; qed")
{
"pwasm" => TargetRuntime::pwasm(),
"substrate" => TargetRuntime::substrate(),
_ => unreachable!("all possible values are enumerated in clap config; qed"),
@@ -173,21 +192,22 @@ fn do_main() -> Result<(), Error> {
runtime_type_version,
&public_api_entries,
matches.is_present("enforce_stack_adjustment"),
matches.value_of("shrink_stack").unwrap_or_else(|| "49152").parse()
matches
.value_of("shrink_stack")
.unwrap_or_else(|| "49152")
.parse()
.expect("New stack size is not valid u32"),
matches.is_present("skip_optimization"),
&target_runtime,
).map_err(Error::Build)?;
)
.map_err(Error::Build)?;
if let Some(save_raw_path) = matches.value_of("save_raw") {
parity_wasm::serialize_to_file(save_raw_path, module.clone()).map_err(Error::Encoding)?;
}
if let Some(ctor_module) = ctor_module {
parity_wasm::serialize_to_file(
&path,
ctor_module,
).map_err(Error::Encoding)?;
parity_wasm::serialize_to_file(&path, ctor_module).map_err(Error::Encoding)?;
} else {
parity_wasm::serialize_to_file(&path, module).map_err(Error::Encoding)?;
}
@@ -204,11 +224,10 @@ fn main() {
#[cfg(test)]
mod tests {
use tempdir::TempDir;
use std::fs;
use tempdir::TempDir;
use super::process_output;
use super::source::SourceInput;
use super::{process_output, source::SourceInput};
#[test]
fn processes_cargo_output() {
@@ -230,8 +249,8 @@ mod tests {
process_output(&input).expect("process output failed");
assert!(
fs::metadata(tmp_dir.path().join("example-wasm.wasm")).expect("metadata failed").is_file()
)
assert!(fs::metadata(tmp_dir.path().join("example-wasm.wasm"))
.expect("metadata failed")
.is_file())
}
}
+1 -6
View File
@@ -16,12 +16,7 @@ pub struct SourceInput<'a> {
impl<'a> SourceInput<'a> {
pub fn new<'b>(target_dir: &'b str, bin_name: &'b str) -> SourceInput<'b> {
SourceInput {
target_dir,
bin_name,
final_name: bin_name,
target: SourceTarget::Emscripten,
}
SourceInput { target_dir, bin_name, final_name: bin_name, target: SourceTarget::Emscripten }
}
pub fn unknown(mut self) -> Self {