Skip to content

Commit

Permalink
allow set_character_data on mixed content elements
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielT committed Sep 4, 2023
1 parent 07b7dbe commit 0cbf93d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
3 changes: 1 addition & 2 deletions autosar-data/src/autosarmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,6 @@ impl std::fmt::Debug for WeakAutosarModel {
}
}


#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -1328,7 +1327,7 @@ mod test {
let mut hashset = HashSet::<AutosarModel>::new();
hashset.insert(model);
let inserted = hashset.insert(model_cloned);
assert!(!inserted);
assert!(!inserted);

// CharacterData
let cdata = CharacterData::String("x".to_string());
Expand Down
14 changes: 6 additions & 8 deletions autosar-data/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,8 @@ impl Element {

/// Set the character data of this element
///
/// This method only applies to elements which contain character data, i.e. element.content_type == CharacterData
/// This method only applies to elements which contain character data, i.e. element.content_type == CharacterData or Mixed.
/// On elements with mixed content this function will replace all current content with the single new CharacterData item.
///
/// # Example
///
Expand All @@ -830,10 +831,10 @@ impl Element {
/// - [AutosarDataError::ItemDeleted]: The current element is in the deleted state and will be freed once the last reference is dropped
/// - [AutosarDataError::ParentElementLocked]: a parent element was locked and did not become available after waiting briefly.
/// The operation was aborted to avoid a deadlock, but can be retried.
/// - [AutosarDataError::IncorrectContentType]: Cannot set character data on an element whoch does not contain character data
/// - [AutosarDataError::IncorrectContentType]: Cannot set character data on an element which does not contain character data
pub fn set_character_data(&self, chardata: CharacterData) -> Result<(), AutosarDataError> {
let elemtype = self.elemtype();
if elemtype.content_mode() == ContentMode::Characters {
if elemtype.content_mode() == ContentMode::Characters || elemtype.content_mode() == ContentMode::Mixed {
if let Some(cdata_spec) = elemtype.chardata_spec() {
let model = self.model()?;
let version = self.min_version()?;
Expand All @@ -859,11 +860,8 @@ impl Element {
// update the character data
{
let mut element = self.0.lock();
if element.content.is_empty() {
element.content.push(ElementContent::CharacterData(chardata));
} else {
element.content[0] = ElementContent::CharacterData(chardata);
}
element.content.clear();
element.content.push(ElementContent::CharacterData(chardata));
}

// short-name: make sure the hashmap in the top-level AutosarModel is updated so that this element can still be found
Expand Down
4 changes: 1 addition & 3 deletions autosar-data/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,7 @@ mod test {
let data = b"<!-- foo--><element>";
let mut lexer = ArxmlLexer::new(data, PathBuf::from("(buffer)"));
// the comment is skipped, and lexer.next() directly returns the following element
assert!(
matches!(lexer.next(), Ok((_, ArxmlEvent::BeginElement(_elem, _attrs))))
);
assert!(matches!(lexer.next(), Ok((_, ArxmlEvent::BeginElement(_elem, _attrs)))));
}

#[test]
Expand Down
4 changes: 3 additions & 1 deletion autosar-data/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,9 @@ mod test {

#[test]
fn test_unexpected_xml_file_header() {
let discriminant = std::mem::discriminant(&ArxmlParserError::UnexpectedXmlFileHeader { element: ElementName::Autosar });
let discriminant = std::mem::discriminant(&ArxmlParserError::UnexpectedXmlFileHeader {
element: ElementName::Autosar,
});
test_helper(UNEXPECTED_XML_FILE_HEADER.as_bytes(), discriminant, true);
}

Expand Down

0 comments on commit 0cbf93d

Please sign in to comment.