Skip to content

Commit

Permalink
Add information to some error messages
Browse files Browse the repository at this point in the history
- ElementInsertionConflict
- ElementNotFound
- DuplicateItemName
  • Loading branch information
DanielT committed Oct 21, 2024
1 parent dc9fa16 commit e6802f0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
9 changes: 5 additions & 4 deletions autosar-data/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4028,13 +4028,14 @@ mod test {
let ar_packages_lvl2_count = ar_packages_elem.elements_dfs_with_max_depth(2).count();
assert_eq!(root_lvl3_count, ar_packages_lvl2_count + 1);

root_elem.elements_dfs_with_max_depth(3).skip(1).zip(
ar_packages_elem.elements_dfs_with_max_depth(2)
).for_each(|((_, x), (_, y))| assert_eq!(x, y));
root_elem
.elements_dfs_with_max_depth(3)
.skip(1)
.zip(ar_packages_elem.elements_dfs_with_max_depth(2))
.for_each(|((_, x), (_, y))| assert_eq!(x, y));

for elem in ar_packages_elem.elements_dfs_with_max_depth(2) {
assert!(elem.0 <= 2);
}
}
}

21 changes: 16 additions & 5 deletions autosar-data/src/elementraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,9 @@ impl ElementRaw {
{
if multiplicity != ElementMultiplicity::Any {
// the new element is identical to an existing one, but repetitions are not allowed
return Err(AutosarDataError::ElementInsertionConflict);
return Err(AutosarDataError::ElementInsertionConflict {
element: element_name,
});
}
}
}
Expand All @@ -1020,15 +1022,17 @@ impl ElementRaw {
{
if multiplicity != ElementMultiplicity::Any {
// the new element is identical to an existing one, but repetitions are not allowed
return Err(AutosarDataError::ElementInsertionConflict);
return Err(AutosarDataError::ElementInsertionConflict {
element: element_name,
});
}
}
// the existing element and the new element are equal in positioning
// start_pos remains at its current value (< current position), while
// end_pos is increased to allow inserting before or after this element
end_pos = idx + 1;
} else {
return Err(AutosarDataError::ElementInsertionConflict);
return Err(AutosarDataError::ElementInsertionConflict { element: element_name });
}
}
ContentMode::Bag | ContentMode::Mixed => {
Expand Down Expand Up @@ -1075,7 +1079,10 @@ impl ElementRaw {
false
}
})
.ok_or(AutosarDataError::ElementNotFound)?;
.ok_or(AutosarDataError::ElementNotFound {
target: sub_element_locked.element_name(),
parent: self.element_name(),
})?;
if self.elemtype.is_named() && sub_element_locked.elemname == ElementName::ShortName {
// may not remove the SHORT-NAME, because that would leave the data in an invalid state
return Err(AutosarDataError::ShortNameRemovalForbidden);
Expand Down Expand Up @@ -1130,7 +1137,11 @@ impl ElementRaw {
}

// set the character data of this element - separated out since this part is not generic
fn set_character_data_internal(&mut self, chardata: CharacterData, version: AutosarVersion) -> Result<(), AutosarDataError> {
fn set_character_data_internal(
&mut self,
chardata: CharacterData,
version: AutosarVersion,
) -> Result<(), AutosarDataError> {
if self.elemtype.content_mode() == ContentMode::Characters
|| (self.elemtype.content_mode() == ContentMode::Mixed && self.content.len() <= 1)
{
Expand Down
10 changes: 5 additions & 5 deletions autosar-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,16 @@ pub enum AutosarDataError {
IncorrectContentType { element: ElementName },

/// Could not insert a sub element, because it conflicts with an existing sub element
#[error("Element insertion conflict")]
ElementInsertionConflict,
#[error("Element insertion conflict: {}", .element)]
ElementInsertionConflict { element: ElementName },

/// The `ElementName` is not a valid sub element according to the specification.
#[error("Invalid sub element: {}", .element)]
InvalidSubElement { element: ElementName },

/// Remove operation failed: the given element is not a sub element of the element from which it was supposed to be removed
#[error("element not found")]
ElementNotFound,
#[error("element {} not found in parent {}", .target, .parent)]
ElementNotFound { target: ElementName, parent: ElementName },

/// [`Element::remove_sub_element`] cannot remove the SHORT-NAME of identifiable elements, as this would render the data invalid
#[error("the SHORT-NAME sub element may not be removed")]
Expand All @@ -230,7 +230,7 @@ pub enum AutosarDataError {
InvalidReference,

/// An element could not be renamed, since this item name is already used by a different element
#[error("Duplicate item name")]
#[error("Duplicate item name {} in {}", .item_name, .element)]
DuplicateItemName { element: ElementName, item_name: String },

/// Cannot move an element into its own sub element
Expand Down

0 comments on commit e6802f0

Please sign in to comment.