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
use std::io::{BufRead, BufReader, Lines, Read};

use crate::error::WithOrExit;
use std::fs::File;
use std::str::FromStr;
use std::{fmt, io, marker, mem};

/// Iterator over the lines that have been mapped to the requested type.
pub struct MappedLines<T: FromStr, R: Read> {
    exit_code_on_fail: i32,
    lines: Lines<BufReader<R>>,
    _marker: marker::PhantomData<T>,
}

impl<T: FromStr, R: Read> Iterator for MappedLines<T, R>
where
    <T as FromStr>::Err: fmt::Debug,
{
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.lines
            .next()
            .map(|line| line.or_exit_with(self.exit_code_on_fail))
            .map(|line| line.parse::<T>())
            .map(|record| record.or_exit_with(self.exit_code_on_fail))
    }
}

pub trait WithReadLines<R: Read> {
    /// Read lines from a given Readable object, and parse each line to the requested type.
    fn read_lines<T: FromStr>(self, exit_code_on_fail: i32) -> MappedLines<T, R>;
}

impl WithReadLines<File> for File {
    fn read_lines<T: FromStr>(self, exit_code_on_fail: i32) -> MappedLines<T, File> {
        MappedLines {
            exit_code_on_fail,
            lines: BufReader::new(self).lines(),
            _marker: Default::default(),
        }
    }
}

impl WithReadLines<File> for io::Result<File> {
    fn read_lines<T: FromStr>(self, exit_code_on_fail: i32) -> MappedLines<T, File> {
        self.or_exit_with(exit_code_on_fail)
            .read_lines(exit_code_on_fail)
    }
}

pub trait WithAsRecords {
    /// Convert to a list of records of the requested type.
    fn as_records<T: FromStr>(&self) -> Result<Vec<T>, <T as FromStr>::Err>;
}

impl<'a> WithAsRecords for Vec<&'a str> {
    fn as_records<T: FromStr>(&self) -> Result<Vec<T>, <T as FromStr>::Err> {
        let mut records = Vec::new();
        for line in self {
            records.push(line.parse::<T>()?);
        }
        Ok(records)
    }
}

/// Inspired by `std::str::FromStr`, so you can read input files where a record spans multiple
/// lines.
pub trait MultilineFromStr {
    /// The associated error which can be returned from parsing.
    type Err;

    /// Create a new initial record.
    fn new() -> Self;

    /// Test for whether a line indicates that a new record starts.
    fn indicates_new_record(&self, line: &str) -> bool;

    /// Parses a line.
    fn parse(&mut self, line: &str) -> Result<(), Self::Err>;
}

/// Iterator over the lines that have been mapped to the requested type.
pub struct MappedMultiLines<T: MultilineFromStr, R: Read> {
    exit_code_on_fail: i32,
    lines: Lines<BufReader<R>>,
    current: T,
    exhausted: bool,
}

impl<T: MultilineFromStr, R: Read> Iterator for MappedMultiLines<T, R>
where
    <T as MultilineFromStr>::Err: fmt::Debug,
{
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.exhausted {
            None
        } else {
            let mut record = mem::replace(&mut self.current, T::new());
            self.exhausted = true;
            while let Some(line) = self.lines.next() {
                let line = line.or_exit_with(self.exit_code_on_fail);
                if self.current.indicates_new_record(&line) {
                    self.exhausted = false;
                    self.current
                        .parse(&line)
                        .or_exit_with(self.exit_code_on_fail);
                    break;
                }
                record.parse(&line).or_exit_with(self.exit_code_on_fail);
            }

            Some(record)
        }
    }
}

pub trait WithReadMultiLines<R: Read> {
    /// Read lines from a given Readable object, and parse each line to the requested type.
    fn read_multi_lines<T: MultilineFromStr>(
        self,
        exit_code_on_fail: i32,
    ) -> MappedMultiLines<T, R>;
}

impl WithReadMultiLines<File> for File {
    fn read_multi_lines<T: MultilineFromStr>(
        self,
        exit_code_on_fail: i32,
    ) -> MappedMultiLines<T, File> {
        MappedMultiLines {
            exit_code_on_fail,
            lines: BufReader::new(self).lines(),
            current: T::new(),
            exhausted: false,
        }
    }
}

impl WithReadMultiLines<File> for io::Result<File> {
    fn read_multi_lines<T: MultilineFromStr>(
        self,
        exit_code_on_fail: i32,
    ) -> MappedMultiLines<T, File> {
        self.or_exit_with(exit_code_on_fail)
            .read_multi_lines(exit_code_on_fail)
    }
}

pub trait WithAsMultilineRecords {
    /// Convert to a list of records of the requested type.
    fn as_multiline_records<T: MultilineFromStr>(&self) -> Result<Vec<T>, <T as MultilineFromStr>::Err>;
}

impl<'a> WithAsMultilineRecords for Vec<&'a str> {
    fn as_multiline_records<T: MultilineFromStr>(&self) -> Result<Vec<T>, <T as MultilineFromStr>::Err> {
        let mut records = Vec::new();
        let mut record = T::new();
        for line in self {
            if record.indicates_new_record(line) {
                records.push(record);
                record = T::new();
            }
            record.parse(line)?;
        }
        records.push(record);
        Ok(records)
    }
}