@@ -3,39 +3,26 @@ use can_dbc::{Message, Signal, ValDescription, ValueDescription, DBC};
33use heck:: { CamelCase , SnakeCase } ;
44use pad:: PadAdapter ;
55use std:: { fs:: File , io:: BufWriter , io:: Write , path:: PathBuf } ;
6- use structopt:: StructOpt ;
76
87mod includes;
98mod keywords;
109mod pad;
1110
12- #[ derive( Debug , StructOpt ) ]
13- struct Cli {
14- /// Path to a `.dbc` file
15- dbc_path : PathBuf ,
16- /// Target directory to write Rust source file(s) to
17- out_path : PathBuf ,
18- /// Enable debug printing
19- #[ structopt( long) ]
20- debug : bool ,
21- }
22-
23- fn main ( ) -> Result < ( ) > {
24- let args = Cli :: from_args ( ) ;
25- let dbc = std:: fs:: read ( & args. dbc_path )
26- . with_context ( || format ! ( "could not read `{}`" , args. dbc_path. display( ) ) ) ?;
11+ pub fn codegen ( dbc_path : PathBuf , out_path : PathBuf , debug : bool ) -> Result < ( ) > {
12+ let dbc = std:: fs:: read ( & dbc_path)
13+ . with_context ( || format ! ( "could not read `{}`" , dbc_path. display( ) ) ) ?;
2714 let dbc = can_dbc:: DBC :: from_slice ( & dbc)
2815 . map_err ( |e| anyhow ! ( "Could not parse dbc file: {:#?}" , e) ) ?;
29- if args . debug {
16+ if debug {
3017 eprintln ! ( "{:#?}" , dbc) ;
3118 }
3219
3320 ensure ! (
34- args . out_path. is_dir( ) ,
21+ out_path. is_dir( ) ,
3522 "Output path needs to point to a directory"
3623 ) ;
3724
38- let messages_path = args . out_path . join ( "messages.rs" ) ;
25+ let messages_path = out_path. join ( "messages.rs" ) ;
3926 let messages_code =
4027 File :: create ( messages_path) . context ( "Could not create `messages.rs` file" ) ?;
4128 let mut w = BufWriter :: new ( messages_code) ;
@@ -47,10 +34,7 @@ fn main() -> Result<()> {
4734 "#![allow(unused, clippy::let_and_return, clippy::eq_op)]"
4835 ) ?;
4936 writeln ! ( & mut w) ?;
50- let dbc_file_name = args
51- . dbc_path
52- . file_name ( )
53- . unwrap_or_else ( || args. dbc_path . as_ref ( ) ) ;
37+ let dbc_file_name = dbc_path. file_name ( ) . unwrap_or_else ( || dbc_path. as_ref ( ) ) ;
5438 writeln ! (
5539 & mut w,
5640 "//! Message definitions from file `{:?}`" ,
0 commit comments