diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-05-19 10:32:11 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-05-19 10:32:11 -0400 |
commit | 2f12e1215c5f259dd0ccb6d5d11e7900d1fe8f4e (patch) | |
tree | 5d7e94d06c5eb573e950e578d189df30d7a80038 /src/sort.rs | |
parent | 5b773910d801143baa11f706daaa85786b49fc7d (diff) |
change enum names in sort
Diffstat (limited to 'src/sort.rs')
-rw-r--r-- | src/sort.rs | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/src/sort.rs b/src/sort.rs index 4f59449..3c48f15 100644 --- a/src/sort.rs +++ b/src/sort.rs @@ -4,13 +4,28 @@ use std::time; use crate::structs; -#[derive(Debug, Clone)] +use alphanumeric_sort::compare_str; +use serde_derive::Deserialize; + +#[derive(Clone, Debug, Deserialize)] pub enum SortType { - SortNatural, - SortMtime, + Lexical, + Mtime, + Natural, +} + +impl SortType { + pub fn parse(s: &str) -> Option<Self> { + match s { + "mtime" => Some(SortType::Mtime), + "natural" => Some(SortType::Natural), + "lexical" => Some(SortType::Lexical), + _ => None, + } + } } -#[derive(Debug, Clone)] +#[derive(Clone, Debug)] pub struct SortOption { pub show_hidden: bool, pub directories_first: bool, @@ -24,14 +39,21 @@ impl SortOption { &self, ) -> impl Fn(&structs::JoshutoDirEntry, &structs::JoshutoDirEntry) -> std::cmp::Ordering { let base_cmp = match self.sort_method { - SortType::SortNatural => { + SortType::Natural => { if self.case_sensitive { natural_sort } else { natural_sort_case_insensitive } } - SortType::SortMtime => mtime_sort, + SortType::Lexical => { + if self.case_sensitive { + natural_sort + } else { + natural_sort_case_insensitive + } + } + SortType::Mtime => mtime_sort, }; let rev_cmp = if self.reverse { @@ -57,6 +79,18 @@ impl SortOption { } } +impl std::default::Default for SortOption { + fn default() -> Self { + SortOption { + show_hidden: false, + directories_first: true, + case_sensitive: false, + reverse: false, + sort_method: SortType::Natural, + } + } +} + const fn no_filter(_: &Result<fs::DirEntry, std::io::Error>) -> bool { true } @@ -121,13 +155,11 @@ fn natural_sort_case_insensitive( ) -> cmp::Ordering { let f1_name = f1.file_name_as_string.to_lowercase(); let f2_name = f2.file_name_as_string.to_lowercase(); - f1_name.partial_cmp(&f2_name).unwrap_or(cmp::Ordering::Less) + compare_str(&f1_name, &f2_name) } fn natural_sort(f1: &structs::JoshutoDirEntry, f2: &structs::JoshutoDirEntry) -> cmp::Ordering { - f1.file_name - .partial_cmp(&f2.file_name) - .unwrap_or(cmp::Ordering::Less) + compare_str(&f1.file_name_as_string, &f2.file_name_as_string) } fn mtime_sort(file1: &structs::JoshutoDirEntry, file2: &structs::JoshutoDirEntry) -> cmp::Ordering { |