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
use std::fmt::{self, Debug, Formatter};
use std::path::Path;
use std::{fs, io, str};

pub struct MboxReader<'a> {
    data: &'a MboxFile,
    idx: usize,
    prev: usize,
    testing: usize,
}

impl<'a> MboxReader<'a> {
    fn new(map: &MboxFile) -> MboxReader {
        MboxReader {
            data: map,
            idx: 0,
            prev: 0,
            testing: 5,
        }
    }
}

impl<'a> Iterator for MboxReader<'a> {
    type Item = Entry<'a>;
    fn next(&mut self) -> Option<Self::Item> {
        let bytes = self.data.as_slice();
        if self.idx >= self.data.len() {
            return None;
        }
        for b in &bytes[self.idx..] {
            if *b == b'\n' {
                self.testing = 5;
                self.idx += 1;
                continue;
            } else if self.testing == 5 && *b == b'F' {
                self.testing = 4;
            } else if self.testing == 4 && *b == b'r' {
                self.testing = 3;
            } else if self.testing == 3 && *b == b'o' {
                self.testing = 2;
            } else if self.testing == 2 && *b == b'm' {
                self.testing = 1;
            } else if self.testing == 1 && *b == b' ' {
                self.testing = 0;
                let start = self.idx - 4;
                if start != 0 {
                    let entry = Entry {
                        idx: start,
                        bytes: &bytes[self.prev..start],
                    };
                    self.prev = start;
                    return Some(entry);
                }
            } else {
                self.testing = 0;
            }
            self.idx += 1;
        }
        None
    }
}

/// The mbox file to read. This uses the OS facility to memory-map the file in
/// order to read it efficiently.
pub struct MboxFile {
    map: memmap::Mmap,
}

impl MboxFile {
    pub fn from_file(name: &Path) -> io::Result<MboxFile> {
        Ok(MboxFile {
            map: unsafe { memmap::Mmap::map(&fs::File::open(name)?)? },
        })
    }
    fn len(&self) -> usize {
        self.map.len()
    }
    fn as_slice(&self) -> &[u8] {
        &self.map
    }
    pub fn iter<'a>(&'a self) -> MboxReader<'a> {
        MboxReader::new(self)
    }
}

pub struct Entry<'a> {
    idx: usize,
    bytes: &'a [u8],
}

impl<'a> Entry<'a> {
    pub fn offset(&self) -> usize {
        self.idx
    }
    pub fn start(&self) -> Start {
        match self.bytes.iter().position(|b| *b == b'\n') {
            Some(pos) => Start::new(&self.bytes[..pos - 1]),
            None => Start::new(&self.bytes),
        }
    }
    pub fn message(&self) -> Option<&[u8]> {
        self.bytes
            .iter()
            .position(|b| *b == b'\n')
            .and_then(|idx| Some(&self.bytes[idx + 1..]))
    }
}

impl<'a> Debug for Entry<'a> {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        f.write_fmt(format_args!(
            "Entry {{ {} bytes @ {} }}",
            self.bytes.len(),
            self.idx
        ))
    }
}

pub struct Start<'a> {
    bytes: &'a [u8],
    address: &'a str,
    date: &'a str,
}

impl<'a> Start<'a> {
    fn new(bytes: &'a [u8]) -> Start {
        let mut parts = bytes.splitn(3, |b| *b == b' ');
        let _ = parts.next();
        let address = str::from_utf8(parts.next().unwrap()).unwrap();
        let date = str::from_utf8(parts.next().unwrap()).unwrap();
        Start {
            bytes,
            address,
            date,
        }
    }
    pub fn address(&self) -> &str {
        self.address
    }
    pub fn date(&self) -> &str {
        self.date
    }
    pub fn as_str(&self) -> &str {
        str::from_utf8(self.bytes).unwrap()
    }
}