aboutsummaryrefslogtreecommitdiff
path: root/arg_picker/src/value/paths.rs
blob: 403d6cc197b56a125da92f711c5f54c7c3f9f1a9 (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
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
use std::{
    ops::{Deref, DerefMut},
    path::{Path, PathBuf},
};

/// A file path.
///
/// `FilePath` is a wrapper type around `PathBuf` representing an arbitrary file path.
///
/// It implements `Pickable` and can be parsed by a `Picker`.
///
/// # Parsing Behavior
///
/// At the moment of parsing, checks whether the path exists and is a file.
/// If not a file, returns `NotFound`.
#[non_exhaustive]
pub struct FilePath {
    path: PathBuf,
}

/// A file path that should not exist.
///
/// `NoFilePath` is a wrapper type around `PathBuf` representing an arbitrary path
/// that must not currently point to an existing file.
///
/// It implements `Pickable` and can be parsed by a `Picker`.
///
/// # Parsing Behavior
///
/// At the moment of parsing, checks whether no file exists at the given path.
/// If a file already exists (regardless of whether it is a regular file, directory,
/// symlink, or other type), returns `NotFound`.
#[non_exhaustive]
pub struct NoFilePath {
    path: PathBuf,
}

/// A directory path.
///
/// `DirPath` is a wrapper type around `PathBuf` representing an arbitrary existing
/// directory path.
///
/// It implements `Pickable` and can be parsed by a `Picker`.
///
/// # Parsing Behavior
///
/// At the moment of parsing, checks whether a directory exists at the given path.
/// If no directory exists, returns `NotFound`.
#[non_exhaustive]
pub struct DirPath {
    path: PathBuf,
}

/// A directory path that should not exist.
///
/// `NoDirPath` is a wrapper type around `PathBuf` representing an arbitrary path
/// that must not currently point to an existing directory.
///
/// It implements `Pickable` and can be parsed by a `Picker`.
///
/// # Parsing Behavior
///
/// At the moment of parsing, checks whether no directory exists at the given path.
/// If a directory already exists (regardless of whether it is a regular file, file,
/// symlink, or other type), returns `NotFound`.
#[non_exhaustive]
pub struct NoDirPath {
    path: PathBuf,
}

/// A symbolic link path.
///
/// `SymlinkPath` is a wrapper type around `PathBuf` representing an existing symbolic link.
///
/// It implements `Pickable` and can be parsed by a `Picker`.
///
/// # Parsing Behavior
///
/// At the moment of parsing, checks whether the path exists and is a symlink.
/// If not a symlink, returns `NotFound`.
#[non_exhaustive]
pub struct SymlinkPath {
    path: PathBuf,
}

/// A symbolic link path that should not exist.
///
/// `NoSymlinkPath` is a wrapper type around `PathBuf` representing an arbitrary path
/// that must not currently point to an existing symbolic link.
///
/// It implements `Pickable` and can be parsed by a `Picker`.
///
/// # Parsing Behavior
///
/// At the moment of parsing, checks whether no symlink exists at the given path.
/// If a symlink already exists (regardless of whether it points to a file, directory,
/// or other type), returns `NotFound`.
#[non_exhaustive]
pub struct NoSymlinkPath {
    path: PathBuf,
}

/// A path that should not exist at all.
///
/// `NoPath` is a wrapper type around `PathBuf` representing an arbitrary path
/// that must not currently exist on the filesystem (as a file, directory, symlink,
/// or any other type).
///
/// It implements `Pickable` and can be parsed by a `Picker`.
///
/// # Parsing Behavior
///
/// At the moment of parsing, checks whether any filesystem entry exists at the given path.
/// If anything exists at that path, returns `NotFound`.
#[non_exhaustive]
pub struct NoPath {
    path: PathBuf,
}

/// Implements common trait impls (From, AsRef, Deref, DerefMut) for a path wrapper type.
macro_rules! impl_path_traits {
    ($type:ident) => {
        impl From<PathBuf> for $type {
            fn from(value: PathBuf) -> Self {
                $type { path: value }
            }
        }

        impl From<&PathBuf> for $type {
            fn from(value: &PathBuf) -> Self {
                $type {
                    path: value.clone(),
                }
            }
        }

        impl AsRef<Path> for $type {
            fn as_ref(&self) -> &Path {
                &self.path
            }
        }

        impl DerefMut for $type {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.path
            }
        }

        impl Deref for $type {
            type Target = PathBuf;

            fn deref(&self) -> &Self::Target {
                &self.path
            }
        }

        impl From<$type> for PathBuf {
            fn from(value: $type) -> Self {
                value.path
            }
        }

        impl From<&$type> for PathBuf {
            fn from(value: &$type) -> Self {
                value.path.clone()
            }
        }
    };
}

