-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dapi-grpc): compile proto to rust using prost (#1028)
Co-authored-by: Quantum Explorer <[email protected]> Co-authored-by: markin.io <[email protected]>
- Loading branch information
1 parent
acaad44
commit 5d8c51e
Showing
26 changed files
with
301 additions
and
9,113 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.