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
350
351
352
353
354
355
356
357
358
#![forbid(unsafe_code)]
#![forbid(missing_docs)]
#![warn(clippy::all)]
#![deny(warnings)]
#![allow(clippy::needless_doctest_main)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]

#[cfg(feature = "clap-parse")]
pub mod clapers;
mod error;
#[cfg(feature = "http")]
mod http;
mod input;
mod output;
mod path;

pub use crate::error::Error;
pub use crate::error::Result;
pub use crate::input::CachedInput;
pub use crate::input::Input;
pub use crate::input::InputPath;
pub use crate::output::Output;
pub use crate::output::OutputPath;
pub use crate::path::ClioPath;

use std::ffi::OsStr;
use std::fs::Metadata;
use std::path::Path;

#[cfg(not(unix))]
fn is_fifo(_: &Metadata) -> bool {
    false
}

#[cfg(unix)]
fn is_fifo(metadata: &Metadata) -> bool {
    use std::os::unix::fs::FileTypeExt;
    metadata.file_type().is_fifo()
}

fn assert_exists(path: &Path) -> Result<()> {
    if !path.try_exists()? {
        return Err(Error::not_found_error());
    }
    Ok(())
}

#[cfg(not(unix))]
fn assert_readable(_path: &Path) -> Result<()> {
    Ok(())
}

#[cfg(unix)]
fn assert_readable(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    let permissions = path.metadata()?.permissions();
    if (permissions.mode() & 0o444) == 0 {
        return Err(Error::permission_error());
    }
    Ok(())
}

fn assert_writeable(path: &Path) -> Result<()> {
    let permissions = path.metadata()?.permissions();
    if permissions.readonly() {
        return Err(Error::permission_error());
    }
    Ok(())
}

fn assert_not_dir(path: &ClioPath) -> Result<()> {
    if path.try_exists()? {
        if path.is_dir() {
            return Err(Error::dir_error());
        }
        if path.ends_with_slash() {
            return Err(Error::not_dir_error());
        }
    }
    if path.ends_with_slash() {
        return Err(Error::not_found_error());
    }
    Ok(())
}

fn assert_is_dir(path: &Path) -> Result<()> {
    assert_exists(path)?;
    if !path.is_dir() {
        return Err(Error::not_dir_error());
    }
    Ok(())
}

/// A predicate builder for filtering files based on extension
///
/// ```no_run
/// use clio::{ClioPath, has_extension};
///
/// let dir = ClioPath::new("/tmp/foo")?;
/// for txt_file in dir.files(has_extension("txt"))? {
///     txt_file.open()?;
/// }
/// # Ok::<(), clio::Error>(())
/// ```
pub fn has_extension<S: AsRef<OsStr>>(ext: S) -> impl Fn(&ClioPath) -> bool {
    {
        move |path| path.extension() == Some(ext.as_ref())
    }
}

/// A predicate for filtering files that accepts any file
///
/// ```no_run
/// use clio::{ClioPath, any_file};
///
/// let dir = ClioPath::new("/tmp/foo")?;
/// for file in dir.files(any_file)? {
///     file.open()?;
/// }
/// # Ok::<(), clio::Error>(())
/// ```
pub fn any_file(_: &ClioPath) -> bool {
    true
}

#[cfg(test)]
#[cfg(feature = "clap-parse")]
/// Trait to throw compile errors if a type will not be supported by clap
trait Parseable: Clone + Sync + Send {}

