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
use std::path::{Path, PathBuf};
use std::fs::{remove_dir_all, read_dir};
use std::io::Result as IoResult;

/// Returns true if `path` exists and is a directory.
///
/// ```rust,no_run
/// if latin::directory::exists("/tmp") {
///     // do stuff
/// }
pub fn exists<P: AsRef<Path>>(path: P) -> bool {
    let path = path.as_ref();
    path.is_dir() && path.exists()
}

/// Returns an iterator of the children in `path`.
///
/// ```rust,no_run
/// for child in latin::directory::children("./").unwrap() {
///     println!("{:?}", child);
/// }
pub fn children<P: AsRef<Path>>(path: P) -> IoResult<::std::vec::IntoIter<PathBuf>> {
    let mut out = vec![];
    for entry in try!(read_dir(path)) {
        let entry = try!(entry);
        out.push(entry.path());
    }
    Ok(out.into_iter())
}

/// Returns an iterator of the child files in `path`.
///
/// ```rust,no_run
/// for file in latin::directory::files("./").unwrap() {
///     for (i, line) in latin::file::read_lines(file).unwrap().enumerate() {
///         println!("{} {}", i, line.unwrap());
///     }
/// }
/// ```
pub fn files<P: AsRef<Path>>(path: P) -> IoResult<::std::vec::IntoIter<PathBuf>> {
    let mut out = vec![];
    for entry in try!(read_dir(path)) {
        let entry = try!(entry);

        // TODO: also check symlinks
        if try!(entry.file_type()).is_file() { 
            out.push(entry.path());
        }
    }
    Ok(out.into_iter())
}

/// Returns a list of all the subdirectories in `path`.
///
/// ```rust,no_run
/// for subdir in latin::directory::sub_directories("./").unwrap() {
///     let children_count = latin::directory::children(subdir).unwrap().count();
///     println!("{:?}: {}", subdir, children_count);
/// }
pub fn sub_directories<P: AsRef<Path>>(path: P) -> IoResult<::std::vec::IntoIter<PathBuf>> {
    let mut out = vec![];
    for entry in try!(read_dir(path)) {
        let entry = try!(entry);

        // TODO: also check symlinks
        if try!(entry.file_type()).is_dir() {
            out.push(entry.path());
        }
    }
    Ok(out.into_iter())
}

/// Removes the directory and all containing files and directories.
///
/// ```rust,no_run
/// latin::directory::remove("/tmp/foobar");
/// ```
pub fn remove<P: AsRef<Path>>(path: P) -> IoResult<()> {
    remove_dir_all(path)
}