diff options
Diffstat (limited to 'src/joshuto/sort.rs')
-rw-r--r-- | src/joshuto/sort.rs | 147 |
1 files changed, 70 insertions, 77 deletions
diff --git a/src/joshuto/sort.rs b/src/joshuto/sort.rs index 9d3dee5..f6b92d6 100644 --- a/src/joshuto/sort.rs +++ b/src/joshuto/sort.rs @@ -1,4 +1,3 @@ -use std; use std::cmp; use std::fs; use std::time; @@ -20,8 +19,9 @@ pub enum SortType { } impl SortType { - pub fn compare_func(&self) -> fn (&structs::JoshutoDirEntry, &structs::JoshutoDirEntry) -> std::cmp::Ordering - { + pub fn compare_func( + &self, + ) -> fn(&structs::JoshutoDirEntry, &structs::JoshutoDirEntry) -> std::cmp::Ordering { match *self { SortType::SortNatural(ref ss) => { if ss.directories_first && !ss.case_sensitive && !ss.reverse { @@ -42,8 +42,9 @@ impl SortType { } } - pub fn filter_func(&self) -> fn(Result<fs::DirEntry, std::io::Error>) -> Option<structs::JoshutoDirEntry> - { + pub fn filter_func( + &self, + ) -> fn(Result<fs::DirEntry, std::io::Error>) -> Option<structs::JoshutoDirEntry> { match *self { SortType::SortNatural(ref ss) => { if ss.show_hidden { @@ -51,94 +52,81 @@ impl SortType { } else { filter_hidden_files } - }, + } SortType::SortMtime(ref ss) => { if ss.show_hidden { filter_default } else { filter_hidden_files } - }, + } } } - pub fn show_hidden(&self) -> bool - { + pub fn show_hidden(&self) -> bool { match *self { - SortType::SortNatural(ref ss) => { - ss.show_hidden - }, - SortType::SortMtime(ref ss) => { - ss.show_hidden - }, + SortType::SortNatural(ref ss) => ss.show_hidden, + SortType::SortMtime(ref ss) => ss.show_hidden, } } - pub fn set_show_hidden(&mut self, show_hidden: bool) - { + pub fn set_show_hidden(&mut self, show_hidden: bool) { match self { SortType::SortNatural(ref mut ss) => { ss.show_hidden = show_hidden; - }, + } SortType::SortMtime(ref mut ss) => { ss.show_hidden = show_hidden; - }, + } } } } -fn filter_default(result : Result<fs::DirEntry, std::io::Error>) -> Option<structs::JoshutoDirEntry> -{ +fn filter_default( + result: Result<fs::DirEntry, std::io::Error>, +) -> Option<structs::JoshutoDirEntry> { match result { - Ok(direntry) => { - match structs::JoshutoDirEntry::from(&direntry) { - Ok(s) => Some(s), - Err(e) => { - eprintln!("error: {:?}", e); - None - }, + Ok(direntry) => match structs::JoshutoDirEntry::from(&direntry) { + Ok(s) => Some(s), + Err(e) => { + eprintln!("error: {:?}", e); + None } }, - Err(_) => { - None - } + Err(_) => None, } } -fn filter_hidden_files(result : Result<fs::DirEntry, std::io::Error>) -> Option<structs::JoshutoDirEntry> -{ +fn filter_hidden_files( + result: Result<fs::DirEntry, std::io::Error>, +) -> Option<structs::JoshutoDirEntry> { match result { - Ok(direntry) => { - match direntry.file_name().into_string() { - Ok(file_name) => { - if file_name.starts_with(".") { - None - } else { - match structs::JoshutoDirEntry::from(&direntry) { - Ok(s) => Some(s), - Err(e) => { - eprintln!("error: {:?}", e); - None - }, + Ok(direntry) => match direntry.file_name().into_string() { + Ok(file_name) => { + if file_name.starts_with(".") { + None + } else { + match structs::JoshutoDirEntry::from(&direntry) { + Ok(s) => Some(s), + Err(e) => { + eprintln!("error: {:?}", e); + None } } - }, - Err(_) => { - None - }, + } } + Err(_) => None, }, - Err(_) => { - None - } + Err(_) => None, } } pub struct SortNatural {} impl SortNatural { - pub fn dir_first_case_insensitive(file1 : &structs::JoshutoDirEntry, - file2 : &structs::JoshutoDirEntry) -> cmp::Ordering - { + pub fn dir_first_case_insensitive( + file1: &structs::JoshutoDirEntry, + file2: &structs::JoshutoDirEntry, + ) -> cmp::Ordering { let f1_isdir = file1.path.is_dir(); let f2_isdir = file2.path.is_dir(); @@ -157,9 +145,10 @@ impl SortNatural { } } - pub fn dir_first(file1: &structs::JoshutoDirEntry, - file2: &structs::JoshutoDirEntry) -> cmp::Ordering - { + pub fn dir_first( + file1: &structs::JoshutoDirEntry, + file2: &structs::JoshutoDirEntry, + ) -> cmp::Ordering { let f1_isdir = file1.path.is_dir(); let f2_isdir = file2.path.is_dir(); @@ -172,9 +161,10 @@ impl SortNatural { } } - pub fn default_sort(file1 : &structs::JoshutoDirEntry, - file2 : &structs::JoshutoDirEntry) -> cmp::Ordering - { + pub fn default_sort( + file1: &structs::JoshutoDirEntry, + file2: &structs::JoshutoDirEntry, + ) -> cmp::Ordering { if file1.file_name <= file2.file_name { cmp::Ordering::Less } else { @@ -185,12 +175,14 @@ impl SortNatural { pub struct SortMtime {} impl SortMtime { - pub fn dir_first(file1 : &structs::JoshutoDirEntry, - file2 : &structs::JoshutoDirEntry) -> cmp::Ordering - { - fn compare(file1 : &structs::JoshutoDirEntry, file2 : &structs::JoshutoDirEntry) - -> Result<cmp::Ordering, std::io::Error> - { + pub fn dir_first( + file1: &structs::JoshutoDirEntry, + file2: &structs::JoshutoDirEntry, + ) -> cmp::Ordering { + fn compare( + file1: &structs::JoshutoDirEntry, + file2: &structs::JoshutoDirEntry, + ) -> Result<cmp::Ordering, std::io::Error> { let f1_isdir = file1.path.is_dir(); let f2_isdir = file2.path.is_dir(); @@ -205,12 +197,14 @@ impl SortMtime { compare(&file1, &file2).unwrap_or(cmp::Ordering::Less) } - pub fn default_sort(file1: &structs::JoshutoDirEntry, - file2: &structs::JoshutoDirEntry) -> cmp::Ordering - { - fn compare(file1 : &structs::JoshutoDirEntry, file2 : &structs::JoshutoDirEntry) - -> Result<cmp::Ordering, std::io::Error> - { + pub fn default_sort( + file1: &structs::JoshutoDirEntry, + file2: &structs::JoshutoDirEntry, + ) -> cmp::Ordering { + fn compare( + file1: &structs::JoshutoDirEntry, + file2: &structs::JoshutoDirEntry, + ) -> Result<cmp::Ordering, std::io::Error> { let f1_meta: fs::Metadata = std::fs::metadata(&file1.path)?; let f2_meta: fs::Metadata = std::fs::metadata(&file2.path)?; @@ -218,12 +212,11 @@ impl SortMtime { let f2_mtime: time::SystemTime = f2_meta.modified()?; Ok(if f1_mtime <= f2_mtime { - cmp::Ordering::Less - } else { - cmp::Ordering::Greater + cmp::Ordering::Less + } else { + cmp::Ordering::Greater }) } compare(&file1, &file2).unwrap_or(cmp::Ordering::Less) } } - |