Skip to content

Commit

Permalink
Sorting improvements - replace custom comparison with Ord/PartialOrd …
Browse files Browse the repository at this point in the history
…implementation

Comparison of elements is now recursive, so element content can be taken into account.
For example, references are sorted by thei reference string, and this also works
for nested references like FIBEX-ELEMENTS.

Fixes issue #20
  • Loading branch information
DanielT committed Aug 16, 2024
1 parent 9aba7fc commit 2adbd54
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 228 deletions.
3 changes: 0 additions & 3 deletions autosar-data-specification/src/autosarversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ impl AutosarVersion {
Self::Autosar_00050 => "AUTOSAR_00050.xsd",
Self::Autosar_00051 => "AUTOSAR_00051.xsd",
Self::Autosar_00052 => "AUTOSAR_00052.xsd",

}
}

Expand Down Expand Up @@ -109,7 +108,6 @@ impl AutosarVersion {
Self::Autosar_00050 => "AUTOSAR R21-11",
Self::Autosar_00051 => "AUTOSAR R22-11",
Self::Autosar_00052 => "AUTOSAR R23-11",

}
}

Expand Down Expand Up @@ -163,4 +161,3 @@ impl std::fmt::Display for AutosarVersion {
f.write_str(self.describe())
}
}

2 changes: 1 addition & 1 deletion autosar-data-specification/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
//! ## Crate features
//!
//! * **docstrings** - Enables the function `ElementType::docstring`, which allows you to retrieve element documentation.
//! This feature increases the size of the compiled code, because all docstrings are compiled in. It is disabled by default.
//! This feature increases the size of the compiled code, because all docstrings are compiled in. It is disabled by default.
//!
//! ## Note
//!
Expand Down
31 changes: 31 additions & 0 deletions autosar-data/src/chardata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,37 @@ impl Display for CharacterData {
}
}

impl Ord for CharacterData {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
// compare two CharacterData values
// if the types are different, the order is determined by the type so that there is a consistent order
// sort order: enum < string < unsigned integer < double - this is arbitrary
match (self, other) {
(CharacterData::Enum(a), CharacterData::Enum(b)) => a.to_str().cmp(b.to_str()),
(CharacterData::String(a), CharacterData::String(b)) => a.cmp(b),
(CharacterData::UnsignedInteger(a), CharacterData::UnsignedInteger(b)) => a.cmp(b),
(CharacterData::Double(a), CharacterData::Double(b)) => {
a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
}
(CharacterData::Enum(_), _) => std::cmp::Ordering::Less,
(CharacterData::String(_), CharacterData::Enum(_)) => std::cmp::Ordering::Greater,
(CharacterData::String(_), _) => std::cmp::Ordering::Less,
(CharacterData::UnsignedInteger(_), CharacterData::Enum(_)) => std::cmp::Ordering::Greater,
(CharacterData::UnsignedInteger(_), CharacterData::String(_)) => std::cmp::Ordering::Greater,
(CharacterData::UnsignedInteger(_), _) => std::cmp::Ordering::Less,
(CharacterData::Double(_), _) => std::cmp::Ordering::Greater,
}
}
}

impl PartialOrd for CharacterData {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Eq for CharacterData {}

fn escape_text(input: &str) -> Cow<str> {
if input.contains(['&', '>', '<', '\'', '"']) {
let mut escaped = String::with_capacity(input.len() + 6);
Expand Down
Loading

0 comments on commit 2adbd54

Please sign in to comment.