summaryrefslogtreecommitdiffstats
path: root/src/sort.rs
blob: d40016ac73459746e9b2053ade490f8b6f402771 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::cmp;
use std::fs;
use std::time;

use crate::structs;

#[derive(Debug, Clone)]
pub enum SortType {
    SortNatural,
    SortMtime,
}

#[derive(Debug, Clone)]
pub struct SortOption {
    pub show_hidden: bool,
    pub directories_first: bool,
    pub case_sensitive: bool,
    pub reverse: bool,
    pub sort_method: SortType,
}

impl SortOption {
    pub fn compare_func(
        &self,
    ) -> impl Fn(&structs::JoshutoDirEntry, &structs::JoshutoDirEntry) -> std::cmp::Ordering {
        let base_cmp = match self.sort_method {
            SortType::SortNatural => {
                if self.case_sensitive {
                    natural_sort
                } else {
                    natural_sort_case_insensitive
                }
            }
            SortType::SortMtime => mtime_sort,
        };

        let rev_cmp = if self.reverse {
            reverse_ordering
        } else {
            dummy_reverse
        };
        let dir_cmp = if self.directories_first {
            dir_first
        } else {
            dummy_dir_first
        };

        move |f1, f2| dir_cmp(f1, f2).unwrap_or_else(|| rev_cmp(base_cmp(f1, f2)))
    }

    pub fn filter_func(&self) -> fn(&Result<fs::DirEntry, std::io::Error>) -> bool {
        if self.show_hidden {
            no_filter
        } else {
            filter_hidden
        }
    }
}

const fn no_filter(_: &Result<fs::DirEntry, std::io::Error>) -> bool {
    true
}

fn filter_hidden(result: &Result<fs::DirEntry, std::io::Error>) -> bool {
    match result {
        Err(_) => false,
        Ok(entry) => {
            let file_name = entry.file_name();
            if let Some(file_name) = file_name.to_str() {
                !file_name.starts_with(".")
            } else {
                false
            }
        }
    }
}

pub fn map_entry_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(_) => None,
        },
        Err(_) => None,
    }
}

const fn dummy_dir_first(
    _: &structs::JoshutoDirEntry,
    _: &structs::JoshutoDirEntry,
) -> Option<cmp::Ordering> {
    None
}

fn dir_first(
    f1: &structs::JoshutoDirEntry,
    f2: &structs::JoshutoDirEntry,
) -> Option<cmp::Ordering> {
    let f1_isdir = f1.path.is_dir();
    let f2_isdir = f2.path.is_dir();

    if f1_isdir && !f2_isdir {
        Some(cmp::Ordering::Less)
    } else if !f1_isdir && f2_isdir {
        Some(cmp::Ordering::Greater)
    } else {
        None
    }
}

const fn dummy_reverse(c: cmp::Ordering) -> cmp::Ordering {
    c
}

fn reverse_ordering(c: cmp::Ordering) -> cmp::Ordering {
    match c {
        cmp::Ordering::Less => cmp::Ordering::Greater,
        cmp::Ordering::Greater => cmp::Ordering::Less,
        x => x,
    }
}

fn natural_sort_case_insensitive(
    f1: &structs::JoshutoDirEntry,
    f2: &structs::JoshutoDirEntry,
) -> cmp::Ordering {
    let f1_name = f1.file_name_as_string.to_lowercase();
    let f2_name = f2.file_name_as_string.to_lowercase();
    if f1_name <= f2_name {
        cmp::Ordering::Less
    } else {
        cmp::Ordering::Greater
    }
}

fn natural_sort(f1: &structs::JoshutoDirEntry, f2: &structs::JoshutoDirEntry) -> cmp::Ordering {
    if f1.file_name <= f2.file_name {
        cmp::Ordering::Less
    } else {
        cmp::Ordering::Greater
    }
}

fn mtime_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)?;

        let f1_mtime: time::SystemTime = f1_meta.modified()?;
        let f2_mtime: time::SystemTime = f2_meta.modified()?;

        Ok(if f1_mtime >= f2_mtime {
            cmp::Ordering::Less
        } else {
            cmp::Ordering::Greater
        })
    }
    compare(&file1, &file2).unwrap_or(cmp::Ordering::Less)
}