Skip to content

Commit ab4b352

Browse files
author
Pascal Hertleif
committed
Refactored library to take buffers, not Paths
Also made the binary return fancy exit codes and respect debug flag a bit more.
1 parent d05a01e commit ab4b352

File tree

6 files changed

+161
-145
lines changed

6 files changed

+161
-145
lines changed

Cargo.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dbc-codegen-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ authors = ["Pascal Hertleif <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8-
anyhow = "1.0.33"
98
heck = "0.3.1"
109
structopt = "0.3.20"
10+
exitcode = "1.1.2"
1111
dbc-codegen = { path = ".." }
1212

1313
[[bin]]

dbc-codegen-cli/src/main.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use anyhow::Result;
2-
use std::path::PathBuf;
1+
use std::fs::File;
2+
use std::{path::PathBuf, process::exit};
33
use structopt::StructOpt;
44

5-
/// Generate Rust structs from a `dbc` file.
5+
/// Generate Rust `struct`s from a `dbc` file.
66
#[derive(Debug, StructOpt)]
77
struct Cli {
88
/// Path to a `.dbc` file
@@ -14,8 +14,43 @@ struct Cli {
1414
debug: bool,
1515
}
1616

17-
fn main() -> Result<()> {
17+
fn main() {
1818
let args = Cli::from_args();
19-
dbc_codegen::codegen(args.dbc_path, args.out_path, args.debug)?;
20-
Ok(())
19+
let dbc_file = std::fs::read(&args.dbc_path).unwrap_or_else(|e| {
20+
eprintln!("could not read `{}`: {}", args.dbc_path.display(), e);
21+
exit(exitcode::NOINPUT);
22+
});
23+
let dbc_file_name = args
24+
.dbc_path
25+
.file_name()
26+
.unwrap_or_else(|| args.dbc_path.as_ref())
27+
.to_string_lossy();
28+
29+
if !args.out_path.is_dir() {
30+
eprintln!(
31+
"Output path needs to point to a directory (checked {})",
32+
args.out_path.display()
33+
);
34+
exit(exitcode::CANTCREAT);
35+
}
36+
37+
let messages_path = args.out_path.join("messages.rs");
38+
let mut messages_code = File::create(messages_path).unwrap_or_else(|e| {
39+
eprintln!(
40+
"Could not create `messages.rs` file in {}: {:?}",
41+
args.out_path.display(),
42+
e
43+
);
44+
exit(exitcode::CANTCREAT);
45+
});
46+
47+
dbc_codegen::codegen(&dbc_file_name, &dbc_file, &mut messages_code, args.debug).unwrap_or_else(
48+
|e| {
49+
eprintln!("could not convert `{}`: {}", args.dbc_path.display(), e);
50+
if args.debug {
51+
eprintln!("details: {:?}", e);
52+
}
53+
exit(exitcode::NOINPUT)
54+
},
55+
)
2156
}

src/lib.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
1-
use anyhow::{anyhow, ensure, Context, Result};
1+
use anyhow::{anyhow, Context, Result};
22
use can_dbc::{Message, Signal, ValDescription, ValueDescription, DBC};
33
use heck::{CamelCase, SnakeCase};
44
use pad::PadAdapter;
5-
use std::{fs::File, io::BufWriter, io::Write, path::PathBuf};
5+
use std::io::{BufWriter, Write};
66

77
mod includes;
88
mod keywords;
99
mod pad;
1010

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()))?;
14-
let dbc = can_dbc::DBC::from_slice(&dbc)
15-
.map_err(|e| anyhow!("Could not parse dbc file: {:#?}", e))?;
11+
pub fn codegen(dbc_name: &str, dbc_content: &[u8], out: impl Write, debug: bool) -> Result<()> {
12+
let dbc = can_dbc::DBC::from_slice(dbc_content).map_err(|e| {
13+
let msg = "Could not parse dbc file";
14+
if debug {
15+
anyhow!("{}: {:#?}", msg, e)
16+
} else {
17+
anyhow!("{}", msg)
18+
}
19+
})?;
1620
if debug {
1721
eprintln!("{:#?}", dbc);
1822
}
19-
20-
ensure!(
21-
out_path.is_dir(),
22-
"Output path needs to point to a directory"
23-
);
24-
25-
let messages_path = out_path.join("messages.rs");
26-
let messages_code =
27-
File::create(messages_path).context("Could not create `messages.rs` file")?;
28-
let mut w = BufWriter::new(messages_code);
23+
let mut w = BufWriter::new(out);
2924

3025
writeln!(&mut w, "// Generated code!")?;
3126
writeln!(&mut w, "#![no_std]")?;
@@ -34,12 +29,7 @@ pub fn codegen(dbc_path: PathBuf, out_path: PathBuf, debug: bool) -> Result<()>
3429
"#![allow(unused, clippy::let_and_return, clippy::eq_op)]"
3530
)?;
3631
writeln!(&mut w)?;
37-
let dbc_file_name = dbc_path.file_name().unwrap_or_else(|| dbc_path.as_ref());
38-
writeln!(
39-
&mut w,
40-
"//! Message definitions from file `{:?}`",
41-
dbc_file_name
42-
)?;
32+
writeln!(&mut w, "//! Message definitions from file `{:?}`", dbc_name)?;
4333
writeln!(&mut w, "//!")?;
4434
writeln!(&mut w, "//! - Version: `{:?}`", dbc.version())?;
4535
writeln!(&mut w)?;

testing/can-messages/build.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
use std::path::PathBuf;
2-
31
use anyhow::Result;
2+
use std::{
3+
fs::{self, File},
4+
io::BufWriter,
5+
};
46

57
fn main() -> Result<()> {
6-
let dbc_file: PathBuf = PathBuf::from("../dbc-examples/example.dbc");
7-
let out: PathBuf = PathBuf::from("src/");
8+
let dbc_file = fs::read("../dbc-examples/example.dbc")?;
9+
let mut out = BufWriter::new(File::create("src/messages.rs")?);
810
println!("cargo:rerun-if-changed=../dbc-examples/example.dbc");
9-
dbc_codegen::codegen(dbc_file, out, true)?;
11+
12+
dbc_codegen::codegen("example.dbc", &dbc_file, &mut out, true)?;
1013
Ok(())
1114
}

0 commit comments

Comments
 (0)