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
//! Set of high level IO methods.

#![crate_name = "monstrio"]

use std::io::{BufRead, BufReader};
use std::fs::File;

#[macro_use]
extern crate log;

extern crate multi_reader;


pub struct Input<R> {
    source: R,
}

pub type MReader<I> = multi_reader::MultiReader<File, I>;
pub type BMReader<I> = BufReader<MReader<I>>;

impl<R: BufRead> AsMut<R> for Input<R> {
    fn as_mut(&mut self) -> &mut R {
        &mut self.source
    }
}

impl<I: Iterator<Item = File>> Input<BMReader<I>> {
    pub fn files(files: I) -> Input<BMReader<I>> {
        Input { source: BMReader::new(MReader::new(files)) }
    }
}

#[cfg(feature = "glob")]
mod glob_input {
    use std::fs::{File, metadata};
    use std::vec::IntoIter;

    extern crate glob;
    use self::glob::glob;

    impl super::Input<super::BMReader<IntoIter<File>>> {
        pub fn glob<P: Iterator>(patterns: P) -> super::Input<super::BMReader<IntoIter<File>>>
            where P::Item: AsRef<str>
        {
            let mut files = Vec::new();
            for pattern in patterns {
                match glob(pattern.as_ref()) {
                    Ok(entries) => {
                        for entry in entries {
                            match entry {
                                Ok(ref path) => {
                                    match metadata(path) {
                                        Ok(m) => {
                                            if m.is_dir() {
                                                warn!("Pattern matches to a directory: {}",
                                                      path.display());
                                                continue;
                                            }
                                        }
                                        Err(err) => {
                                            warn!("Cannot read file metadata {}\n{}",
                                                  path.display(),
                                                  err);
                                            continue;
                                        }
                                    }
                                    match File::open(path) {
                                        Ok(file) => files.push(file),
                                        Err(err) => {
                                            warn!("Cannot open file {}\n{}", path.display(), err)
                                        }
                                    }
                                }
                                Err(err) => warn!("Cannot access file {}", err),
                            }
                        }
                    }
                    Err(err) => warn!("Bad pattern {}\n{}", pattern.as_ref(), err),
                }
            }

            super::Input::files(files.into_iter())
        }
    }
}

#[cfg(feature = "glob")]
pub use self::glob_input::*;