summaryrefslogtreecommitdiffstats
path: root/src/mail.rs
blob: c818a3dcf14080ce75e0982396811585e133cecb (plain)
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
use std::path::PathBuf;
use anyhow::Result;
use anyhow::Error;
use mailparse::ParsedMail;

#[derive(Clone, Debug)]
pub struct Mail {
    id: String,
    buffer: Vec<u8>,
    parsed: ParsedMail,
}

impl Mail {
    pub fn read_from_path(id: String, pb: PathBuf) -> Result<Self> {
        std::fs::read(pb)
            .map_err(Error::from)
            .and_then(|buffer| {
                mailparse::parse_mail(buffer.clone())
                    .map(|parsed| (buffer, parsed))
                    .map_err(Error::from)
            })
            .map(|(buffer, parsed)| Mail { id, buffer, parsed })
    }

    pub fn parsed(&self) -> &ParsedMail {
        &self.parsed
    }

    pub fn id(&self) -> &String {
        &self.id
    }

}