|
13 | 13 | // You should have received a copy of the GNU General Public License |
14 | 14 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
15 | 15 |
|
| 16 | +//! Miscellaneous functions. |
| 17 | +
|
| 18 | +use std::cmp::Ordering; |
| 19 | +use std::sync::LazyLock; |
| 20 | + |
| 21 | +use icu_collator::options::{AlternateHandling, CaseLevel, CollatorOptions, Strength}; |
| 22 | +use icu_collator::preferences::{CollationCaseFirst, CollationNumericOrdering}; |
| 23 | +use icu_collator::{Collator, CollatorBorrowed, CollatorPreferences}; |
| 24 | + |
| 25 | +use mmm_core::file_tree::{TreeNode, TreeNodeKind}; |
| 26 | + |
| 27 | +static COLLATOR: LazyLock<CollatorBorrowed<'static>> = LazyLock::new(|| { |
| 28 | + let mut prefs = CollatorPreferences::default(); |
| 29 | + prefs.numeric_ordering = Some(CollationNumericOrdering::True); |
| 30 | + prefs.case_first = Some(CollationCaseFirst::False); |
| 31 | + |
| 32 | + let mut options = CollatorOptions::default(); |
| 33 | + options.strength = Some(Strength::Tertiary); |
| 34 | + options.alternate_handling = Some(AlternateHandling::NonIgnorable); |
| 35 | + options.case_level = Some(CaseLevel::Off); |
| 36 | + |
| 37 | + Collator::try_new(prefs, options).unwrap() |
| 38 | +}); |
| 39 | + |
| 40 | +/// A comparator for [`TreeNode`]s that sorts directories before files |
| 41 | +/// and sorts names using the CLDR Collation Algorithm provided by ICU4X. |
| 42 | +pub fn node_ord<F>(left: &TreeNode<F>, right: &TreeNode<F>) -> Ordering { |
| 43 | + match (&left.kind, &right.kind) { |
| 44 | + (TreeNodeKind::Dir, TreeNodeKind::File(_)) => Ordering::Less, |
| 45 | + (TreeNodeKind::File(_), TreeNodeKind::Dir) => Ordering::Greater, |
| 46 | + _ => COLLATOR.compare(&left.name, &right.name), |
| 47 | + } |
| 48 | +} |
| 49 | + |
16 | 50 | /// Moves multiple items in a slice to the specified index. |
17 | 51 | /// |
18 | 52 | /// When moving items to the right, the target index needs to be adjusted to compensate for the items shifted left, |
|
0 commit comments