macro_rules! impl_try_from {
    ($struct_name:ident) => {
        impl_try_from!($struct_name Base);
        impl_try_from!($struct_name Default);
        impl_try_from!($struct_name TryFrom<ClioPath>);

        #[cfg(feature = "clap-parse")]
        #[cfg_attr(docsrs, doc(cfg(feature = "clap-parse")))]
        /// Opens a new handle on the file from the path that was used to create it
        /// Probably a bad idea to have two write handles to the same file or to std in
        /// There is no effort done to make the clone be at the same position as the original
        ///
        /// This will panic if the file has been deleted
        ///
        /// Only included when using the `clap-parse` feature as it is needed for `value_parser`
        impl Clone for $struct_name {
            fn clone(&self) -> Self {
                $struct_name::new(self.path().clone()).unwrap()
            }
        }
    };
    (ClioPath: Clone) => {
        impl_try_from!(ClioPath Base);
        impl_try_from!(ClioPath Default);
    };
    ($struct_name:ident: Clone) => {
        impl_try_from!($struct_name Base);
        impl_try_from!($struct_name Default);
        impl_try_from!($struct_name TryFrom<ClioPath>);
    };
    ($struct_name:ident: Clone - Default) => {
        impl_try_from!($struct_name Base);
        impl_try_from!($struct_name TryFrom<ClioPath>);
    };
    ($struct_name:ident Default) => {
        impl Default for $struct_name {
            fn default() -> Self {
                $struct_name::std()
            }
        }

        #[cfg(test)]
        impl $struct_name {
            // Check that all clio types have the core methods
            #[allow(dead_code)]
            fn test_core_methods() {
                let s = crate::$struct_name::std();
                assert!(s.is_std());
                assert!(!s.is_local());
                s.is_tty();
                s.path();
            }
        }
    };
    ($struct_name:ident TryFrom<ClioPath>) => {
        impl TryFrom<ClioPath> for $struct_name {
            type Error = crate::Error;
            fn try_from(file_name: ClioPath) -> Result<Self> {
                $struct_name::new(file_name)
            }
        }
    };
    ($struct_name:ident Base) => {

        impl TryFrom<&OsStr> for $struct_name {
            type Error = crate::Error;
            fn try_from(file_name: &OsStr) -> Result<Self> {
                $struct_name::new(file_name)
            }
        }

        impl TryFrom<&std::ffi::OsString> for $struct_name {
            type Error = crate::Error;
            fn try_from(file_name: &std::ffi::OsString) -> Result<Self> {
                $struct_name::new(file_name)
            }
        }

        impl TryFrom<&std::path::PathBuf> for $struct_name {
            type Error = crate::Error;
            fn try_from(file_name: &std::path::PathBuf) -> Result<Self> {
                $struct_name::new(file_name)
            }
        }

        impl TryFrom<&std::path::Path> for $struct_name {
            type Error = crate::Error;
            fn try_from(file_name: &std::path::Path) -> Result<Self> {
                $struct_name::new(file_name)
            }
        }

        impl TryFrom<&String> for $struct_name {
            type Error = crate::Error;
            fn try_from(file_name: &String) -> Result<Self> {
                $struct_name::new(file_name)
            }
        }

        impl TryFrom<&str> for $struct_name {
            type Error = crate::Error;
            fn try_from(file_name: &str) -> Result<Self> {
                $struct_name::new(file_name)
            }
        }

        /// formats as the path it was created from
        impl Display for $struct_name {
            fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(fmt, "{:?}", self.path().as_os_str())
            }
        }

        #[cfg(feature = "clap-parse")]
        #[cfg_attr(docsrs, doc(cfg(feature = "clap-parse")))]
        impl clap::builder::ValueParserFactory for $struct_name {
            type Parser = crate::clapers::OsStrParser<$struct_name>;
            fn value_parser() -> Self::Parser {
                crate::clapers::OsStrParser::new()
            }
        }

        #[cfg(test)]
        #[cfg(feature = "clap-parse")]
        impl crate::Parseable for $struct_name {}
    };
}

pub(crate) use impl_try_from;

#[cfg(test)]
mod tests {
    use super::*;
    use std::{
        fs::{create_dir, set_permissions, write, File},
        io::Read,
    };
    use tempfile::{tempdir, TempDir};

    fn set_mode(path: &Path, mode: u32) -> Result<()> {
        let mut perms = path.metadata()?.permissions();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            perms.set_mode(mode);
        }
        #[cfg(not(unix))]
        {
            perms.set_readonly((mode & 0o222) == 0);
        }
        set_permissions(path, perms)?;
        Ok(())
    }

    fn temp() -> TempDir {
        let tmp = tempdir().expect("could not make tmp dir");
        create_dir(&tmp.path().join("dir")).expect("could not create dir");
        write(&tmp.path().join("file"), "contents").expect("could not create dir");
        let ro = tmp.path().join("ro");
        write(&ro, "contents").expect("could not create ro");
        set_mode(&ro, 0o400).expect("could make ro read only");
        let wo = tmp.path().join("wo");
        write(&wo, "contents").expect("could not create wo");
        set_mode(&wo, 0o200).expect("could make ro write only");
        tmp
    }

    macro_rules! assert_all_eq {
        ($path:ident, $a:ident, $($b:expr),+) => {
            let a = comparable($a);
            $(
                assert_eq!(
                    &a,
                    &comparable($b),
                    "mismatched error for path {:?} ({:?}) {}",
                    $path, Path::new($path).canonicalize(),
                    stringify!($a != $b)
                );
            )+
        };
    }

    #[test]
    fn test_path_err_match_real_err() {
        let tmp = temp();
        let tmp_w = temp();
        for path in [
            "file",
            "ro",
            "wo",
            "file/",
            "dir",
            "dir/",
            "missing-file",
            "missing-dir/",
            "missing-dir/file",
        ] {
            let tmp_path = tmp.path().join(path);
            let raw_r = File::open(&tmp_path).and_then(|mut f| {
                let mut s = String::new();
                f.read_to_string(&mut s)?;
                Ok(s)
            });
            let raw_w = write(&tmp_w.path().join(path), "junk");

            let in_path_err = InputPath::new(&tmp_path);
            let open_err = Input::new(&tmp_path);
            assert_all_eq!(path, raw_r, in_path_err, open_err);

            let out_path_err = OutputPath::new(&tmp_path);
            let create_err = Output::new(&tmp_path);
            assert_all_eq!(path, raw_w, out_path_err, create_err);
        }
    }

    #[cfg(any(target_os = "linux", target_os = "macos"))]
    fn comparable<E: std::fmt::Display, A>(
        a: std::result::Result<A, E>,
    ) -> std::result::Result<&'static str, String> {
        a.map(|_| "Ok").map_err(|e| e.to_string())
    }

    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
    fn comparable<E, A>(a: std::result::Result<A, E>) -> bool {
        a.is_ok()
    }
}