diff --git a/src/traits.rs b/src/traits.rs index b7ff999..20051fb 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -100,6 +100,8 @@ pub trait CompleteCalendar : BaseCalendar { async fn get_item_ids(&self) -> Result, Box>; /// Returns all items that this calendar contains + /// + /// See [`crate::utils::comparison`] for helper functions that help sorting the results async fn get_items(&self) -> Result, Box>; /// Returns a particular item diff --git a/src/utils/comparison.rs b/src/utils/comparison.rs new file mode 100644 index 0000000..db0095b --- /dev/null +++ b/src/utils/comparison.rs @@ -0,0 +1,10 @@ +//! Utilities to compare custom types +//! +//! These can be used to sort results, e.g. by using `sorted_by` from the `itertools` crate + +use crate::item::{Item, ItemId}; + +/// Compare alphabetically types returned e.g. by [`crate::traits::CompleteCalendar::get_items`] +pub fn compare_items_alpha(left: &(&ItemId, &&Item), right: &(&ItemId, &&Item)) -> std::cmp::Ordering { + Ord::cmp(&left.1.name().to_lowercase(), &right.1.name().to_lowercase()) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 9aba3a5..82519e2 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -13,6 +13,8 @@ use crate::calendar::CalendarId; use crate::Item; use crate::item::SyncStatus; +pub mod comparison; + /// Walks an XML tree and returns every element that has the given name pub fn find_elems>(root: &Element, searched_name: S) -> Vec<&Element> { let searched_name = searched_name.as_ref();