alphabetical order in toc

This commit is contained in:
Mylloon 2024-11-09 17:46:53 +01:00
parent 754e717a58
commit a5240fea57
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -1,21 +1,40 @@
use std::path::Path; use std::{cmp::Ordering, path::Path};
use regex::Regex; use regex::Regex;
use serde::Serialize; use serde::Serialize;
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize, PartialEq, Eq)]
pub struct FileNode { pub struct FileNode {
name: String, name: String,
is_dir: bool, is_dir: bool,
children: Vec<FileNode>, children: Vec<FileNode>,
} }
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<Ordering> {
Some(self.cmp(other))
}
}
pub fn get_filetree( pub fn get_filetree(
dir_path: &str, dir_path: &str,
exclusion_list: &[String], exclusion_list: &[String],
exclusion_patterns: &[Regex], exclusion_patterns: &[Regex],
) -> FileNode { ) -> FileNode {
let children = std::fs::read_dir(dir_path) let mut children: Vec<FileNode> = std::fs::read_dir(dir_path)
.unwrap() .unwrap()
.filter_map(Result::ok) .filter_map(Result::ok)
.filter_map(|entry| { .filter_map(|entry| {
@ -49,6 +68,8 @@ pub fn get_filetree(
}) })
.collect(); .collect();
children.sort();
FileNode { FileNode {
name: Path::new(dir_path) name: Path::new(dir_path)
.file_name() .file_name()