impl_path_traits!(FilePath);
impl_path_traits!(NoFilePath);
impl_path_traits!(DirPath);
impl_path_traits!(NoDirPath);
impl_path_traits!(SymlinkPath);
impl_path_traits!(NoSymlinkPath);
impl_path_traits!(NoPath);

/// Recursive file paths.
///
/// `RecursiveFiles` is a wrapper type around `Vec<PathBuf>` representing a list of
/// existing files.
///
/// It implements `Pickable` and can be parsed by a `Picker`.
///
/// # Parsing Behavior
///
/// - If a file path is given, returns a list of length 1 containing that file.
/// - If a directory path is given, recursively collects all files under that directory
///   and returns them as a list.
#[non_exhaustive]
pub struct RecursiveFiles {
    paths: Vec<PathBuf>,
}

impl From<Vec<PathBuf>> for RecursiveFiles {
    fn from(paths: Vec<PathBuf>) -> Self {
        Self { paths }
    }
}

impl From<RecursiveFiles> for Vec<PathBuf> {
    fn from(value: RecursiveFiles) -> Self {
        value.paths
    }
}

impl AsRef<[PathBuf]> for RecursiveFiles {
    fn as_ref(&self) -> &[PathBuf] {
        &self.paths
    }
}

impl Deref for RecursiveFiles {
    type Target = Vec<PathBuf>;

    fn deref(&self) -> &Self::Target {
        &self.paths
    }
}

impl DerefMut for RecursiveFiles {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.paths
    }
}

impl RecursiveFiles {
    /// Returns the number of file paths.
    pub fn len(&self) -> usize {
        self.paths.len()
    }

    /// Returns `true` if there are no file paths.
    pub fn is_empty(&self) -> bool {
        self.paths.is_empty()
    }

    /// Returns an iterator over the file paths.
    pub fn iter(&self) -> std::slice::Iter<'_, PathBuf> {
        self.paths.iter()
    }
}

impl From<Vec<RecursiveFiles>> for RecursiveFiles {
    fn from(value: Vec<RecursiveFiles>) -> Self {
        Self {
            paths: value.into_iter().flat_map(|r| r.paths).collect(),
        }
    }
}

/// Trait for types that can be combined into a single `RecursiveFiles`.
pub trait IntoRecursiveFiles {
    /// Combines multiple sources of file paths into a single `RecursiveFiles`.
    fn combine(self) -> RecursiveFiles;
}

impl<T> IntoRecursiveFiles for Vec<T>
where
    T: Into<RecursiveFiles>,
{
    fn combine(self) -> RecursiveFiles {
        self.into_iter()
            .map(Into::into)
            .collect::<Vec<RecursiveFiles>>()
            .into()
    }
}

impl<T> IntoRecursiveFiles for &[T]
where
    T: Into<RecursiveFiles> + Clone,
{
    fn combine(self) -> RecursiveFiles {
        self.iter()
            .cloned()
            .map(Into::into)
            .collect::<Vec<RecursiveFiles>>()
            .into()
    }
}

impl<T, const N: usize> IntoRecursiveFiles for [T; N]
where
    T: Into<RecursiveFiles>,
{
    fn combine(self) -> RecursiveFiles {
        self.into_iter()
            .map(Into::into)
            .collect::<Vec<RecursiveFiles>>()
            .into()
    }
}