diff --git a/src/lib.rs b/src/lib.rs index 0e9aeb3..6a09e9f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -159,7 +159,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; fn lookup_signal_comment() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); let comment = dbc_content - .signal_comment(&MessageId(1840), "Signal_4") + .signal_comment(MessageId(1840), "Signal_4") .expect("Signal comment missing"); assert_eq!( "asaklfjlsdfjlsdfgls\nHH?=(%)/&KKDKFSDKFKDFKSDFKSDFNKCnvsdcvsvxkcv", @@ -170,7 +170,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn lookup_signal_comment_none_when_missing() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); - let comment = dbc_content.signal_comment(&MessageId(1840), "Signal_2"); + let comment = dbc_content.signal_comment(MessageId(1840), "Signal_2"); assert_eq!(None, comment); } @@ -178,7 +178,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; fn lookup_message_comment() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); let comment = dbc_content - .message_comment(&MessageId(1840)) + .message_comment(MessageId(1840)) .expect("Message comment missing"); assert_eq!("Some Message comment", comment); } @@ -186,7 +186,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn lookup_message_comment_none_when_missing() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); - let comment = dbc_content.message_comment(&MessageId(2000)); + let comment = dbc_content.message_comment(MessageId(2000)); assert_eq!(None, comment); } @@ -194,7 +194,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; fn lookup_value_descriptions_for_signal() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); let val_descriptions = dbc_content - .value_descriptions_for_signal(&MessageId(2000), "Signal_3") + .value_descriptions_for_signal(MessageId(2000), "Signal_3") .expect("Message comment missing"); let exp = vec![ValDescription { @@ -208,7 +208,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; fn lookup_value_descriptions_for_signal_none_when_missing() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); let val_descriptions = - dbc_content.value_descriptions_for_signal(&MessageId(2000), "Signal_2"); + dbc_content.value_descriptions_for_signal(MessageId(2000), "Signal_2"); assert_eq!(None, val_descriptions); } @@ -216,7 +216,7 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; fn lookup_extended_value_type_for_signal() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); let extended_value_type = - dbc_content.extended_value_type_for_signal(&MessageId(2000), "Signal_8"); + dbc_content.extended_value_type_for_signal(MessageId(2000), "Signal_8"); assert_eq!( extended_value_type, Some(&SignalExtendedValueType::IEEEfloat32Bit) @@ -227,31 +227,28 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; fn lookup_extended_value_type_for_signal_none_when_missing() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); let extended_value_type = - dbc_content.extended_value_type_for_signal(&MessageId(2000), "Signal_1"); + dbc_content.extended_value_type_for_signal(MessageId(2000), "Signal_1"); assert_eq!(extended_value_type, None); } #[test] fn lookup_signal_by_name() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); - let signal = - dbc_content.signal_by_name(MessageId(2000), "Signal_8"); + let signal = dbc_content.signal_by_name(MessageId(2000), "Signal_8"); assert!(signal.is_some()); } #[test] fn lookup_signal_by_name_none_when_missing() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); - let signal = - dbc_content.signal_by_name(MessageId(2000), "Signal_25"); + let signal = dbc_content.signal_by_name(MessageId(2000), "Signal_25"); assert_eq!(signal, None); } #[test] fn lookup_multiplex_indicator_switch() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); - let multiplexor_switch = - dbc_content.message_multiplexor_switch(MessageId(3040)); + let multiplexor_switch = dbc_content.message_multiplexor_switch(MessageId(3040)); assert!(multiplexor_switch.is_some()); assert_eq!(multiplexor_switch.unwrap().name(), "Switch"); } @@ -259,11 +256,9 @@ SIG_VALTYPE_ 2000 Signal_8 : 1; #[test] fn lookup_multiplex_indicator_switch_none_when_missing() { let dbc_content = DBC::from_slice(SAMPLE_DBC).expect("Failed to parse DBC"); - let multiplexor_switch = - dbc_content.message_multiplexor_switch(MessageId(1840)); + let multiplexor_switch = dbc_content.message_multiplexor_switch(MessageId(1840)); assert!(multiplexor_switch.is_none()); } - } /// Possible error cases for `can-dbc` @@ -651,19 +646,22 @@ impl DBC { } pub fn signal_by_name(&self, message_id: MessageId, signal_name: &str) -> Option<&Signal> { - let message = self.messages + let message = self + .messages .iter() .find(|message| message.message_id == message_id); if let Some(message) = message { - return message.signals.iter() - .find(|signal| signal.name == *signal_name); + return message + .signals + .iter() + .find(|signal| signal.name == *signal_name); } None } /// Lookup a message comment - pub fn message_comment(&self, message_id: &MessageId) -> Option<&str> { + pub fn message_comment(&self, message_id: MessageId) -> Option<&str> { self.comments .iter() .filter_map(|x| match x { @@ -671,7 +669,7 @@ impl DBC { message_id: ref x_message_id, ref comment, } => { - if x_message_id == message_id { + if *x_message_id == message_id { Some(comment.as_str()) } else { None @@ -683,7 +681,7 @@ impl DBC { } /// Lookup a signal comment - pub fn signal_comment(&self, message_id: &MessageId, signal_name: &str) -> Option<&str> { + pub fn signal_comment(&self, message_id: MessageId, signal_name: &str) -> Option<&str> { self.comments .iter() .filter_map(|x| match x { @@ -692,7 +690,7 @@ impl DBC { signal_name: ref x_signal_name, comment, } => { - if x_message_id == message_id && x_signal_name == signal_name { + if *x_message_id == message_id && x_signal_name == signal_name { Some(comment.as_str()) } else { None @@ -706,7 +704,7 @@ impl DBC { /// Lookup value descriptions for signal pub fn value_descriptions_for_signal( &self, - message_id: &MessageId, + message_id: MessageId, signal_name: &str, ) -> Option<&[ValDescription]> { self.value_descriptions @@ -717,7 +715,7 @@ impl DBC { signal_name: ref x_signal_name, ref value_descriptions, } => { - if x_message_id == message_id && x_signal_name == signal_name { + if *x_message_id == message_id && x_signal_name == signal_name { Some(value_descriptions.as_slice()) } else { None @@ -731,7 +729,7 @@ impl DBC { /// Lookup the extended value for a given signal pub fn extended_value_type_for_signal( &self, - message_id: &MessageId, + message_id: MessageId, signal_name: &str, ) -> Option<&SignalExtendedValueType> { self.signal_extended_value_type_list @@ -742,7 +740,7 @@ impl DBC { signal_name: ref x_signal_name, ref signal_extended_value_type, } => { - if x_message_id == message_id && x_signal_name == signal_name { + if *x_message_id == message_id && x_signal_name == signal_name { Some(signal_extended_value_type) } else { None @@ -753,17 +751,17 @@ impl DBC { } /// Lookup the message multiplexor switch signal for a given message - pub fn message_multiplexor_switch( - &self, - message_id: MessageId - ) -> Option<&Signal> { - let message = self.messages + pub fn message_multiplexor_switch(&self, message_id: MessageId) -> Option<&Signal> { + let message = self + .messages .iter() .find(|message| message.message_id == message_id); if let Some(message) = message { - return message.signals.iter() - .find(|signal| signal.multiplexer_indicator == MultiplexIndicator::Multiplexor); + return message + .signals + .iter() + .find(|signal| signal.multiplexer_indicator == MultiplexIndicator::Multiplexor); } None } diff --git a/src/parser.rs b/src/parser.rs index 8d52d17..4052c0d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -490,42 +490,42 @@ fn is_quote(chr: char) -> bool { chr == '"' } -/// Multi space +// Multi space named!(multispace1>, many1!(char!(' '))); -/// Abreviation for multispace1 +// Abreviation for multispace1 named!(ms>, many1!(char!(' '))); -/// Colon +// Colon named!(colon, char!(':')); -/// Comma aka ',' +// Comma aka ',' named!(comma, char!(',')); -/// Comma aka ';' +// Comma aka ';' named!(semi_colon, char!(';')); -/// Quote aka '"' +// Quote aka '"' named!(quote, char!('"')); named!(pipe, char!('|')); named!(at, char!('@')); -/// brace open aka '(' +// brace open aka '(' named!(brc_open, char!('(')); -/// brace close aka '(' +// brace close aka '(' named!(brc_close, char!(')')); -/// bracket open aka '[' +// bracket open aka '[' named!(brk_open, char!('[')); -/// bracket close aka ']' +// bracket close aka ']' named!(brk_close, char!(']')); -/// 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_identifiers start with a alphacharacter or an underscore +// and may further consist of alpha­numeric, characters and underscore named!(c_ident, map_res!( recognize!( @@ -560,7 +560,7 @@ named!(i64_digit, named!(char_string, do_parse!( quote >> - s: take_till_s!(|c| is_quote(c as char)) >> + s: take_till!(|c| is_quote(c as char)) >> quote >> (String::from_utf8_lossy(s.as_bytes()).to_string()) ) @@ -622,7 +622,7 @@ named!(pub bit_timing>, multispace0 >> tag!("BS_:") >> baudrates: opt!(preceded!(ms, separated_list!(comma, map!(u64_s, Baudrate)))) >> - (baudrates.unwrap_or(Vec::new())) + (baudrates.unwrap_or_default()) ) ); @@ -840,7 +840,7 @@ named!(env_float, value!(EnvType::EnvTypeFloat, char named!(env_int, value!(EnvType::EnvTypeu64, char!('1'))); named!(env_data, value!(EnvType::EnvTypeData, char!('2'))); -/// 9 Environment Variable Definitions +// 9 Environment Variable Definitions named!(pub env_var_type, alt_complete!(env_float | env_int | env_data)); named!(dummy_node_vector_0, value!(AccessType::DummyNodeVector0, char!('0'))); @@ -848,7 +848,7 @@ named!(dummy_node_vector_1, value!(AccessType::Du named!(dummy_node_vector_2, value!(AccessType::DummyNodeVector2, char!('2'))); named!(dummy_node_vector_3, value!(AccessType::DummyNodeVector3, char!('3'))); -/// 9 Environment Variable Definitions +// 9 Environment Variable Definitions named!(pub access_type, do_parse!( tag!("DUMMY_NODE_VECTOR") >> @@ -860,10 +860,10 @@ named!(pub access_type, named!(access_node_vector_xxx, value!(AccessNode::AccessNodeVectorXXX, tag!("VECTOR_XXX"))); named!(access_node_name, map!(c_ident, |name| AccessNode::AccessNodeName(name))); -/// 9 Environment Variable Definitions +// 9 Environment Variable Definitions named!(pub access_node, alt_complete!(access_node_vector_xxx | access_node_name)); -/// 9 Environment Variable Definitions +// 9 Environment Variable Definitions named!(pub environment_variable, do_parse!( multispace0 >> @@ -1069,7 +1069,7 @@ named!(pub attribute_definition_node, do_parse!( tag!("BU_") >> ms >> - x: map!(take_till_s!(|c |is_semi_colon(c as char)), |x| String::from_utf8(x.as_bytes().to_vec()).unwrap()) >> + x: map!(take_till!(|c |is_semi_colon(c as char)), |x| String::from_utf8(x.as_bytes().to_vec()).unwrap()) >> (AttributeDefinition::Node(x)) ) ); @@ -1079,7 +1079,7 @@ named!(pub attribute_definition_signal, do_parse!( tag!("SG_") >> ms >> - x: map!(take_till_s!(|c |is_semi_colon(c as char)), |x| String::from_utf8(x.as_bytes().to_vec()).unwrap()) >> + x: map!(take_till!(|c |is_semi_colon(c as char)), |x| String::from_utf8(x.as_bytes().to_vec()).unwrap()) >> (AttributeDefinition::Signal(x)) ) ); @@ -1089,7 +1089,7 @@ named!(pub attribute_definition_environment_variable> ms >> - x: map!(take_till_s!(|c |is_semi_colon(c as char)), |x| String::from_utf8(x.as_bytes().to_vec()).unwrap()) >> + x: map!(take_till!(|c |is_semi_colon(c as char)), |x| String::from_utf8(x.as_bytes().to_vec()).unwrap()) >> (AttributeDefinition::EnvironmentVariable(x)) ) ); @@ -1099,7 +1099,7 @@ named!(pub attribute_definition_message, do_parse!( tag!("BO_") >> ms >> - x: map!(take_till_s!(|c |is_semi_colon(c as char)), |x| String::from_utf8(x.as_bytes().to_vec()).unwrap()) >> + x: map!(take_till!(|c |is_semi_colon(c as char)), |x| String::from_utf8(x.as_bytes().to_vec()).unwrap()) >> (AttributeDefinition::Message(x)) ) ); @@ -1107,7 +1107,7 @@ named!(pub attribute_definition_message, // TODO add properties named!(pub attribute_definition_plain, do_parse!( - x: map!(take_till_s!(|c |is_semi_colon(c as char)), |x| String::from_utf8(x.as_bytes().to_vec()).unwrap()) >> + x: map!(take_till!(|c |is_semi_colon(c as char)), |x| String::from_utf8(x.as_bytes().to_vec()).unwrap()) >> (AttributeDefinition::Plain(x)) ) ); @@ -1149,9 +1149,9 @@ named!(pub new_symbols>, ) ); -/// -/// Network node -/// +// +// Network node +// named!(pub node, do_parse!( multispace0 >>