diff --git a/Cargo.toml b/Cargo.toml index bba63cf..5b55062 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,9 @@ rust-version = "1.74" [features] default = ["with-serde"] -with-serde = ["dep:serde"] +serde = ["dep:serde"] +# This feature is deprecated per clippy::redundant_feature_names. Use `serde` instead. +with-serde = ["serde"] [dependencies] derive-getters = "0.5" @@ -31,20 +33,14 @@ unused_qualifications = "warn" [lints.clippy] cargo = { level = "warn", priority = -1 } pedantic = { level = "warn", priority = -1 } +# Verified allows +ref_option = "allow" # required by derive-getters +redundant_feature_names = "allow" # remove once with-serde feature is removed # TODO: fix these if it makes sense -doc_markdown = "allow" # 36 +#doc_markdown = "allow" # 36 enum_variant_names = "allow" # 20 must_use_candidate = "allow" # 12 struct_field_names = "allow" # 10 missing_errors_doc = "allow" # 8 similar_names = "allow" # 4 -redundant_feature_names = "allow" # 4 needless_pass_by_value = "allow" # 4 -cargo_common_metadata = "allow" # 4 -upper_case_acronyms = "allow" # 2 -trivially_copy_pass_by_ref = "allow" # 2 -ref_option = "allow" # 2 -match_wildcard_for_single_variants = "allow" # 2 -cast_possible_truncation = "allow" # 2 -match_same_arms = "allow" # 1 -items_after_test_module = "allow" # 1 diff --git a/README.md b/README.md index 0ec92ba..99f1da2 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ A CAN-dbc format parser written with Rust's [nom](https://github.com/Geal/nom) p Read dbc file and generate Rust structs based on the messages/signals defined in the dbc. ```rust,no_run -use can_dbc::DBC; +use can_dbc::Dbc; use codegen::Scope; use std::fs::File; @@ -27,7 +27,7 @@ fn main() -> io::Result<()> { let mut buffer = Vec::new(); f.read_to_end(&mut buffer)?; - let dbc = can_dbc::DBC::from_slice(&buffer).expect("Failed to parse dbc file"); + let dbc = can_dbc::Dbc::from_slice(&buffer).expect("Failed to parse dbc file"); let mut scope = Scope::new(); for message in dbc.messages() { @@ -66,28 +66,28 @@ can-dbc = "3.0" # Implemented DBC parts -- [x] version -- [x] new_symbols -- [x] bit_timing *(deprecated but mandatory)* -- [x] nodes -- [x] value_tables -- [x] messages -- [x] message_transmitters -- [x] environment_variables -- [x] environment_variables_data -- [x] signal_types -- [x] comments -- [x] attribute_definitions -- [ ] sigtype_attr_list *(format missing documentation)* -- [x] attribute_defaults -- [x] attribute_values -- [x] value_descriptions -- [ ] category_definitions *(deprecated)* -- [ ] categories *(deprecated)* -- [ ] filter *(deprecated)* -- [x] signal_type_refs -- [x] signal_groups -- [x] signal_extended_value_type_list +- [x] `version` +- [x] `new_symbols` +- [x] `bit_timing` *(deprecated but mandatory)* +- [x] `nodes` +- [x] `value_tables` +- [x] `messages` +- [x] `message_transmitters` +- [x] `environment_variables` +- [x] `environment_variables_data` +- [x] `signal_types` +- [x] `comments` +- [x] `attribute_definitions` +- [ ] `sigtype_attr_list` *(format missing documentation)* +- [x] `attribute_defaults` +- [x] `attribute_values` +- [x] `value_descriptions` +- [ ] `category_definitions` *(deprecated)* +- [ ] `categories` *(deprecated)* +- [ ] `filter` *(deprecated)* +- [x] `signal_type_refs` +- [x] `signal_groups` +- [x] `signal_extended_value_type_list` # Deviating from standard - multispace between parsers instead of single space allowing e.g. (two spaces) `SIG_GROUP 13`. diff --git a/examples/file_parser.rs b/examples/file_parser.rs index 9b630d3..aedfbac 100644 --- a/examples/file_parser.rs +++ b/examples/file_parser.rs @@ -27,7 +27,7 @@ fn main() -> io::Result<()> { f.read_to_end(&mut buffer)?; let dbc_in = std::str::from_utf8(&buffer).unwrap(); - match can_dbc::DBC::try_from(dbc_in) { + match can_dbc::Dbc::try_from(dbc_in) { Ok(dbc_content) => println!("DBC Content{dbc_content:#?}"), Err(e) => { match e { diff --git a/justfile b/justfile index e34227a..e97d48d 100755 --- a/justfile +++ b/justfile @@ -14,7 +14,7 @@ ci_mode := if env('CI', '') != '' {'1'} else {''} binstall_args := if env('CI', '') != '' {'--no-confirm --no-track --disable-telemetry'} else {''} export RUSTFLAGS := env('RUSTFLAGS', if ci_mode == '1' {'-D warnings'} else {''}) export RUSTDOCFLAGS := env('RUSTDOCFLAGS', if ci_mode == '1' {'-D warnings'} else {''}) -export RUST_BACKTRACE := env('RUST_BACKTRACE', if ci_mode == '1' {'1'} else {''}) +export RUST_BACKTRACE := env('RUST_BACKTRACE', if ci_mode == '1' {'1'} else {'0'}) @_default: {{just_executable()}} --list diff --git a/src/lib.rs b/src/lib.rs index 208bb8d..ca917fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -87,7 +87,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn dbc_definition_test() { - match DBC::try_from(SAMPLE_DBC) { + match Dbc::try_from(SAMPLE_DBC) { Ok(dbc_content) => println!("DBC Content{dbc_content:#?}"), Err(e) => { match e { @@ -111,7 +111,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn lookup_signal_comment() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let comment = dbc_content .signal_comment(MessageId::Standard(1840), "Signal_4") .expect("Signal comment missing"); @@ -123,14 +123,14 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn lookup_signal_comment_none_when_missing() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let comment = dbc_content.signal_comment(MessageId::Standard(1840), "Signal_2"); assert_eq!(None, comment); } #[test] fn lookup_message_comment() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let comment = dbc_content .message_comment(MessageId::Standard(1840)) .expect("Message comment missing"); @@ -139,14 +139,14 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn lookup_message_comment_none_when_missing() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let comment = dbc_content.message_comment(MessageId::Standard(2000)); assert_eq!(None, comment); } #[test] fn lookup_value_descriptions_for_signal() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let val_descriptions = dbc_content .value_descriptions_for_signal(MessageId::Standard(2000), "Signal_3") .expect("Message comment missing"); @@ -160,7 +160,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn lookup_value_descriptions_for_signal_none_when_missing() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let val_descriptions = dbc_content.value_descriptions_for_signal(MessageId::Standard(2000), "Signal_2"); assert_eq!(None, val_descriptions); @@ -168,7 +168,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn lookup_extended_value_type_for_signal() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let extended_value_type = dbc_content.extended_value_type_for_signal(MessageId::Standard(2000), "Signal_8"); assert_eq!( @@ -179,7 +179,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn lookup_extended_value_type_for_signal_none_when_missing() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let extended_value_type = dbc_content.extended_value_type_for_signal(MessageId::Standard(2000), "Signal_1"); assert_eq!(extended_value_type, None); @@ -187,21 +187,21 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn lookup_signal_by_name() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let signal = dbc_content.signal_by_name(MessageId::Standard(2000), "Signal_8"); assert!(signal.is_some()); } #[test] fn lookup_signal_by_name_none_when_missing() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let signal = dbc_content.signal_by_name(MessageId::Standard(2000), "Signal_25"); assert_eq!(signal, None); } #[test] fn lookup_multiplex_indicator_switch() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let multiplexor_switch = dbc_content.message_multiplexor_switch(MessageId::Standard(3040)); assert!(multiplexor_switch.is_ok()); assert!(multiplexor_switch.as_ref().unwrap().is_some()); @@ -210,7 +210,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn lookup_multiplex_indicator_switch_none_when_missing() { - let dbc_content = DBC::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); + let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC"); let multiplexor_switch = dbc_content.message_multiplexor_switch(MessageId::Standard(1840)); assert!(multiplexor_switch.unwrap().is_none()); } @@ -236,7 +236,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; pub enum Error<'a> { /// Remaining String, the DBC was only read partially. /// Occurs when e.g. an unexpected symbol occurs. - Incomplete(DBC, &'a str), + Incomplete(Dbc, &'a str), /// Parser failed Nom(nom::Err>), /// Can't Lookup multiplexors because the message uses extended multiplexing. @@ -564,7 +564,7 @@ pub struct SignalExtendedValueTypeList { } #[derive(Clone, Debug, PartialEq, Getters, Serialize, Deserialize)] -pub struct DBC { +pub struct Dbc { /// Version generated by DB editor version: Version, new_symbols: Vec, @@ -603,18 +603,18 @@ pub struct DBC { extended_multiplex: Vec, } -impl DBC { +impl Dbc { /// Read a DBC from a buffer #[allow(clippy::result_large_err)] - pub fn from_slice(buffer: &[u8]) -> Result> { + pub fn from_slice(buffer: &[u8]) -> Result> { let dbc_in = std::str::from_utf8(buffer).map_err(Error::InvalidContent)?; Self::try_from(dbc_in) } #[allow(clippy::should_implement_trait)] - #[deprecated(since = "4.0.0", note = "please use `DBC::try_from` instead")] + #[deprecated(since = "4.0.0", note = "please use `Dbc::try_from` instead")] #[allow(clippy::result_large_err)] - pub fn from_str(dbc_in: &str) -> Result> { + pub fn from_str(dbc_in: &str) -> Result> { let (remaining, dbc) = parser::dbc(dbc_in).map_err(Error::Nom)?; if !remaining.is_empty() { return Err(Error::Incomplete(dbc, remaining)); @@ -745,7 +745,7 @@ impl DBC { } } -impl<'a> TryFrom<&'a str> for DBC { +impl<'a> TryFrom<&'a str> for Dbc { type Error = Error<'a>; fn try_from(dbc_in: &'a str) -> Result { diff --git a/src/parser.rs b/src/parser.rs index b5704fd..f3ef6d1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -16,11 +16,12 @@ use nom::{AsChar, IResult, Input, Parser}; use crate::{ AccessNode, AccessType, AttributeDefault, AttributeDefinition, AttributeValue, - AttributeValueForObject, AttributeValuedForObjectType, Baudrate, ByteOrder, Comment, EnvType, - EnvironmentVariable, EnvironmentVariableData, ExtendedMultiplex, ExtendedMultiplexMapping, - Message, MessageId, MessageTransmitter, MultiplexIndicator, Node, Signal, - SignalExtendedValueType, SignalExtendedValueTypeList, SignalGroups, SignalType, SignalTypeRef, - Symbol, Transmitter, ValDescription, ValueDescription, ValueTable, ValueType, Version, DBC, + AttributeValueForObject, AttributeValuedForObjectType, Baudrate, ByteOrder, Comment, Dbc, + EnvType, EnvironmentVariable, EnvironmentVariableData, ExtendedMultiplex, + ExtendedMultiplexMapping, Message, MessageId, MessageTransmitter, MultiplexIndicator, Node, + Signal, SignalExtendedValueType, SignalExtendedValueTypeList, SignalGroups, SignalType, + SignalTypeRef, Symbol, Transmitter, ValDescription, ValueDescription, ValueTable, ValueType, + Version, }; fn is_semi_colon(chr: char) -> bool { @@ -116,8 +117,8 @@ fn brk_close(s: &str) -> IResult<&str, char> { char(']').parse(s) } -/// A valid C_identifier. C_identifiers start with a alphacharacter or an underscore -/// and may further consist of alpha­numeric, characters and underscore +/// A valid `C_identifier`. `C_identifier`s start with an alpha character or an underscore +/// and may further consist of alphanumeric characters and underscore fn c_ident(s: &str) -> IResult<&str, String> { let (s, head) = take_while1(is_c_ident_head).parse(s)?; let (s, remaining) = take_while(is_c_string_char).parse(s)?; @@ -1026,7 +1027,7 @@ fn signal_groups(s: &str) -> IResult<&str, SignalGroups> { )) } -pub fn dbc(s: &str) -> IResult<&str, DBC> { +pub fn dbc(s: &str) -> IResult<&str, Dbc> { let ( s, ( @@ -1075,7 +1076,7 @@ pub fn dbc(s: &str) -> IResult<&str, DBC> { let (s, _) = multispace0(s)?; Ok(( s, - DBC { + Dbc { version, new_symbols, bit_timing, @@ -1113,7 +1114,7 @@ mod tests { let (_, cid2) = c_ident(def2).unwrap(); assert_eq!("_EALL_DUSasb18", cid2); - // identifiers must not start with digit1s + // identifiers must not start with digits let def3 = "3EALL_DUSasb18 "; let cid3_result = c_ident(def3); assert!(cid3_result.is_err()); diff --git a/tests/snapshots-dbc-cantools/!error___BU_BO_REL_.snap b/tests/snapshots-dbc-cantools/!error___BU_BO_REL_.snap index dcc28d2..4086d76 100644 --- a/tests/snapshots-dbc-cantools/!error___BU_BO_REL_.snap +++ b/tests/snapshots-dbc-cantools/!error___BU_BO_REL_.snap @@ -2,7 +2,7 @@ source: tests/test-snapshots.rs --- Incomplete( - DBC { + Dbc { version: Version( "", ), diff --git a/tests/snapshots-dbc-cantools/!error___BU_BO_REL_Message.snap b/tests/snapshots-dbc-cantools/!error___BU_BO_REL_Message.snap index b4a6d6f..8de3d08 100644 --- a/tests/snapshots-dbc-cantools/!error___BU_BO_REL_Message.snap +++ b/tests/snapshots-dbc-cantools/!error___BU_BO_REL_Message.snap @@ -2,7 +2,7 @@ source: tests/test-snapshots.rs --- Incomplete( - DBC { + Dbc { version: Version( "", ), diff --git a/tests/snapshots-dbc-cantools/!error___attributes_relation.snap b/tests/snapshots-dbc-cantools/!error___attributes_relation.snap index debe2fe..134bd9d 100644 --- a/tests/snapshots-dbc-cantools/!error___attributes_relation.snap +++ b/tests/snapshots-dbc-cantools/!error___attributes_relation.snap @@ -2,7 +2,7 @@ source: tests/test-snapshots.rs --- Incomplete( - DBC { + Dbc { version: Version( "", ), diff --git a/tests/snapshots-dbc-cantools/!error___emc32.snap b/tests/snapshots-dbc-cantools/!error___emc32.snap index 3405294..0ad5c34 100644 --- a/tests/snapshots-dbc-cantools/!error___emc32.snap +++ b/tests/snapshots-dbc-cantools/!error___emc32.snap @@ -2,7 +2,7 @@ source: tests/test-snapshots.rs --- Incomplete( - DBC { + Dbc { version: Version( "HINBNYYYYYYYYYYYYYYYYYYYYYYYYYYYNNNNNNNNNN/4/%%%/4/'%**4NNN///", ), diff --git a/tests/snapshots-dbc-cantools/!error___issue_199.snap b/tests/snapshots-dbc-cantools/!error___issue_199.snap index bc50325..2329f1e 100644 --- a/tests/snapshots-dbc-cantools/!error___issue_199.snap +++ b/tests/snapshots-dbc-cantools/!error___issue_199.snap @@ -2,7 +2,7 @@ source: tests/test-snapshots.rs --- Incomplete( - DBC { + Dbc { version: Version( "", ), diff --git a/tests/snapshots-dbc-cantools/!error___issue_199_extended.snap b/tests/snapshots-dbc-cantools/!error___issue_199_extended.snap index ef02515..05842ac 100644 --- a/tests/snapshots-dbc-cantools/!error___issue_199_extended.snap +++ b/tests/snapshots-dbc-cantools/!error___issue_199_extended.snap @@ -2,7 +2,7 @@ source: tests/test-snapshots.rs --- Incomplete( - DBC { + Dbc { version: Version( "", ), diff --git a/tests/snapshots-dbc-cantools/!error___issue_228.snap b/tests/snapshots-dbc-cantools/!error___issue_228.snap index 8185888..0a913b2 100644 --- a/tests/snapshots-dbc-cantools/!error___issue_228.snap +++ b/tests/snapshots-dbc-cantools/!error___issue_228.snap @@ -2,7 +2,7 @@ source: tests/test-snapshots.rs --- Incomplete( - DBC { + Dbc { version: Version( "", ), diff --git a/tests/snapshots-dbc-cantools/!error___open_actuator.snap b/tests/snapshots-dbc-cantools/!error___open_actuator.snap index 7395e33..8d237a0 100644 --- a/tests/snapshots-dbc-cantools/!error___open_actuator.snap +++ b/tests/snapshots-dbc-cantools/!error___open_actuator.snap @@ -2,7 +2,7 @@ source: tests/test-snapshots.rs --- Incomplete( - DBC { + Dbc { version: Version( "", ), diff --git a/tests/snapshots-dbc-cantools/!error___socialledge.snap b/tests/snapshots-dbc-cantools/!error___socialledge.snap index fd0c438..62d52b4 100644 --- a/tests/snapshots-dbc-cantools/!error___socialledge.snap +++ b/tests/snapshots-dbc-cantools/!error___socialledge.snap @@ -2,7 +2,7 @@ source: tests/test-snapshots.rs --- Incomplete( - DBC { + Dbc { version: Version( "", ), diff --git a/tests/test-snapshots.rs b/tests/test-snapshots.rs index 742d637..691e058 100644 --- a/tests/test-snapshots.rs +++ b/tests/test-snapshots.rs @@ -59,7 +59,7 @@ fn parse_one_file(path: &Path) { eprintln!("Testing DBC file: {}", path.display()); let file_name = path.file_stem().unwrap().to_string_lossy().to_string(); let buffer = fs::read(path).unwrap(); - match can_dbc::DBC::from_slice(&buffer) { + match can_dbc::Dbc::from_slice(&buffer) { Ok(dbc) => assert_yaml_snapshot!(file_name, dbc), Err(e) => { eprintln!("Failed to parse {file_name}.dbc: {e:?}");