summaryrefslogtreecommitdiffstats
path: root/src/sort.rs
blob: cbfc6352d2c06d27fe54cd405d1f7896a9f1a557 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use std::cmp;
use std::fs;
use std::time;

use crate::structs;

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

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

impl SortType {
    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 {
                    SortNatural::dir_first_case_insensitive
                } else if ss.directories_first && ss.case_sensitive && !ss.reverse {
                    SortNatural::dir_first
                } else {
                    SortNatural::default_sort
                }
            }
            SortType::SortMtime(ref ss) => {
                if ss.directories_first && !ss.reverse {
                    SortMtime::dir_first
                } else {
                    SortMtime::default_sort
                }
            }
        }
    }

    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 {
                    filter_default
                } else {
                    filter_hidden_files
                }
            }
            SortType::SortMtime(ref ss) => {
                if ss.show_hidden {
                    filter_default
                } else {
                    filter_hidden_files
                }
            }
        }
    }

    pub fn show_hidden(&self) -> bool {
        match *self {
            SortType::SortNatural(ref ss) => ss.show_hidden,
            SortType::SortMtime(ref ss) => ss.show_hidden,
        }
    }

    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> {
    match result {
        Ok(direntry) => match structs::JoshutoDirEntry::from(&direntry) {
            Ok(s) => Some(s),
            Err(e) => {
                eprintln!("error: {:?}", e);
                None
            }
        },
        Err(_) => None,
    }
}

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

pub struct SortNatural {}
impl SortNatural {
    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();

        if f1_isdir && !f2_isdir {
            cmp::Ordering::Less
        } else if !f1_isdir && f2_isdir {
            cmp::Ordering::Greater
        } else {
            let f1_name = file1.file_name_as_string.to_lowercase();
            let f2_name = file2.file_name_as_string.to_lowercase();
            if f1_name <= f2_name {
                cmp::Ordering::Less
            } else {
                cmp::Ordering::Greater
            }
        }
    }

    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();

        if f1_isdir && !f2_isdir {
            cmp::Ordering::Less
        } else if !f1_isdir && f2_isdir {
            cmp::Ordering::Greater
        } else {
            Self::default_sort(file1, file2)
        }
    }

    pub fn default_sort(
        file1: &structs::JoshutoDirEntry,
        file2: &structs::JoshutoDirEntry,
    ) -> cmp::Ordering {
        if file1.file_name <= file2.file_name {
            cmp::Ordering::Less
        } else {
            cmp::Ordering::Greater
        }
    }
}

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> {
            let f1_isdir = file1.path.is_dir();
            let f2_isdir = file2.path.is_dir();

            if f1_isdir && !f2_isdir {
                Ok(cmp::Ordering::Less)
            } else if !f1_isdir && f2_isdir {
                Ok(cmp::Ordering::Greater)
            } else {
                Ok(SortMtime::default_sort(file1, file2))
            }
        }
        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> {
            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)
    }
}