~llndqvst/notmuch-mvrs

ref: ae973f7bd1e1c9aa26e4889458f3da67846cbb3c notmuch-mvrs/src/mover_config.rs -rw-r--r-- 1.3 KiB
ae973f7bLukas Lindqvist Add support for actually renaming emails 9 months ago
                                                                                
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
use crate::movers::mover::Mover;
use std::path::PathBuf;

pub struct MoverConfig<'a, T>
where
    T: Mover<PathBuf>,
{
    mover: T,
    nm_db: &'a notmuch::Database,
    query_string: &'a str,
}

impl<'a, T> MoverConfig<'a, T>
where
    T: Mover<PathBuf>,
{
    pub fn new(
        nm_db_location: &'a notmuch::Database,
        query: &'a str,
        target_path: PathBuf,
    ) -> Self {
        MoverConfig {
            mover: T::new(target_path),
            nm_db: nm_db_location,
            query_string: query,
        }
    }

    pub fn dry_execute(&mut self) -> Result<i32, &str> {
        let query = self.nm_db.create_query(self.query_string).unwrap();
        let messages = query.search_messages().unwrap();
        for m in messages {
            self.mover.add_file_path(m.filename());
        }
        self.mover.dry_rename()
    }

    pub fn execute(&mut self) -> Result<i32, notmuch::Error> {
        let query = self.nm_db.create_query(self.query_string).unwrap();
        let messages = match query.search_messages() {
            Ok(mq) => {
                for m in mq {
                    self.mover.add_file_path(m.filename());
                }
                Ok(self.mover.rename().unwrap())
            },
            Err(e) => {
                Err(e)
            }
        };
        messages
    }
}