aboutsummaryrefslogtreecommitdiff
path: root/arg_picker/src/builtin/pick_paths.rs
blob: 8e6c0fd5d602a0c1164ac8a264b1a37bd7b152d1 (plain) (blame)
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
use std::{
    fs,
    path::{Path, PathBuf},
};

use crate::{
    PickerArgResult::{self, NotFound, Parsed, Unparsed},
    SinglePickable,
    value::{
        DirPath, FilePath, NoDirPath, NoFilePath, NoPath, NoSymlinkPath, RecursiveFiles,
        SymlinkPath,
    },
};

impl SinglePickable for FilePath {
    fn pick_single(str: Option<&str>) -> PickerArgResult<Self> {
        match <PathBuf as SinglePickable>::pick_single(str) {
            Parsed(path) => {
                if path.exists() && path.is_file() {
                    Parsed(FilePath::from(path))
                } else {
                    NotFound
                }
            }
            Unparsed => Unparsed,
            NotFound => NotFound,
        }
    }
}

impl SinglePickable for NoFilePath {
    fn pick_single(str: Option<&str>) -> PickerArgResult<Self> {
        match <PathBuf as SinglePickable>::pick_single(str) {
            Parsed(path) => {
                if !path.exists() || !path.is_file() {
                    Parsed(NoFilePath::from(path))
                } else {
                    NotFound
                }
            }
            Unparsed => Unparsed,
            NotFound => NotFound,
        }
    }
}

impl SinglePickable for DirPath {
    fn pick_single(str: Option<&str>) -> PickerArgResult<Self> {
        match <PathBuf as SinglePickable>::pick_single(str) {
            Parsed(path) => {
                if path.exists() && path.is_dir() {
                    Parsed(DirPath::from(path))
                } else {
                    NotFound
                }
            }
            Unparsed => Unparsed,
            NotFound => NotFound,
        }
    }
}

impl SinglePickable for NoDirPath {
    fn pick_single(str: Option<&str>) -> PickerArgResult<Self> {
        match <PathBuf as SinglePickable>::pick_single(str) {
            Parsed(path) => {
                if !path.exists() || !path.is_dir() {
                    Parsed(NoDirPath::from(path))
                } else {
                    NotFound
                }
            }
            Unparsed => Unparsed,
            NotFound => NotFound,
        }
    }
}

impl SinglePickable for SymlinkPath {
    fn pick_single(str: Option<&str>) -> PickerArgResult<Self> {
        match <PathBuf as SinglePickable>::pick_single(str) {
            Parsed(path) => {
                if path.exists() && path.is_symlink() {
                    Parsed(SymlinkPath::from(path))
                } else {
                    NotFound
                }
            }
            Unparsed => Unparsed,
            NotFound => NotFound,
        }
    }
}

impl SinglePickable for NoSymlinkPath {
    fn pick_single(str: Option<&str>) -> PickerArgResult<Self> {
        match <PathBuf as SinglePickable>::pick_single(str) {
            Parsed(path) => {
                if !path.exists() || !path.is_symlink() {
                    Parsed(NoSymlinkPath::from(path))
                } else {
                    NotFound
                }
            }
            Unparsed => Unparsed,
            NotFound => NotFound,
        }
    }
}

impl SinglePickable for NoPath {
    fn pick_single(str: Option<&str>) -> PickerArgResult<Self> {
        match <PathBuf as SinglePickable>::pick_single(str) {
            Parsed(path) => {
                if !path.exists() {
                    Parsed(NoPath::from(path))
                } else {
                    NotFound
                }
            }
            Unparsed => Unparsed,
            NotFound => NotFound,
        }
    }
}

impl SinglePickable for RecursiveFiles {
    fn pick_single(str: Option<&str>) -> PickerArgResult<Self> {
        match <PathBuf as SinglePickable>::pick_single(str) {
            Parsed(path) => {
                if !path.exists() {
                    return NotFound;
                }
                if path.is_file() || path.is_symlink() {
                    return Parsed(RecursiveFiles::from(vec![path]));
                }
                let mut entries = Vec::new();
                if let Ok(dir_entries) = fs::read_dir(&path) {
                    for entry in dir_entries.flatten() {
                        let entry_path = entry.path();
                        if entry_path.is_file() || entry_path.is_symlink() {
                            entries.push(entry_path);
                        } else if entry_path.is_dir() {
                            collect_files(&entry_path, &mut entries);
                        }
                    }
                }
                Parsed(RecursiveFiles::from(entries))
            }
            Unparsed => Unparsed,
            NotFound => NotFound,
        }
    }
}

fn collect_files(dir: &Path, entries: &mut Vec<PathBuf>) {
    if let Ok(dir_entries) = fs::read_dir(dir) {
        for entry in dir_entries.flatten() {
            let entry_path = entry.path();
            if entry_path.is_file() || entry_path.is_symlink() {
                entries.push(entry_path);
            } else if entry_path.is_dir() {
                collect_files(&entry_path, entries);
            }
        }
    }
}