casperlabs-engine-grpc-server 0.17.0

Wasm execution engine for CasperLabs smart contracts.
Documentation
use std::{
    env, fs,
    path::{Path, PathBuf},
};

const PROTOBUF_DIR: &str = "generated_protobuf";
const WORKAROUND_COMMENT: &str = "// workaround for https://github.com/rust-lang/rfcs/issues/752";

// The generated file needs to be sourced via `include!` which doesn't work where the file has top-
// level inner attributes (see https://github.com/rust-lang/rfcs/issues/752).
//
// To work around this issue, we add 'pub mod proto { }' around the contents.
fn wrap_file_contents(target_dir: &Path, file_name_without_suffix: &str) {
    let generated_file = target_dir
        .join(file_name_without_suffix)
        .with_extension("rs");

    let contents = fs::read_to_string(&generated_file)
        .unwrap_or_else(|_| panic!("should read {}", generated_file.display()));
    fs::write(
        &generated_file,
        &format!(
            "pub mod {} {{ {}\n\n{}\n}}",
            file_name_without_suffix, WORKAROUND_COMMENT, contents
        ),
    )
    .unwrap_or_else(|_| panic!("should write {}", generated_file.display()));
}

fn main() {
    println!("cargo:rerun-if-changed=protobuf/io/casperlabs/casper/consensus/state.proto");
    println!("cargo:rerun-if-changed=protobuf/io/casperlabs/ipc/ipc.proto");
    println!("cargo:rerun-if-changed=protobuf/io/casperlabs/ipc/transforms.proto");

    let target_dir = PathBuf::from(format!(
        "{}/../../../../{}",
        env::var("OUT_DIR").expect("should have env var OUT_DIR set"),
        PROTOBUF_DIR
    ));
    fs::create_dir_all(&target_dir)
        .unwrap_or_else(|_| panic!("should create dir {}", target_dir.display()));

    protoc_rust_grpc::run(protoc_rust_grpc::Args {
        out_dir: target_dir.to_str().unwrap(),
        input: &[
            "protobuf/io/casperlabs/casper/consensus/state.proto",
            "protobuf/io/casperlabs/ipc/ipc.proto",
            "protobuf/io/casperlabs/ipc/transforms.proto",
        ],
        includes: &[
            "protobuf/",
            "protobuf/io/casperlabs/casper/consensus",
            "protobuf/io/casperlabs/ipc",
        ],
        rust_protobuf: true,
    })
    .expect("protoc-rust-grpc");

    wrap_file_contents(&target_dir, "state");
    wrap_file_contents(&target_dir, "ipc");
    wrap_file_contents(&target_dir, "transforms");
    wrap_file_contents(&target_dir, "ipc_grpc");
}