Skip to content

Commit

Permalink
feat(dapi-grpc): compile proto to rust using prost (#1028)
Browse files Browse the repository at this point in the history
Co-authored-by: Quantum Explorer <[email protected]>
Co-authored-by: markin.io <[email protected]>
  • Loading branch information
3 people authored May 5, 2023
1 parent acaad44 commit 5d8c51e
Show file tree
Hide file tree
Showing 26 changed files with 301 additions and 9,113 deletions.
17 changes: 13 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]

members = [
"packages/dapi-grpc",
"packages/rs-dpp",
"packages/wasm-dpp",
"packages/rs-drive",
Expand All @@ -14,5 +15,5 @@ members = [
"packages/feature-flags-contract",
"packages/dpns-contract",
"packages/data-contracts",
"packages/rs-drive-verify-c-binding"
"packages/rs-drive-verify-c-binding",
]
38 changes: 38 additions & 0 deletions packages/dapi-grpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "dapi-grpc"
description = "GRPC client for Dash Platform"
version = "0.25.0-dev.6"
authors = [
"Samuel Westrich <[email protected]>",
"Igor Markin <[email protected]>",
"Łukasz Klimek <[email protected]>",
"Anton Suprunchuk <[email protected]>",
"Ivan Shumkov <[email protected]>",
]
edition = "2021"
license = "MIT"

[dependencies]
prost = { version = "0.11" }

[build-dependencies]
prost-build = { version = "0.11" }

[features]
default = ["core", "platform"]
core = ["core_v0"]
platform = ["platform_v0"]

core_v0 = []
platform_v0 = []

[lib]

[[example]]
name = "core_example"
path = "clients/core/v0/rust/core_example.rs"


[[example]]
name = "platform_example"
path = "clients/platform/v0/rust/platform_example.rs"
65 changes: 65 additions & 0 deletions packages/dapi-grpc/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::{
collections::HashMap,
fs::{create_dir_all, remove_dir_all},
path::PathBuf,
};

fn main() {
generate().expect("failed to compile protobuf definitions");

println!("cargo:rerun-if-changed=./protos");
println!("cargo:rerun-if-changed=./src/core/proto");
println!("cargo:rerun-if-changed=./src/platform/proto");
}

/// Generate Rust definitions from Protobuf definitions
pub fn generate() -> Result<(), std::io::Error> {
// Mapping between protobuf files => output directory
let mut input = HashMap::<PathBuf, PathBuf>::new();
input.insert(
PathBuf::from("protos/core/v0/core.proto"),
PathBuf::from("src/core/proto"),
);
input.insert(
PathBuf::from("protos/platform/v0/platform.proto"),
PathBuf::from("src/platform/proto"),
);

let proto_includes = vec![abs_path(&PathBuf::from("protos"))];

for (proto, dest) in input {
let proto = abs_path(&proto);
let dest = abs_path(&dest);
// Remove old compiled files; ignore errors
if dest.exists() {
remove_dir_all(&dest)?;
}
create_dir_all(&dest)?;

generate1(&[proto], &proto_includes, &dest)?;
}

Ok(())
}

/// Run single generation process.
///
/// All paths must be absolute
fn generate1(
files: &[PathBuf],
proto_includes: &[PathBuf],
out_dir: &PathBuf,
) -> Result<(), std::io::Error> {
let mut pb = prost_build::Config::new();
pb.out_dir(out_dir);
pb.format(true);
pb.compile_protos(files, proto_includes)
}

fn abs_path(path: &PathBuf) -> PathBuf {
if path.is_absolute() {
return path.to_owned();
}

PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(path)
}
3 changes: 3 additions & 0 deletions packages/dapi-grpc/clients/core/v0/rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Rust bindings

Rust definitions can be found in [dapi-grpc/src](../../../../src/) directory.
Loading

0 comments on commit 5d8c51e

Please sign in to comment.