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
use async_trait::async_trait;
use log::info;

use crate::{
    folder::{Folder, FolderKind, Folders},
    notmuch::NotmuchContextSync,
    AnyResult,
};

use super::ListFolders;

pub struct ListNotmuchFolders {
    ctx: NotmuchContextSync,
}

impl ListNotmuchFolders {
    pub fn new(ctx: &NotmuchContextSync) -> Self {
        Self { ctx: ctx.clone() }
    }

    pub fn new_boxed(ctx: &NotmuchContextSync) -> Box<dyn ListFolders> {
        Box::new(Self::new(ctx))
    }

    pub fn some_new_boxed(ctx: &NotmuchContextSync) -> Option<Box<dyn ListFolders>> {
        Some(Self::new_boxed(ctx))
    }
}

#[async_trait]
impl ListFolders for ListNotmuchFolders {
    async fn list_folders(&self) -> AnyResult<Folders> {
        info!("listing notmuch folders via maildir");

        let ctx = self.ctx.lock().await;
        let config = &ctx.account_config;
        let mdir_ctx = &ctx.mdir_ctx;

        let mut folders = Folders::default();

        folders.push(Folder {
            kind: Some(FolderKind::Inbox),
            name: config.get_inbox_folder_alias(),
            desc: mdir_ctx.root.path().to_string_lossy().to_string(),
        });

        let subfolders: Vec<Folder> =
            Folders::from_submaildirs(config, mdir_ctx.root.list_subdirs()).into();

        folders.extend(subfolders);

        Ok(folders)
    }
}