From a5240fea57996d1b6deb4fcc274411bd3faa6f2a Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 9 Nov 2024 17:46:53 +0100 Subject: [PATCH] alphabetical order in toc --- src/utils/routes/cours.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/utils/routes/cours.rs b/src/utils/routes/cours.rs index 7318e86..3259bbd 100644 --- a/src/utils/routes/cours.rs +++ b/src/utils/routes/cours.rs @@ -1,21 +1,40 @@ -use std::path::Path; +use std::{cmp::Ordering, path::Path}; use regex::Regex; use serde::Serialize; -#[derive(Clone, Debug, Serialize)] +#[derive(Clone, Debug, Serialize, PartialEq, Eq)] pub struct FileNode { name: String, is_dir: bool, children: Vec, } +impl Ord for FileNode { + fn cmp(&self, other: &Self) -> Ordering { + match (self.is_dir, other.is_dir) { + // If both are directories or both are files, compare names + (true, true) | (false, false) => self.name.cmp(&other.name), + // If self is directory and other is file, self comes first + (true, false) => Ordering::Less, + // If self is file and other is directory, other comes first + (false, true) => Ordering::Greater, + } + } +} + +impl PartialOrd for FileNode { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + pub fn get_filetree( dir_path: &str, exclusion_list: &[String], exclusion_patterns: &[Regex], ) -> FileNode { - let children = std::fs::read_dir(dir_path) + let mut children: Vec = std::fs::read_dir(dir_path) .unwrap() .filter_map(Result::ok) .filter_map(|entry| { @@ -49,6 +68,8 @@ pub fn get_filetree( }) .collect(); + children.sort(); + FileNode { name: Path::new(dir_path) .file_name()