1- use std:: { env, fs, path:: { Path , PathBuf } } ;
1+ extern crate flatc_rust;
2+ use std:: {
3+ env, fs,
4+ path:: { Path , PathBuf } ,
5+ } ;
26
37/// Normalize path only on Windows, else just return unchanged.
48fn maybe_normalize_windows_path ( path : & Path ) -> PathBuf {
@@ -18,34 +22,74 @@ fn maybe_normalize_windows_path(path: &Path) -> PathBuf {
1822 }
1923}
2024
21- fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
22- let proto_dir = PathBuf :: from ( "proto" ) ;
25+ fn monitor_and_get_files (
26+ dir : & PathBuf ,
27+ env_var : & str ,
28+ extension : & str ,
29+ ) -> Result < ( Vec < PathBuf > , PathBuf ) , Box < dyn std:: error:: Error > > {
2330 let abs = env:: current_dir ( )
2431 . expect ( "cwd" )
25- . join ( & proto_dir )
32+ . join ( dir )
2633 . canonicalize ( )
2734 . map ( |p| maybe_normalize_windows_path ( & p) )
28- . expect ( "canonicalize proto dir" ) ;
35+ . expect ( "canonicalize dir" ) ;
2936
3037 println ! ( "cargo:rerun-if-changed={}" , abs. display( ) ) ;
31- println ! ( "cargo:PROTO_DIR ={}" , abs. display( ) ) ;
38+ println ! ( "cargo:{} ={}" , env_var , abs. display( ) ) ;
3239
33- let mut proto_files = vec ! [ ] ;
40+ let mut files = vec ! [ ] ;
3441 for entry in fs:: read_dir ( & abs) ? {
3542 let path = entry?. path ( ) ;
36- if path. extension ( ) . and_then ( |e| e. to_str ( ) ) == Some ( "proto" ) {
43+ if path. extension ( ) . and_then ( |e| e. to_str ( ) ) == Some ( extension ) {
3744 println ! ( "cargo:rerun-if-changed={}" , path. display( ) ) ;
38- proto_files . push ( path) ;
45+ files . push ( path) ;
3946 }
4047 }
4148
49+ Ok ( ( files, abs) )
50+ }
51+
52+ fn compile_protos ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
53+ let proto_dir = PathBuf :: from ( "proto" ) ;
54+ let ( proto_files, abs) = monitor_and_get_files ( & proto_dir, "PROTO_DIR" , "proto" ) ?;
55+
4256 let out_dir = PathBuf :: from ( env:: var ( "OUT_DIR" ) ?) ;
4357 let mut config = prost_build:: Config :: new ( ) ;
4458 config. out_dir ( & out_dir) ;
4559 config. compile_protos (
46- & proto_files. iter ( ) . map ( |p| p. display ( ) . to_string ( ) ) . collect :: < Vec < _ > > ( ) ,
60+ & proto_files
61+ . iter ( )
62+ . map ( |p| p. display ( ) . to_string ( ) )
63+ . collect :: < Vec < _ > > ( ) ,
4764 & [ abs] ,
4865 ) ?;
4966
5067 Ok ( ( ) )
5168}
69+
70+ fn compile_flatbuffers ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
71+ let flatbuffers_dir = PathBuf :: from ( "flatbuffers" ) ;
72+ let ( flatbuffers_files, abs) =
73+ monitor_and_get_files ( & flatbuffers_dir, "FLATBUFFERS_DIR" , "fbs" ) ?;
74+
75+ let out_dir = PathBuf :: from ( env:: var ( "OUT_DIR" ) ?) ;
76+ flatc_rust:: run ( flatc_rust:: Args {
77+ lang : "rust" ,
78+ inputs : flatbuffers_files
79+ . iter ( )
80+ . map ( |p| p. as_path ( ) )
81+ . collect :: < Vec < _ > > ( )
82+ . as_slice ( ) ,
83+ out_dir : out_dir. as_path ( ) ,
84+ includes : & [ abs. as_path ( ) ] ,
85+ ..Default :: default ( )
86+ } ) ?;
87+
88+ Ok ( ( ) )
89+ }
90+
91+ fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
92+ compile_protos ( ) ?;
93+ compile_flatbuffers ( ) ?;
94+ Ok ( ( ) )
95+ }
0 commit comments