summaryrefslogtreecommitdiff
path: root/src/cmd/count.rs
blob: 5de542bb7adbc10d40cf8a760c53a074cd63b10a (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
use std::path::PathBuf;

use protobuf::Message;

use crate::cmd::open_submaildir;
use crate::error::Result;
use crate::pb3::jwebmail::{StatsReq, StatsResp};

pub fn count(path: PathBuf, req: &[u8]) -> Result<Vec<u8>> {
    let r = StatsReq::parse_from_bytes(req)?;

    let md = open_submaildir(path, &r.folder);

    let mut resp = StatsResp::new();
    resp.mail_count = md.count_cur() as u32;
    resp.unread_count = md
        .list_cur()
        .filter(|x| x.as_ref().map_or(false, |z| !z.is_seen()))
        .count() as u32;
    resp.byte_size = md
        .path()
        .join("cur")
        .read_dir()?
        .map(|x| x.map_or(0, |z| z.metadata().map_or(0, |y| y.len())))
        .sum();
    resp.write_to_bytes().map_err(|e| e.into())
}