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
359
360
361
362
363
364
365
366
367
368
369
370
371
use crate::error::seek_error;
#[cfg(feature = "http")]
use crate::http::{is_http, try_to_url, HttpReader};
use crate::{impl_try_from, is_fifo, Error, Result};
use std::convert::TryFrom;
use std::ffi::{OsStr, OsString};
use std::fmt::{self, Debug, Display};
use std::fs::File;
use std::io::{self, BufRead, BufReader, Cursor, ErrorKind, Read, Result as IoResult, Seek, Stdin};
use std::path::Path;

/// An enum that represents a command line input stream,
/// either [`Stdin`] or [`File`]
///
/// It is designed to be used with the [`clap` crate](https://docs.rs/clap/latest) when taking a file name as an
/// argument to CLI app
/// ```
/// use clap::Parser;
/// use clio::Input;
///
/// #[derive(Parser)]
/// struct Opt {
///     /// path to file, use '-' for stdin
///     #[clap(value_parser)]
///     input_file: Input,
/// }
/// ```
#[derive(Debug)]
pub enum Input {
    /// a [`Stdin`] when the path was `-`
    Stdin(Stdin),
    /// a [`File`] represeinting the named pipe e.g. if called with `<(cat /dev/null)`
    Pipe(OsString, File),
    /// a normal [`File`] opened from the path
    File(OsString, File),
    #[cfg(feature = "http")]
    /// a reader that will download response from the HTTP server
    Http(OsString, HttpReader),
}

impl Input {
    /// Contructs a new input either by opening the file or for '-' returning stdin
    pub fn new<S: AsRef<OsStr>>(path: S) -> Result<Self> {
        let path = path.as_ref();
        if path == "-" {
            Ok(Self::std())
        } else {
            #[cfg(feature = "http")]
            if is_http(path) {
                let url = try_to_url(path)?;
                let reader = HttpReader::new(&url)?;
                return Ok(Input::Http(path.to_os_string(), reader));
            }
            let file = File::open(path)?;
            if is_fifo(&file)? {
                Ok(Input::Pipe(path.to_os_string(), file))
            } else {
                Ok(Input::File(path.to_os_string(), file))
            }
        }
    }

    /// Contructs a new input for stdin
    pub fn std() -> Self {
        Input::Stdin(io::stdin())
    }

    /// Contructs a new input either by opening the file or for '-' returning stdin
    ///
    /// The error is converted to a [`OsString`](std::ffi::OsString) so that [stuctopt](https://docs.rs/structopt/latest/structopt/#custom-string-parsers) can show it to the user.
    ///
    /// It is recomended that you use [`TryFrom::try_from`] and [clap 3.0](https://docs.rs/clap/latest/clap/index.html) instead.
    pub fn try_from_os_str(path: &OsStr) -> std::result::Result<Self, std::ffi::OsString> {
        TryFrom::try_from(path).map_err(|e: Error| e.to_os_string(path))
    }

    /// If input is a file, returns the size of the file, in bytes
    /// otherwise if input is stdin returns none.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let file = clio::Input::new("foo.txt").unwrap();
    ///
    /// assert_eq!(Some(3), file.len());
    /// ```
    pub fn len(&self) -> Option<u64> {
        match self {
            Input::Stdin(_) => None,
            Input::Pipe(_, _) => None,
            Input::File(_, file) => file.metadata().ok().map(|x| x.len()),
            #[cfg(feature = "http")]
            Input::Http(_, http) => http.len(),
        }
    }

    /// Returns a boolean saying if the file is empty, if using stdin returns None
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let file = clio::Input::new("foo.txt").unwrap();
    ///
    /// assert_eq!(Some(true), file.is_empty());
    /// ```
    pub fn is_empty(&self) -> Option<bool> {
        self.len().map(|l| l == 0)
    }

    /// If the input is std in [locks](std::io::Stdin::lock) it, otherwise wraps the file in a buffered reader.
    /// This is useful to get the line iterator of the [`BufRead`](std::io::BufRead).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::BufRead;
    /// # fn main() -> Result<(), clio::Error> {
    /// let mut file = clio::Input::new("-")?;
    ///
    /// for line in file.lock().lines() {
    ///   println!("line is: {}", line?);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn lock<'a>(&'a mut self) -> Box<dyn BufRead + 'a> {
        match self {
            Input::Stdin(stdin) => Box::new(stdin.lock()),
            Input::Pipe(_, pipe) => Box::new(BufReader::new(pipe)),
            Input::File(_, file) => Box::new(BufReader::new(file)),
            #[cfg(feature = "http")]
            Input::Http(_, http) => Box::new(BufReader::new(http)),
        }
    }

    /// Returns the path/url used to create the input
    pub fn path(&self) -> &OsStr {
        match self {
            Input::Stdin(_) => "-".as_ref(),
            Input::Pipe(path, _) => path,
            Input::File(path, _) => path,
            #[cfg(feature = "http")]
            Input::Http(url, _) => url,
        }
    }
}

impl_try_from!(Input);

impl Read for Input {
    fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
        match self {
            Input::Stdin(stdin) => stdin.read(buf),
            Input::Pipe(_, pipe) => pipe.read(buf),
            Input::File(_, file) => file.read(buf),
            #[cfg(feature = "http")]
            Input::Http(_, reader) => reader.read(buf),
        }
    }
}

