Skip to content

Commit

Permalink
[Enhancement] Jangan taruh autogen proto file di dalam src, tapi taru…
Browse files Browse the repository at this point in the history
…h di `OUT_DIR`

lalu di-include via `mod.rs`, hal ini dikarenakan oleh: rust-lang/cargo#5584

Tambah beberapa proto message.
  • Loading branch information
robin committed Feb 3, 2019
1 parent f04ce74 commit 8bc37f2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
27 changes: 25 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
extern crate protoc_rust;

use protoc_rust::{Args, Customize};
use std::{
env,
fs,
// fs::File,
// io::{BufRead, BufReader, BufWriter},
};

fn main() {
let out_dir = env::var("OUT_DIR").expect("Cannot get OUT_DIR");

protoc_rust::run(Args {
out_dir: "src/protos",
out_dir: &out_dir,
input: &["src/protos/apf.proto"],
includes: &["src/protos"],
customize: Customize {
Expand All @@ -14,5 +22,20 @@ fn main() {
})
.expect("Protoc compile");

println!("cargo:rerun-if-changed={}", "src/protos");
let path = format!("{}/apf.rs", out_dir);
let content = fs::read_to_string(&path).expect("cannot read autogen protobuf apf.rs");
let mut new_content = vec![];

for line in content.split("\n") {
if line.starts_with("#![") {
continue;
}
new_content.push(line);
}

let new_content: String = new_content.join("\n");

let _ = fs::write(&path, new_content);

println!("cargo:rerun-if-changed={}", "src/protos/apf.proto");
}
15 changes: 15 additions & 0 deletions src/protos/apf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,18 @@ message Transfer {
int64 seed = 5;
}

message Credit {
int64 account = 1;
double amount = 2;
int64 timestamp = 3;
int64 seed = 4;
}


message Debit {
int64 account = 1;
double amount = 2;
int64 timestamp = 3;
int64 seed = 4;
}

27 changes: 25 additions & 2 deletions src/protos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
mod apf;
//! Wrapper untuk meng-import struct dari protobuf autogenerated output files.
//!

pub use self::apf::Transfer;
#![allow(bare_trait_objects)]
#![allow(renamed_and_removed_lints)]
#![allow(unknown_lints)]
#![allow(clippy)]

#![cfg_attr(rustfmt, rustfmt_skip)]

#![allow(box_pointers)]
#![allow(dead_code)]
#![allow(missing_docs)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(trivial_casts)]
#![allow(unsafe_code)]
#![allow(unused_imports)]
#![allow(unused_results)]

mod inner {
include!(concat!(env!("OUT_DIR"), "/apf.rs"));
}

pub use self::inner::{Transfer, Credit, Debit};

0 comments on commit 8bc37f2

Please sign in to comment.