Skip to content

Commit cc2ca19

Browse files
committed
Move cli args to main
1 parent e8c3691 commit cc2ca19

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/lib.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,26 @@ use can_dbc::{Message, Signal, ValDescription, ValueDescription, DBC};
33
use heck::{CamelCase, SnakeCase};
44
use pad::PadAdapter;
55
use std::{fs::File, io::BufWriter, io::Write, path::PathBuf};
6-
use structopt::StructOpt;
76

87
mod includes;
98
mod keywords;
109
mod 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 `{:?}`",

src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use anyhow::Result;
2+
use std::path::PathBuf;
3+
use structopt::StructOpt;
4+
5+
#[derive(Debug, StructOpt)]
6+
struct Cli {
7+
/// Path to a `.dbc` file
8+
dbc_path: PathBuf,
9+
/// Target directory to write Rust source file(s) to
10+
out_path: PathBuf,
11+
/// Enable debug printing
12+
#[structopt(long)]
13+
debug: bool,
14+
}
15+
16+
fn main() -> Result<()> {
17+
let args = Cli::from_args();
18+
dbc_codegen::codegen(args.dbc_path, args.out_path, args.debug)?;
19+
Ok(())
20+
}

0 commit comments

Comments
 (0)