impl Seek for Input {
    fn seek(&mut self, pos: io::SeekFrom) -> IoResult<u64> {
        match self {
            Input::Pipe(_, pipe) => pipe.seek(pos),
            Input::File(_, file) => file.seek(pos),
            _ => Err(seek_error()),
        }
    }
}

/// A struct that contains all the connents of a command line input stream,
/// either std in or a file
///
/// It is designed to be used with the [`clap` crate](https://docs.rs/clap/latest) when taking a file name as an
/// argument to CLI app
/// ```
/// use clap::Parser;
/// use clio::CachedInput;
///
/// #[derive(Parser)]
/// struct Opt {
///     /// path to file, use '-' for stdin
///     #[clap(value_parser)]
///     input_file: CachedInput,
/// }
/// ```
#[derive(Debug, Clone)]
pub struct CachedInput {
    path: OsString,
    data: Cursor<Vec<u8>>,
}

impl CachedInput {
    /// Reads all the data from an file (stdin for "-") into memmory and stores it in a new CachedInput.
    ///
    /// Useful if you want to use the input twice (see [reset](Self::reset)), or
    /// need to know the size.
    pub fn new<S: AsRef<OsStr>>(path: S) -> Result<Self> {
        let mut source = Input::new(path)?;
        let path = source.path().to_os_string();
        let capacity = source.len().unwrap_or(4096) as usize;
        let mut data = Cursor::new(Vec::with_capacity(capacity));
        io::copy(&mut source, &mut data)?;
        data.set_position(0);
        Ok(CachedInput { path, data })
    }

    /// Reads all the data from stdin into memmory and stores it in a new CachedInput.
    ///
    /// This will block until std in is closed.
    pub fn std() -> Result<Self> {
        Self::new("-")
    }

    /// Contructs a new [`CachedInput`] either by opening the file or for '-' stdin and reading
    /// all the data into memory.
    ///
    /// The error is converted to a [`OsString`](std::ffi::OsString) so that [stuctopt](https://docs.rs/structopt/latest/structopt/#custom-string-parsers) can show it to the user.
    ///
    /// It is recomended that you use [`TryFrom::try_from`] and [clap 3.0](https://docs.rs/clap/latest/clap/index.html) instead.
    pub fn try_from_os_str(path: &OsStr) -> std::result::Result<Self, std::ffi::OsString> {
        TryFrom::try_from(path).map_err(|e: Error| e.to_os_string(path))
    }

    /// Returns the size of the file in bytes.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let file = clio::CachedInput::try_from_os_str("foo.txt".as_ref()).unwrap();
    ///
    /// assert_eq!(3, file.len());
    /// ```
    pub fn len(&self) -> u64 {
        self.data.get_ref().len() as u64
    }

    /// Returns a boolean saying if the file is empty
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let file = clio::CachedInput::try_from_os_str("foo.txt".as_ref()).unwrap();
    ///
    /// assert_eq!(true, file.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.data.get_ref().is_empty()
    }

    /// Returns the path/url used to create the input
    pub fn path(&self) -> &OsStr {
        &self.path
    }

    /// Resets the reader back to the start of the file
    pub fn reset(&mut self) {
        self.data.set_position(0)
    }

    /// Returns data from the input as a Vec<u8>
    pub fn into_vec(self) -> Vec<u8> {
        self.data.into_inner()
    }

    /// Returns reference to the data from the input as a slice
    pub fn get_data(&self) -> &[u8] {
        self.data.get_ref()
    }
}

impl BufRead for CachedInput {
    fn fill_buf(&mut self) -> IoResult<&[u8]> {
        self.data.fill_buf()
    }

    fn consume(&mut self, amt: usize) {
        self.data.consume(amt)
    }
}

impl Read for CachedInput {
    fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
        self.data.read(buf)
    }
}

impl Seek for CachedInput {
    fn seek(&mut self, pos: io::SeekFrom) -> IoResult<u64> {
        self.data.seek(pos)
    }
}

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

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

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

/// A builder for [Input](crate::Input) that validates the path but
/// defers creating it until you call the [create](crate::InputPath::open) method.
///
/// It is designed to be used with the [`clap` crate](https://docs.rs/clap/latest) when taking a file name as an
/// argument to CLI app
/// ```
/// use clap::Parser;
/// use clio::InputPath;
///
/// #[derive(Parser)]
/// struct Opt {
///     /// path to file, use '-' for stdin
///     #[clap(value_parser)]
///     input_file: InputPath,
/// }
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct InputPath {
    path: OsString,
}

impl InputPath {
    /// Contructs a new [`InputPath`] representing the path and checking that the file exists and is readable
    ///
    /// note: even if this passes open may still fail if e.g. the file was delete in between
    pub fn new<S: AsRef<OsStr>>(path: S) -> Result<Self> {
        let path = path.as_ref().to_owned();
        #[cfg(feature = "http")]
        if is_http(&path) {
            try_to_url(&path)?;
            return Ok(InputPath { path });
        }
        if path != "-" && !Path::new(&path).is_file() {
            return Err(Error::io(ErrorKind::NotFound, "file not found"));
        }
        Ok(InputPath { path })
    }

    /// Contructs a new [`InputPath`] to stdout ("-")
    pub fn std() -> Self {
        InputPath { path: "-".into() }
    }

    /// Create an [`Input`] by opening the file or for '-' returning stdin
    pub fn open(self) -> Result<Input> {
        Input::new(&self.path)
    }

    /// The original path used to create this [`InputPath`]
    pub fn path(&self) -> &OsStr {
        &self.path
    }
}

impl_try_from!(InputPath);