Skip to content

Commit 28368c6

Browse files
committed
Merge PR oxibus#22: Added missing Attribute Definitions
2 parents 467717d + d0a0137 commit 28368c6

File tree

2 files changed

+185
-35
lines changed

2 files changed

+185
-35
lines changed

src/lib.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -577,15 +577,11 @@ pub struct AttributeValueForObject {
577577
#[derive(Clone, Debug, PartialEq)]
578578
#[cfg_attr(feature = "serde", derive(Serialize))]
579579
pub enum AttributeDefinition {
580-
// TODO add properties
581-
Message(String),
582-
// TODO add properties
583-
Node(String),
584-
// TODO add properties
585-
Signal(String),
586-
EnvironmentVariable(String),
587-
// TODO figure out name
588-
Plain(String),
580+
Message(String, AttributeValueType),
581+
Node(String, AttributeValueType),
582+
Signal(String, AttributeValueType),
583+
EnvironmentVariable(String, AttributeValueType),
584+
Plain(String, AttributeValueType),
589585
}
590586

591587
/// Encoding for signal raw values.

src/parser.rs

Lines changed: 180 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,70 @@ use nom::bytes::complete::{escaped, take_till1};
1919
use nom::character::complete::one_of;
2020
use crate::{
2121
AccessNode, AccessType, AttributeDefault, AttributeDefinition, AttributeValue,
22-
AttributeValueForObject, AttributeValuedForObjectType, Baudrate, ByteOrder, Comment, EnvType,
23-
EnvironmentVariable, EnvironmentVariableData, ExtendedMultiplex, ExtendedMultiplexMapping,
24-
Message, MessageId, MessageTransmitter, MultiplexIndicator, Node, Signal,
25-
SignalExtendedValueType, SignalExtendedValueTypeList, SignalGroups, SignalType, SignalTypeRef,
26-
Symbol, Transmitter, ValDescription, ValueDescription, ValueTable, ValueType, Version, DBC,
22+
AttributeValueForObject, AttributeValueType, AttributeValuedForObjectType, Baudrate, ByteOrder,
23+
Comment, EnvType, EnvironmentVariable, EnvironmentVariableData, ExtendedMultiplex,
24+
ExtendedMultiplexMapping, Message, MessageId, MessageTransmitter, MultiplexIndicator, Node,
25+
Signal, SignalExtendedValueType, SignalExtendedValueTypeList, SignalGroups, SignalType,
26+
SignalTypeRef, Symbol, Transmitter, ValDescription, ValueDescription, ValueTable, ValueType,
27+
Version, DBC,
2728
};
2829

2930
#[cfg(test)]
3031
mod tests {
3132
use super::*;
3233

34+
#[test]
35+
fn attribute_value_type_int_test() {
36+
let (_, attr_val) =
37+
attribute_value_type_int("INT -1 100").expect("Parsing INT attr definition failed");
38+
assert_eq!(attr_val, AttributeValueType::AttributeValueTypeInt(-1, 100));
39+
}
40+
#[test]
41+
fn attribute_value_type_test() {
42+
let (_, attr_val) =
43+
attribute_value_type("INT -1 100 ;\n").expect("Parsing INT attr definition failed");
44+
assert_eq!(attr_val, AttributeValueType::AttributeValueTypeInt(-1, 100));
45+
46+
let (_, attr_val) =
47+
attribute_value_type("HEX -1 100 ;\n").expect("Parsing HEX attr definition failed");
48+
assert_eq!(attr_val, AttributeValueType::AttributeValueTypeHex(-1, 100));
49+
50+
let (_, attr_val) =
51+
attribute_value_type("STRING ;\n").expect("Parsing STRING attr definition failed");
52+
assert_eq!(attr_val, AttributeValueType::AttributeValueTypeString);
53+
54+
let (_, attr_val) =
55+
attribute_value_type("FLOAT 0.2 1.7;\n").expect("Parsing FLOAT attr definition failed");
56+
assert_eq!(
57+
attr_val,
58+
AttributeValueType::AttributeValueTypeFloat(0.2f64, 1.7f64)
59+
);
60+
61+
let (_, attr_val) =
62+
attribute_value_type("ENUM \"NEE\" , \"OCH\", \"MENSCH\" , \"DOCH\";\n")
63+
.expect("Parsing ENUM attr definition failed");
64+
assert_eq!(
65+
attr_val,
66+
AttributeValueType::AttributeValueTypeEnum(vec![
67+
"NEE".to_string(),
68+
"OCH".to_string(),
69+
"MENSCH".to_string(),
70+
"DOCH".to_string()
71+
])
72+
);
73+
74+
let (_, attr_val) =
75+
attribute_value_type("ENUM \"NEE\";\n").expect("Parsing ENUM attr definition failed");
76+
assert_eq!(
77+
attr_val,
78+
AttributeValueType::AttributeValueTypeEnum(vec!["NEE".to_string()])
79+
);
80+
81+
let (_, attr_val) =
82+
attribute_value_type("ENUM;\n").expect("Parsing ENUM attr definition failed");
83+
assert_eq!(attr_val, AttributeValueType::AttributeValueTypeEnum(vec![]));
84+
}
85+
3386
#[test]
3487
fn c_ident_test() {
3588
let def1 = "EALL_DUSasb18 ";
@@ -436,23 +489,34 @@ mod tests {
436489
fn attribute_definition_test() {
437490
let def_bo = "BA_DEF_ BO_ \"BaDef1BO\" INT 0 1000000;\n";
438491
let (_, bo_def) = attribute_definition(def_bo).unwrap();
439-
let bo_def_exp = AttributeDefinition::Message("\"BaDef1BO\" INT 0 1000000".to_string());
492+
let bo_def_exp = AttributeDefinition::Message(
493+
"BaDef1BO".to_string(),
494+
AttributeValueType::AttributeValueTypeInt(0, 1000000),
495+
);
440496
assert_eq!(bo_def_exp, bo_def);
441497

442498
let def_bu = "BA_DEF_ BU_ \"BuDef1BO\" INT 0 1000000;\n";
443499
let (_, bu_def) = attribute_definition(def_bu).unwrap();
444-
let bu_def_exp = AttributeDefinition::Node("\"BuDef1BO\" INT 0 1000000".to_string());
500+
let bu_def_exp = AttributeDefinition::Node(
501+
"BuDef1BO".to_string(),
502+
AttributeValueType::AttributeValueTypeInt(0, 1000000),
503+
);
445504
assert_eq!(bu_def_exp, bu_def);
446505

447506
let def_signal = "BA_DEF_ SG_ \"SgDef1BO\" INT 0 1000000;\n";
448507
let (_, signal_def) = attribute_definition(def_signal).unwrap();
449-
let signal_def_exp = AttributeDefinition::Signal("\"SgDef1BO\" INT 0 1000000".to_string());
508+
let signal_def_exp = AttributeDefinition::Signal(
509+
"SgDef1BO".to_string(),
510+
AttributeValueType::AttributeValueTypeInt(0, 1000000),
511+
);
450512
assert_eq!(signal_def_exp, signal_def);
451513

452514
let def_env_var = "BA_DEF_ EV_ \"EvDef1BO\" INT 0 1000000;\n";
453515
let (_, env_var_def) = attribute_definition(def_env_var).unwrap();
454-
let env_var_def_exp =
455-
AttributeDefinition::EnvironmentVariable("\"EvDef1BO\" INT 0 1000000".to_string());
516+
let env_var_def_exp = AttributeDefinition::EnvironmentVariable(
517+
"EvDef1BO".to_string(),
518+
AttributeValueType::AttributeValueTypeInt(0, 1000000),
519+
);
456520
assert_eq!(env_var_def_exp, env_var_def);
457521
}
458522

@@ -1336,45 +1400,136 @@ fn attribute_value_for_object(s: &str) -> IResult<&str, AttributeValueForObject>
13361400
))
13371401
}
13381402

1339-
// TODO add properties
13401403
fn attribute_definition_node(s: &str) -> IResult<&str, AttributeDefinition> {
13411404
let (s, _) = tag("BU_")(s)?;
13421405
let (s, _) = ms1(s)?;
1343-
let (s, node) = take_till(is_semi_colon)(s)?;
1344-
Ok((s, AttributeDefinition::Node(node.to_string())))
1406+
let (s, name) = char_string(s)?;
1407+
1408+
let (s, _) = ms0(s)?;
1409+
let (s, val) = attribute_value_type(s)?;
1410+
Ok((s, AttributeDefinition::Node(name.to_string(), val)))
13451411
}
13461412

1347-
// TODO add properties
13481413
fn attribute_definition_signal(s: &str) -> IResult<&str, AttributeDefinition> {
13491414
let (s, _) = tag("SG_")(s)?;
13501415
let (s, _) = ms1(s)?;
1351-
let (s, signal) = take_till(is_semi_colon)(s)?;
1352-
Ok((s, AttributeDefinition::Signal(signal.to_string())))
1416+
let (s, name) = char_string(s)?;
1417+
1418+
let (s, _) = ms0(s)?;
1419+
let (s, val) = attribute_value_type(s)?;
1420+
Ok((s, AttributeDefinition::Signal(name.to_string(), val)))
13531421
}
13541422

1355-
// TODO add properties
13561423
fn attribute_definition_environment_variable(s: &str) -> IResult<&str, AttributeDefinition> {
13571424
let (s, _) = tag("EV_")(s)?;
13581425
let (s, _) = ms1(s)?;
1359-
let (s, env_var) = take_till(is_semi_colon)(s)?;
1426+
let (s, name) = char_string(s)?;
1427+
1428+
let (s, _) = ms0(s)?;
1429+
let (s, val) = attribute_value_type(s)?;
13601430
Ok((
13611431
s,
1362-
AttributeDefinition::EnvironmentVariable(env_var.to_string()),
1432+
AttributeDefinition::EnvironmentVariable(name.to_string(), val),
13631433
))
13641434
}
13651435

1366-
// TODO add properties
13671436
fn attribute_definition_message(s: &str) -> IResult<&str, AttributeDefinition> {
13681437
let (s, _) = tag("BO_")(s)?;
13691438
let (s, _) = ms1(s)?;
1370-
let (s, message) = take_till(is_semi_colon)(s)?;
1371-
Ok((s, AttributeDefinition::Message(message.to_string())))
1439+
let (s, name) = char_string(s)?;
1440+
1441+
let (s, _) = ms0(s)?;
1442+
let (s, val) = attribute_value_type(s)?;
1443+
Ok((s, AttributeDefinition::Message(name.to_string(), val)))
13721444
}
13731445

1374-
// TODO add properties
13751446
fn attribute_definition_plain(s: &str) -> IResult<&str, AttributeDefinition> {
1376-
let (s, plain) = take_till(is_semi_colon)(s)?;
1377-
Ok((s, AttributeDefinition::Plain(plain.to_string())))
1447+
let (s, name) = char_string(s)?;
1448+
let (s, val) = attribute_value_type(s)?;
1449+
Ok((s, AttributeDefinition::Plain(name.to_string(), val)))
1450+
}
1451+
1452+
fn attribute_value_type_int(s: &str) -> IResult<&str, AttributeValueType> {
1453+
let (s, _) = multispace0(s)?;
1454+
let (s, _) = tag("INT")(s)?;
1455+
let (s, _) = ms1(s)?;
1456+
let (s, first) = complete::i64(s)?;
1457+
let (s, _) = ms1(s)?;
1458+
let (s, second) = complete::i64(s)?;
1459+
1460+
let (s, _) = ms0(s)?;
1461+
Ok((s, AttributeValueType::AttributeValueTypeInt(first, second)))
1462+
}
1463+
1464+
fn attribute_value_type_float(s: &str) -> IResult<&str, AttributeValueType> {
1465+
let (s, _) = multispace0(s)?;
1466+
let (s, _) = tag("FLOAT")(s)?;
1467+
let (s, _) = ms1(s)?;
1468+
let (s, first) = double(s)?;
1469+
let (s, _) = ms1(s)?;
1470+
let (s, second) = double(s)?;
1471+
1472+
let (s, _) = ms0(s)?;
1473+
Ok((
1474+
s,
1475+
AttributeValueType::AttributeValueTypeFloat(first, second),
1476+
))
1477+
}
1478+
1479+
fn attribute_value_type_hex(s: &str) -> IResult<&str, AttributeValueType> {
1480+
let (s, _) = multispace0(s)?;
1481+
let (s, _) = tag("HEX")(s)?;
1482+
let (s, _) = ms1(s)?;
1483+
let (s, first) = complete::i64(s)?;
1484+
let (s, _) = ms1(s)?;
1485+
let (s, second) = complete::i64(s)?;
1486+
1487+
let (s, _) = ms0(s)?;
1488+
Ok((s, AttributeValueType::AttributeValueTypeHex(first, second)))
1489+
}
1490+
1491+
fn attribute_value_type_string(s: &str) -> IResult<&str, AttributeValueType> {
1492+
let (s, _) = multispace0(s)?;
1493+
let (s, _) = tag("STRING")(s)?;
1494+
let (s, _) = ms0(s)?;
1495+
Ok((s, AttributeValueType::AttributeValueTypeString))
1496+
}
1497+
1498+
fn attribute_value_type(s: &str) -> IResult<&str, AttributeValueType> {
1499+
let (s, _) = multispace0(s)?;
1500+
let (s, val) = alt((
1501+
attribute_value_type_int,
1502+
attribute_value_type_hex,
1503+
attribute_value_type_float,
1504+
attribute_value_type_string,
1505+
attribute_value_type_enum,
1506+
))(s)?;
1507+
1508+
Ok((s, val))
1509+
}
1510+
1511+
fn comma_seperated_char_string(s: &str) -> IResult<&str, String> {
1512+
let (s, _) = ms0(s)?;
1513+
let (s, _) = comma(s)?;
1514+
let (s, _) = ms0(s)?;
1515+
let (s, c_s) = char_string(s)?;
1516+
Ok((s, c_s.to_string()))
1517+
}
1518+
fn attribute_value_type_enum(s: &str) -> IResult<&str, AttributeValueType> {
1519+
let (s, _) = multispace0(s)?;
1520+
let (s, _) = tag("ENUM")(s)?;
1521+
let (s, _) = ms0(s)?;
1522+
let mut buffer: Vec<String> = vec![];
1523+
1524+
if let (s, Some(value)) = opt(char_string)(s)? {
1525+
buffer.push(value.to_string());
1526+
let (s, mut tail) = many0(comma_seperated_char_string)(s)?;
1527+
buffer.append(&mut tail);
1528+
Ok((s, AttributeValueType::AttributeValueTypeEnum(buffer)))
1529+
} else {
1530+
let (s, _) = ms0(s)?;
1531+
Ok((s, AttributeValueType::AttributeValueTypeEnum(buffer)))
1532+
}
13781533
}
13791534

13801535
fn attribute_definition(s: &str) -> IResult<&str, AttributeDefinition> {
@@ -1388,7 +1543,6 @@ fn attribute_definition(s: &str) -> IResult<&str, AttributeDefinition> {
13881543
attribute_definition_message,
13891544
attribute_definition_plain,
13901545
))(s)?;
1391-
13921546
let (s, _) = semi_colon(s)?;
13931547
let (s, _) = line_ending(s)?;
13941548
Ok((s, def))

0 commit comments

Comments
 (0)