-
Notifications
You must be signed in to change notification settings - Fork 39
Added missing Attribute Definitions #22
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ddde8aa
added value type definitions
f75cbf2
removed relevant todo comments
d0a0137
fmt
3c6d433
deny changes
69c4ea9
mini clippy change
badcf55
Merge branch 'main' into dev
nyurik 57cba9b
implement new parsing
nyurik 1e6fa63
clippy
nyurik 30ce6db
Merge branch 'main' into dev
nyurik 54194f6
Merge branch 'main' into dev
nyurik b5a0f4b
cleaner parsing
nyurik 2d353c1
Merge branch 'main' into dev
nyurik 3f57085
new NumericValue enum to parse everything safely
nyurik 9a282d2
use numeric parsing in attr value
nyurik 49d0c26
validate min/max is empty
nyurik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,63 +1,43 @@ | ||
| use can_dbc_pest::{Pair, Pairs, Rule}; | ||
| use can_dbc_pest::{Pair, Rule}; | ||
|
|
||
| use crate::parser::{validated_inner, DbcError}; | ||
| use crate::DbcResult; | ||
| use crate::parser::{ | ||
| expect_empty, inner_str, next, next_optional_rule, next_rule, validated_inner, DbcError, | ||
| }; | ||
| use crate::AttributeValueType; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq)] | ||
| #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
| pub enum AttributeDefinition { | ||
| // TODO add properties | ||
| Message(String), | ||
| // TODO add properties | ||
| Node(String), | ||
| // TODO add properties | ||
| Signal(String), | ||
| EnvironmentVariable(String), | ||
| // TODO figure out name | ||
| Plain(String), | ||
| Message(String, AttributeValueType), | ||
| Node(String, AttributeValueType), | ||
| Signal(String, AttributeValueType), | ||
| EnvironmentVariable(String, AttributeValueType), | ||
| Plain(String, AttributeValueType), | ||
| } | ||
|
|
||
| impl TryFrom<Pair<'_, Rule>> for AttributeDefinition { | ||
| type Error = DbcError; | ||
|
|
||
| /// Parse attribute definition: `BA_DEF_ [object_type] attribute_name attribute_type [min max];` | ||
| fn try_from(value: Pair<'_, Rule>) -> Result<Self, Self::Error> { | ||
| let inner_pairs = validated_inner(value, Rule::attr_def)?; | ||
| let (definition_string, object_type) = parse_obj_type_vals(inner_pairs)?; | ||
| let mut pairs = validated_inner(value, Rule::attr_def)?; | ||
|
|
||
| Ok(match object_type { | ||
| "SG_" => Self::Signal(definition_string), | ||
| "BO_" => Self::Message(definition_string), | ||
| "BU_" => Self::Node(definition_string), | ||
| "EV_" => Self::EnvironmentVariable(definition_string), | ||
| _ => Self::Plain(definition_string), | ||
| }) | ||
| } | ||
| } | ||
| let object_type = if let Some(v) = next_optional_rule(&mut pairs, Rule::object_type) { | ||
| v.as_str().to_string() | ||
| } else { | ||
| String::new() | ||
| }; | ||
|
|
||
| pub(crate) fn parse_obj_type_vals(inner_pairs: Pairs<'_, Rule>) -> DbcResult<(String, &str)> { | ||
| let mut definition_string = String::new(); | ||
| let mut object_type = ""; | ||
| let name = inner_str(next_rule(&mut pairs, Rule::attribute_name)?); | ||
| let value = next(&mut pairs)?.try_into()?; | ||
| expect_empty(&pairs)?; | ||
|
|
||
| // Process all pairs | ||
| for pair in inner_pairs { | ||
| match pair.as_rule() { | ||
| Rule::object_type => { | ||
| object_type = pair.as_str(); | ||
| } | ||
| Rule::attribute_name | ||
| | Rule::attribute_type_int | ||
| | Rule::attribute_type_hex | ||
| | Rule::attribute_type_float | ||
| | Rule::attribute_type_string | ||
| | Rule::attribute_type_enum => { | ||
| if !definition_string.is_empty() { | ||
| definition_string.push(' '); | ||
| } | ||
| definition_string.push_str(pair.as_str()); | ||
| } | ||
| v => return Err(DbcError::UnknownRule(v)), | ||
| } | ||
| Ok(match object_type.as_str() { | ||
| "SG_" => Self::Signal(name, value), | ||
| "BO_" => Self::Message(name, value), | ||
| "BU_" => Self::Node(name, value), | ||
| "EV_" => Self::EnvironmentVariable(name, value), | ||
| _ => Self::Plain(name, value), | ||
| }) | ||
| } | ||
| Ok((definition_string, object_type)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,51 @@ | ||
| // FIXME: not used! | ||
| use can_dbc_pest::{Pair, Rule}; | ||
|
|
||
| use crate::parser::{expect_empty, inner_str, next_rule}; | ||
| use crate::{DbcError, DbcResult, NumericValue}; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq)] | ||
| #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
| pub enum AttributeValueType { | ||
| Int(i64, i64), | ||
| Hex(i64, i64), | ||
| Float(f64, f64), | ||
| Int(NumericValue, NumericValue), | ||
| Hex(NumericValue, NumericValue), | ||
| Float(NumericValue, NumericValue), | ||
| String, | ||
| Enum(Vec<String>), | ||
| } | ||
|
|
||
| impl TryFrom<Pair<'_, Rule>> for AttributeValueType { | ||
| type Error = DbcError; | ||
|
|
||
| fn try_from(pair: Pair<'_, Rule>) -> Result<Self, Self::Error> { | ||
| let rule = pair.as_rule(); | ||
| Ok(match rule { | ||
| Rule::attribute_type_int | Rule::attribute_type_hex | Rule::attribute_type_float => { | ||
| let mut pairs = pair.into_inner(); | ||
| let min = next_rule(&mut pairs, Rule::minimum)?.as_str().parse()?; | ||
| let max = next_rule(&mut pairs, Rule::maximum)?.as_str().parse()?; | ||
| expect_empty(&pairs)?; | ||
| match rule { | ||
| Rule::attribute_type_int => AttributeValueType::Int(min, max), | ||
| Rule::attribute_type_hex => AttributeValueType::Hex(min, max), | ||
| Rule::attribute_type_float => AttributeValueType::Float(min, max), | ||
| _ => unreachable!(), | ||
| } | ||
| } | ||
| Rule::attribute_type_string => AttributeValueType::String, | ||
| Rule::attribute_type_enum => { | ||
| let enum_values: DbcResult<_> = pair | ||
| .into_inner() | ||
| .map(|pair| { | ||
| if pair.as_rule() == Rule::quoted_str { | ||
| Ok(inner_str(pair)) | ||
| } else { | ||
| Err(DbcError::ExpectedRule(Rule::quoted_str, pair.as_rule())) | ||
| } | ||
| }) | ||
| .collect(); | ||
| AttributeValueType::Enum(enum_values?) | ||
| } | ||
| v => return Err(DbcError::UnknownRule(v)), | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| use std::str::FromStr; | ||
|
|
||
| use crate::DbcError; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq)] | ||
| #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
| pub enum NumericValue { | ||
| Uint(u64), | ||
| Int(i64), | ||
| Double(f64), | ||
| } | ||
|
|
||
| impl FromStr for NumericValue { | ||
| type Err = DbcError; | ||
|
|
||
| fn from_str(value: &str) -> Result<Self, Self::Err> { | ||
| if let Ok(v) = value.parse::<u64>() { | ||
| Ok(NumericValue::Uint(v)) | ||
| } else if let Ok(v) = value.parse::<i64>() { | ||
| Ok(NumericValue::Int(v)) | ||
| } else if let Ok(v) = value.parse::<f64>() { | ||
| Ok(NumericValue::Double(v)) | ||
| } else { | ||
| Err(DbcError::InvalidNumericValue(value.to_string())) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.