Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ members = [
"pbjson-build",
"pbjson-test",
"pbjson-types",
"pbjson-types/code-gen",
]
4 changes: 0 additions & 4 deletions pbjson-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,3 @@ serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
serde_json = "1.0"

[build-dependencies] # In alphabetical order
prost-build = "0.11"
pbjson-build = { path = "../pbjson-build", version = "0.5" }
12 changes: 12 additions & 0 deletions pbjson-types/code-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "code_gen"
version = "0.1.0"
edition = "2021"
license = "MIT"
publish = false

[dependencies]
clap = { version = "3", features = ["derive"] }
prost-build = "0.11"
pbjson-build = { path = "../../pbjson-build", version = "0.5" }
tempfile = "3.3.0"
31 changes: 25 additions & 6 deletions pbjson-types/build.rs → pbjson-types/code-gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
//! Compiles Protocol Buffers and FlatBuffers schema definitions into
//! native Rust types.
//!
//! This is kept as a separate binary to generate code for manual check-in, instead of running at
//! compile time as `build.rs` -- that way downstream consumers do not require the build
//! dependencies (espeically protoc) and can import rust sources directly.

use std::env;
use std::path::PathBuf;

use clap::Parser;

type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Parser)]
struct Args {
/// The path of the directory containing the protobuf sources.
#[clap(short, long, default_value = concat!(env!("CARGO_MANIFEST_DIR"), "/../protos"))]
input_proto_dir: PathBuf,

/// The destination directory for generated code.
#[clap(short, long, default_value = concat!(env!("CARGO_MANIFEST_DIR"), "/../src/pb/"))]
output_dir: PathBuf,
}

fn main() -> Result<()> {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("protos");
let args = Args::parse();
let root = args.input_proto_dir;
let out_dir = &args.output_dir;

let proto_files = vec![root.join("google/protobuf/types.proto")];

// Tell cargo to recompile if any of these proto files are changed
for proto_file in &proto_files {
println!("cargo:rerun-if-changed={}", proto_file.display());
}
std::fs::create_dir_all(out_dir)?;

let descriptor_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("proto_descriptor.bin");
let temp_dir = tempfile::tempdir()?;
let descriptor_path = temp_dir.path().join("proto_descriptor.bin");
prost_build::Config::new()
.file_descriptor_set_path(&descriptor_path)
.compile_well_known_types()
.disable_comments(&["."])
.bytes(&[".google"])
.out_dir(out_dir)
.compile_protos(&proto_files, &[root])?;

let descriptor_set = std::fs::read(descriptor_path)?;
Expand All @@ -45,6 +63,7 @@ fn main() -> Result<()> {
".google.protobuf.UInt32Value",
".google.protobuf.UInt64Value",
])
.out_dir(out_dir)
.build(&[".google"])?;

Ok(())
Expand Down
9 changes: 1 addition & 8 deletions pbjson-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@
clippy::enum_variant_names,
clippy::use_self
)]
mod pb {
pub mod google {
pub mod protobuf {
include!(concat!(env!("OUT_DIR"), "/google.protobuf.rs"));
include!(concat!(env!("OUT_DIR"), "/google.protobuf.serde.rs"));
}
}
}
mod pb;

mod duration;
mod list_value;
Expand Down
Loading