-
Notifications
You must be signed in to change notification settings - Fork 45
chore: allow generated file to be used with include!
#108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c8b470b
138db18
2a186df
8e76dcb
c756c3d
cbc6146
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| **/*.rs.bk | ||
| *.pdb | ||
| .envrc | ||
| .idea/ | ||
| .vscode/ | ||
| debug/ | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,16 @@ mod includes; | |
| mod keywords; | ||
| mod pad; | ||
|
|
||
| static ALLOW_DEADCODE: &str = "#[allow(dead_code)]"; | ||
| static ALLOW_LINTS: &str = r"#[allow( | ||
| clippy::absurd_extreme_comparisons, | ||
| clippy::excessive_precision, | ||
| clippy::manual_range_contains, | ||
| clippy::unnecessary_cast, | ||
| clippy::useless_conversion, | ||
| unused_comparisons, | ||
| )]"; | ||
|
|
||
| /// Code generator configuration. See module-level docs for an example. | ||
| #[derive(TypedBuilder)] | ||
| #[non_exhaustive] | ||
|
|
@@ -122,31 +132,13 @@ pub fn codegen(config: Config<'_>, out: impl Write) -> Result<()> { | |
| let mut w = BufWriter::new(out); | ||
|
|
||
| writeln!(&mut w, "// Generated code!")?; | ||
| writeln!(&mut w, "//")?; | ||
| writeln!( | ||
| &mut w, | ||
| "#![allow(unused_comparisons, unreachable_patterns, unused_imports)]" | ||
| )?; | ||
| if config.allow_dead_code { | ||
| writeln!(&mut w, "#![allow(dead_code)]")?; | ||
| } | ||
| writeln!(&mut w, "#![allow(clippy::let_and_return, clippy::eq_op)]")?; | ||
| writeln!( | ||
| &mut w, | ||
| "#![allow(clippy::useless_conversion, clippy::unnecessary_cast)]" | ||
| )?; | ||
| writeln!( | ||
| &mut w, | ||
| "#![allow(clippy::excessive_precision, clippy::manual_range_contains, clippy::absurd_extreme_comparisons, clippy::too_many_arguments)]" | ||
| )?; | ||
| writeln!(&mut w, "#![deny(clippy::arithmetic_side_effects)]")?; | ||
| writeln!(&mut w)?; | ||
| writeln!( | ||
| &mut w, | ||
| "//! Message definitions from file `{:?}`", | ||
| "// Message definitions from file `{}`", | ||
| config.dbc_name | ||
| )?; | ||
| writeln!(&mut w, "//!")?; | ||
| writeln!(&mut w, "//! - Version: `{:?}`", dbc.version)?; | ||
| writeln!(&mut w, "// Version: {}", dbc.version.0)?; | ||
nyurik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| writeln!(&mut w)?; | ||
| writeln!(&mut w, "use core::ops::BitOr;")?; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| writeln!(&mut w, "use bitvec::prelude::*;")?; | ||
|
|
@@ -190,6 +182,10 @@ fn render_dbc(mut w: impl Write, config: &Config<'_>, dbc: &Dbc) -> Result<()> { | |
|
|
||
| fn render_root_enum(mut w: impl Write, dbc: &Dbc, config: &Config<'_>) -> Result<()> { | ||
| writeln!(w, "/// All messages")?; | ||
| writeln!(w, "{ALLOW_LINTS}")?; | ||
| if config.allow_dead_code { | ||
| writeln!(&mut w, "{ALLOW_DEADCODE}")?; | ||
| } | ||
| writeln!(w, "#[derive(Clone)]")?; | ||
| config.impl_debug.fmt_attr(&mut w, "derive(Debug)")?; | ||
| config | ||
|
|
@@ -208,6 +204,10 @@ fn render_root_enum(mut w: impl Write, dbc: &Dbc, config: &Config<'_>) -> Result | |
| writeln!(&mut w, "}}")?; | ||
| writeln!(&mut w)?; | ||
|
|
||
| writeln!(w, "{ALLOW_LINTS}")?; | ||
| if config.allow_dead_code { | ||
| writeln!(&mut w, "{ALLOW_DEADCODE}")?; | ||
| } | ||
| writeln!(w, "impl Messages {{")?; | ||
| { | ||
| let mut w = PadAdapter::wrap(&mut w); | ||
|
|
@@ -275,6 +275,10 @@ fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &D | |
| writeln!(w, "}}")?; | ||
| writeln!(w)?; | ||
|
|
||
| writeln!(w, "{ALLOW_LINTS}")?; | ||
| if config.allow_dead_code { | ||
| writeln!(&mut w, "{ALLOW_DEADCODE}")?; | ||
| } | ||
| writeln!(w, "impl {} {{", type_name(&msg.name))?; | ||
| { | ||
| let mut w = PadAdapter::wrap(&mut w); | ||
|
|
@@ -954,6 +958,10 @@ fn write_enum( | |
| let signal_rust_type = signal_to_rust_type(signal); | ||
|
|
||
| writeln!(w, "/// Defined values for {}", signal.name)?; | ||
| writeln!(w, "{ALLOW_LINTS}")?; | ||
| if config.allow_dead_code { | ||
| writeln!(&mut w, "{ALLOW_DEADCODE}")?; | ||
| } | ||
| writeln!(w, "#[derive(Clone, Copy, PartialEq)]")?; | ||
| config.impl_debug.fmt_attr(&mut w, "derive(Debug)")?; | ||
| config | ||
|
|
@@ -1421,6 +1429,10 @@ fn render_multiplexor_enums( | |
| } | ||
|
|
||
| writeln!(w, "/// Defined values for multiplexed signal {}", msg.name)?; | ||
| writeln!(w, "{ALLOW_LINTS}")?; | ||
| if config.allow_dead_code { | ||
| writeln!(&mut w, "{ALLOW_DEADCODE}")?; | ||
| } | ||
|
|
||
| config.impl_debug.fmt_attr(&mut w, "derive(Debug)")?; | ||
| config | ||
|
|
@@ -1452,6 +1464,10 @@ fn render_multiplexor_enums( | |
| for (switch_index, multiplexed_signals) in &multiplexed_signals { | ||
| let struct_name = multiplexed_enum_variant_name(msg, multiplexor_signal, *switch_index)?; | ||
|
|
||
| writeln!(w, "{ALLOW_LINTS}")?; | ||
| if config.allow_dead_code { | ||
| writeln!(&mut w, "{ALLOW_DEADCODE}")?; | ||
| } | ||
| config.impl_debug.fmt_attr(&mut w, "derive(Debug)")?; | ||
| config | ||
| .impl_defmt | ||
|
|
@@ -1462,6 +1478,10 @@ fn render_multiplexor_enums( | |
| writeln!(w, "pub struct {struct_name} {{ raw: [u8; {}] }}", msg.size)?; | ||
| writeln!(w)?; | ||
|
|
||
| writeln!(w, "{ALLOW_LINTS}")?; | ||
| if config.allow_dead_code { | ||
| writeln!(&mut w, "{ALLOW_DEADCODE}")?; | ||
| } | ||
| writeln!(w, "impl {struct_name} {{")?; | ||
|
|
||
| writeln!( | ||
|
|
@@ -1488,6 +1508,10 @@ fn render_arbitrary(mut w: impl Write, config: &Config<'_>, msg: &Message) -> Re | |
| FeatureConfig::Never => return Ok(()), | ||
| } | ||
|
|
||
| writeln!(w, "{ALLOW_LINTS}")?; | ||
| if config.allow_dead_code { | ||
| writeln!(&mut w, "{ALLOW_DEADCODE}")?; | ||
| } | ||
| writeln!( | ||
| w, | ||
| "impl<'a> Arbitrary<'a> for {typ} {{", | ||
|
|
@@ -1543,7 +1567,7 @@ fn render_error(mut w: impl Write, config: &Config<'_>) -> io::Result<()> { | |
| w.write_all(include_bytes!("./includes/errors.rs"))?; | ||
|
|
||
| config.impl_error.fmt_cfg(w, |w| { | ||
| writeln!(w, "impl std::error::Error for CanError {{}}") | ||
| writeln!(w, "impl core::error::Error for CanError {{}}") | ||
| }) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| use std::env::var; | ||
| use std::fs::{read_to_string, write}; | ||
| use std::path::PathBuf; | ||
|
|
||
| use anyhow::Result; | ||
| use dbc_codegen::{Config, FeatureConfig}; | ||
|
|
||
| fn main() -> Result<()> { | ||
| let dbc_file = read_to_string("../dbc-examples/example.dbc")?; | ||
| println!("cargo:rerun-if-changed=../dbc-examples/example.dbc"); | ||
| println!("cargo:rerun-if-changed=../../src"); | ||
| println!("cargo:rerun-if-changed=../can-embedded/src"); | ||
|
|
||
| let config = Config::builder() | ||
| .dbc_name("example.dbc") | ||
| .dbc_content(&dbc_file) | ||
| .debug_prints(true) | ||
| .allow_dead_code(true) | ||
| .impl_debug(FeatureConfig::Always) | ||
| .impl_defmt(FeatureConfig::Always) | ||
| .impl_error(FeatureConfig::Gated("std")) | ||
| .impl_arbitrary(FeatureConfig::Gated("arb")) | ||
| .check_ranges(FeatureConfig::Always) | ||
| .build(); | ||
|
|
||
| let mut out = Vec::new(); | ||
| dbc_codegen::codegen(config, &mut out)?; | ||
| write( | ||
| PathBuf::from(var("OUT_DIR")?).join("messages.rs"), | ||
| String::from_utf8(out)?, | ||
| )?; | ||
|
|
||
| Ok(()) | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||||
| #![no_std] | ||||||||
|
|
||||||||
| #[rustfmt::skip] | ||||||||
| #[allow(clippy::pedantic)] | ||||||||
| #[allow(clippy::disallowed_names)] | ||||||||
| pub mod messages; | ||||||||
| #[expect(clippy::disallowed_names)] | ||||||||
| mod messages { | ||||||||
| include!(concat!(env!("OUT_DIR"), "/messages.rs")); | ||||||||
| } | ||||||||
|
||||||||
| } | |
| } | |
| pub use messages::*; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add imports to have working example in README?
dbc_fileshould be as astringnow: