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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*! 
`lrg` is a library for find the largest (or smallest) files in a given directory.
There is also support for searching by a custom function, given [`DirEntry`]'s.
In addition to this, you can specify (in [`LrgOptions`]) the minimum depth and maximum depth to search for,
such as if you wanted to prevent recursion. You can also speficy whether to follow links or to include
directories.

Note: [`DirEntry`] is a typedef of [`walkdir::DirEntry`]

## Examples
To find the largest files in a directory:
```
use std::path::Path;
use lrg::{Lrg, LrgOptions, DirEntry, SortBy};
// Get a path to some directory (or file)
let path = Path::new("./some/path");
// Create the Lrg object to store the entries
let mut lrg: Lrg = Lrg::new(path, &LrgOptions::default());
// Sort and get the entries
let mut entries: Vec<DirEntry> = lrg.sort_by(&SortBy::Descending).get_entries();
// You can also call `sort_descending`
entries = lrg.sort_descending().get_entries();
// These calls mutate the underlying struct, so calling:
entries = lrg.get_entries();
// Will give you the same as the call before it
```

To find the smallest files in a directory:
```
# use std::path::Path;
# use lrg::{Lrg, LrgOptions, DirEntry};
let path = Path::new("./some/other/path");
let mut lrg: Lrg = Lrg::new(path, &LrgOptions::default());
let entries: Vec<DirEntry> = lrg.sort_ascending().get_entries();
```

To search using a custom function:
```
# use std::path::Path;
# use lrg::{Lrg, LrgOptions, DirEntry};
let path = Path::new("./another/path");
let mut lrg: Lrg = Lrg::new(path, &LrgOptions::default());
// Sort by filename (note: not the full path)
lrg.sort_by_custom(|a: &DirEntry, b: &DirEntry| {
    a.file_name().cmp(b.file_name())
});
let entries: Vec<DirEntry> = lrg.get_entries();
```

[`Lrg`]: struct.Lrg.html
[`DirEntry`]: struct.DirEntry.html
[`LrgOptions`]: struct.LrgOptions.html
[`walkdir::DirEntry`]: https://docs.rs/walkdir/2.2.7/walkdir/struct.DirEntry.html
*/

use std::cmp::Ordering;
use std::io::ErrorKind;
use std::path::Path;

use log::warn;
use walkdir::WalkDir;

/// Specifies the sorting algorithm.
pub enum SortBy {
    /// Sorts by filesize ascending
    Ascending,
    /// Sorts by filesize descending
    Descending,
}

/// Options when constructing an `Lrg` struct.
///
/// # Examples
/// Can be constructed like normal:
/// ```
/// # use lrg::LrgOptions;
/// let opts = LrgOptions {
///     min_depth: 1,
///     max_depth: 5,
///     follow_links: false,
///     include_dirs: true,
/// };
/// ```
/// Or can also inherit [`default options`]:
/// ```
/// # use lrg::LrgOptions;
/// let opts = LrgOptions {
///     min_depth: 5,
///     max_depth: 10,
///     ..LrgOptions::default()
/// };
/// ```
///
/// [`default options`]: struct.LrgOptions.html#method.default
#[derive(Clone, Debug)]
pub struct LrgOptions {
    /// Specifies them minimum depth for searching
    /// Minimum depth is the depth at which to start searching
    pub min_depth: usize,
    // Specifies the maximum depth for searching
    // Maximum depth is the depth at which to stop searching
    pub max_depth: usize,
    /// Specifies whether to follow links while searching
    pub follow_links: bool,
    /// Speicifies whether to include directories in the search
    pub include_dirs: bool,
}

/// Implements default options
impl Default for LrgOptions {
    /// The default function.
    ///
    /// # Examples
    /// ```
    /// use lrg::LrgOptions;
    /// // Gives options that recurse as far as possible, don't follow links,
    /// // and don't include directories.
    /// let options = LrgOptions::default();
    /// ```
    fn default() -> LrgOptions {
        LrgOptions {
            min_depth: 0,
            max_depth: std::usize::MAX,
            follow_links: false,
            include_dirs: false,
        }
    }
}

/// A type copy of the [`walkdir::DirEntry`] struct.
///
/// [`walkdir::DirEntry`]: https://docs.rs/walkdir/2.2.7/walkdir/struct.DirEntry.html
pub type DirEntry = walkdir::DirEntry;

/// The main struct for searching for files by size.
/// Constructed using [`new`], passing in a path and options.
///
/// [`new`]: struct.Lrg.html#method.new
#[derive(Clone, Debug)]
pub struct Lrg {
    entries: Vec<DirEntry>,
}

impl Lrg {
    /// Creates a new Lrg with options and at the given path.
    ///
    /// # Examples
    /// ```
    /// # use std::path::Path;
    /// # use lrg::{Lrg, LrgOptions};
    /// let path = Path::new(".");
    /// let lrg = Lrg::new(path, &LrgOptions::default());
    /// ```
    /// To use custom options, just supply a [`LrgOptions`] struct.
    ///
    /// To only search the base directoy, using the other default options:
    /// ```
    /// # use std::path::Path;
    /// # use lrg::LrgOptions;
    /// let path = Path::new(".");
    /// let opts = LrgOptions {
    ///     min_depth: 1,
    ///     ..LrgOptions::default()
    /// };
    /// ```
    ///
    /// [`LrgOptions`]: struct.LrgOptions.html
    pub fn new(path: &Path, options: &LrgOptions) -> Self {
        let mut entries: Vec<DirEntry> = Vec::new();

        // Walk directory recursivley (prints debug messages if error)
        for entry in WalkDir::new(&path)
            .min_depth(options.min_depth)
            .max_depth(options.max_depth)
            .follow_links(options.follow_links)
        {
            match entry {
                // Entry can be found
                Ok(entry) => {
                    if entry.file_type().is_dir() && options.include_dirs {
                        entries.push(entry.to_owned())
                    } else if entry.file_type().is_file() || entry.file_type().is_symlink() {
                        entries.push(entry.to_owned());
                    }
                }
                Err(err) => {
                    let path = err.path().unwrap_or_else(|| Path::new("")).display();
                    let error_message = get_walkdir_error_str(&err);
                    println!("lrg: error opening '{}': {}", path, error_message);
                }
            }
        }

        Lrg { entries }
    }

    /// Sorts the lrg object entries, and returns the lrg object.
    ///
    /// # Examples
    /// To get the largest files first:
    /// ```
    /// # use std::path::Path;
    /// # use lrg::{Lrg, LrgOptions, SortBy};
    /// let path = Path::new(".");
    /// let mut lrg = Lrg::new(path, &LrgOptions::default());
    /// lrg.sort_by(&SortBy::Descending);
    /// ```
    pub fn sort_by(&mut self, cmp: &SortBy) -> &Self {
        match cmp {
            SortBy::Ascending => self.sort_ascending(),
            SortBy::Descending => self.sort_descending(),
        }
    }

    /// Sorts the lrg object entries by a custom sort function, and returns the lrg object.
    ///
    /// # Examples
    /// To search by creation date:
    /// ```
    /// # use std::path::{Path, PathBuf};
    /// # use std::ffi::OsStr;
    /// # use lrg::{Lrg, LrgOptions, DirEntry};
    /// let path = Path::new("./another/path");
    /// let mut lrg = Lrg::new(path, &LrgOptions::default());
    /// lrg.sort_by_custom(|a: &DirEntry, b: &DirEntry| {
    ///     // Create custom function to get creation date of a `DirEntry`
    ///     let creation_date = |x: &DirEntry| {
    ///         match x.metadata() {
    ///             Ok(meta) => {
    ///                 match meta.created() {
    ///                     Ok(created) => created,
    ///                     Err(_) => std::time::SystemTime::UNIX_EPOCH,
    ///                 }
    ///             },
    ///             // Default to UNIX epoch
    ///             Err(_) => std::time::SystemTime::UNIX_EPOCH,
    ///         }
    ///     };
    ///     //Make comparison
    ///     creation_date(a).cmp(&creation_date(b))
    /// });
    /// // Get entries
    /// let entries: Vec<DirEntry> = lrg.get_entries();
    /// ```
    pub fn sort_by_custom<F>(&mut self, cmp: F) -> &Self
    where
        F: FnMut(&DirEntry, &DirEntry) -> Ordering,
    {
        self.entries.sort_unstable_by(cmp);
        self
    }

    /// Sorts the lrg object entries by ascending file size, and returns the lrg object.
    ///
    /// # Examples
    /// ```
    /// # use std::path::Path;
    /// # use lrg::{Lrg, LrgOptions};
    /// let path = Path::new("./another/path");
    /// let mut lrg: Lrg = Lrg::new(path, &LrgOptions::default());
    /// let entries = lrg.sort_ascending().get_entries();
    /// ```
    pub fn sort_ascending(&mut self) -> &Self {
        self.entries.sort_unstable_by(|a: &DirEntry, b: &DirEntry| {
            Self::get_size(a).cmp(&Self::get_size(b))
        });
        self
    }

    /// Sorts the lrg object entries by descending file size, and returns the lrg object.
    ///
    /// # Examples
    /// ```
    /// # use std::path::Path;
    /// # use lrg::{Lrg, LrgOptions};
    /// let path = Path::new("./another/path");
    /// let mut lrg: Lrg = Lrg::new(path, &LrgOptions::default());
    /// let entries = lrg.sort_descending().get_entries();
    /// ```
    pub fn sort_descending(&mut self) -> &Self {
        self.entries.sort_unstable_by(|a: &DirEntry, b: &DirEntry| {
            Self::get_size(b).cmp(&Self::get_size(a))
        });
        self
    }

    fn get_size(entry: &DirEntry) -> u64 {
        match entry.metadata() {
            Ok(meta) => meta.len(),
            Err(err) => {
                warn!(
                    "Couldn't get metadata for {}: {:?}",
                    entry.path().display(),
                    err
                );
                0
            }
        }
    }

    /// Gets the entries from the [`Lrg`] object.
    ///
    /// # Examples
    /// ```
    /// # use std::path::Path;
    /// # use lrg::{Lrg, LrgOptions};
    /// let path = Path::new("./another/path");
    /// let mut lrg: Lrg = Lrg::new(path, &LrgOptions::default());
    /// let entries = lrg.sort_ascending().get_entries();
    /// ```
    /// [`Lrg`]: struct.Lrg.html
    pub fn get_entries(&self) -> Vec<DirEntry> {
        self.entries.clone()
    }
}

/// This function gets a string for a walkdir error.
/// This is needed since `io_error.to_str()` is not public.
pub fn get_walkdir_error_str(err: &walkdir::Error) -> String {
    match err.io_error() {
        Some(ioerr) => {
            // Because ErrorKind.as_str() is private, if someone finds a fix, send a pr
            match ioerr.kind() {
                ErrorKind::NotFound => "Entity not found".to_owned(),
                ErrorKind::PermissionDenied => "Permission denied".to_owned(),
                ErrorKind::ConnectionRefused => "Connection refused".to_owned(),
                ErrorKind::ConnectionReset => "Connection reset".to_owned(),
                ErrorKind::ConnectionAborted => "Connection aborted".to_owned(),
                ErrorKind::NotConnected => "Not connected".to_owned(),
                ErrorKind::AddrInUse => "Address in use".to_owned(),
                ErrorKind::AddrNotAvailable => "Address not available".to_owned(),
                ErrorKind::BrokenPipe => "Broken pipe".to_owned(),
                ErrorKind::AlreadyExists => "Entity already exists".to_owned(),
                ErrorKind::WouldBlock => "Operation would block".to_owned(),
                ErrorKind::InvalidInput => "Invalid input parameter".to_owned(),
                ErrorKind::InvalidData => "Invalid data".to_owned(),
                ErrorKind::TimedOut => "Timed out".to_owned(),
                ErrorKind::WriteZero => "Write zero".to_owned(),
                ErrorKind::Interrupted => "Operation interrupted".to_owned(),
                ErrorKind::Other => "Other os error".to_owned(),
                ErrorKind::UnexpectedEof => "Unexpected end of file".to_owned(),
                _ => "Unknown error".to_owned(),
            }
        }
        None => "Unknown error".to_owned(),
    }
}

// Tests are located in <PROJECT_ROOT>/tests folder