~llndqvst/notmuch-mvrs

ref: fb4a33808e4436c4b4f91f0b11227d2c0ed8f99e notmuch-mvrs/src/movers/mbsync.rs -rw-r--r-- 5.5 KiB
fb4a3380Lukas Lindqvist squash! Add support for Maildir cur, tmp, and new 6 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::collections::VecDeque;
use std::ffi::{OsStr, OsString};
use std::fs::rename;
use std::path::PathBuf;

use crate::Mover;

pub struct Mbsync {
    target_path: PathBuf,
    original_files: VecDeque<PathBuf>,
    moved: i32,
}

impl Mbsync {
    fn strip_uid(s: &OsStr) -> PathBuf {
        let mut buf = OsString::new();
        let file_name_str = s.to_str().unwrap();

        fn is_uid(s: &str, search: &str) -> bool {
            let mut s_trunc = String::from(s);
            s_trunc.truncate(2);
            return s_trunc.eq(search);
        }

        let mut tags: Vec<&str> = file_name_str.split(',').collect();

        match tags.iter().rposition(|&r| is_uid(r, "U=")) {
            Some(x) => {
                tags.remove(x);
                buf.push(tags.join(","));
                PathBuf::from(buf)
            }
            None => PathBuf::from(s),
        }
    }

    fn get_rename_pair(&self, from: PathBuf) -> (PathBuf, PathBuf) {
        let new_name: PathBuf = Mbsync::strip_uid(from.file_name().unwrap());
        let mut f = from.clone();
        if f.pop() {
            let maildir_parent = f.file_name().unwrap();
            println!("target: {}, maildir_parent: {}", self.target_path.to_str().unwrap(), maildir_parent.to_str().unwrap());
            (from, self.target_path.join(maildir_parent).join(new_name))
        } else {
            println!("target: {}, ", self.target_path.to_str().unwrap());
            (from, self.target_path.join(new_name))
        }
    }
}

impl Mover<PathBuf> for Mbsync {
    fn new(p: PathBuf) -> Self {
        Mbsync {
            original_files: VecDeque::new(),
            target_path: p,
            moved: i32::from(0),
        }
    }

    fn rename(&mut self) -> Result<i32, &str> {
        while let Some(y) = self.original_files.pop_front() {
            let (from, to): (PathBuf, PathBuf) = self.get_rename_pair(y);
            let ct = to.clone();
            println!("{} -> {}", from.to_str().unwrap(), to.to_str().unwrap());
            match rename(from, to) {
                Ok(_) => (),
                Err(e) => eprintln!("{}, {}", ct.to_str().unwrap(), e.to_string()),
            }
            self.moved = self.moved + 1;
        }
        Ok(self.moved)
    }

    fn dry_rename(&mut self) -> Result<i32, &str> {
        while let Some(y) = self.original_files.pop_front() {
            let (from, to): (PathBuf, PathBuf) = self.get_rename_pair(y);
            match from.eq(&to) {
                true => println!("{}: already at desired location. Skipping", from.to_str().unwrap()),
                false => println!("{} -> {}", from.to_str().unwrap(), to.to_str().unwrap()),
            };
            self.moved = self.moved + 1;
        }
        Ok(self.moved)
    }

    fn add_file_path(&mut self, p: PathBuf) {
        self.original_files.push_back(p);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::ffi::OsStr;

    #[test]
    fn internal_strip_uid() {
        assert_eq!(
            Mbsync::strip_uid(OsStr::new("test,U=1")),
            PathBuf::from("test")
        );
        assert_eq!(
            Mbsync::strip_uid(OsStr::new("test,U=10,S")),
            PathBuf::from("test,S")
        );
        assert_eq!(Mbsync::strip_uid(OsStr::new("test")), PathBuf::from("test"));
        assert_eq!(
            Mbsync::strip_uid(OsStr::new("test,U=1:2,S")),
            PathBuf::from("test,S")
        );
        assert_ne!(
            Mbsync::strip_uid(OsStr::new("test,U=1:2,s")),
            PathBuf::from("test,U=1:2,s")
        );
    }

    #[test]
    fn internal_get_rename_pair() {
        let p = PathBuf::from("test");
        let mut files: Mbsync = Mover::new(PathBuf::from("test"));

        let f1 = PathBuf::from("test1");
        let f2 = PathBuf::from("test,S");
        let f3 = PathBuf::from("test,S");
        let f4 = PathBuf::from("test4");

        files.add_file_path(PathBuf::from("test1"));
        files.add_file_path(PathBuf::from("test,U=10,S"));
        files.add_file_path(PathBuf::from("test,U=1:2,S"));
        files.add_file_path(PathBuf::from("test4"));

        {
            let fof = files.original_files.pop_front();
            let (a, b): (PathBuf, PathBuf) = files.get_rename_pair(fof.unwrap());
            assert_eq!(f1, a);
            assert_eq!(p.join(f1), b);
        }

        {
            let fof = files.original_files.pop_front();
            let (a, b): (PathBuf, PathBuf) = files.get_rename_pair(fof.unwrap());
            assert_eq!(PathBuf::from("test,U=10,S"), a);
            assert_eq!(p.join(f2), b);
        }

        {
            let fof = files.original_files.pop_front();
            let (a, b): (PathBuf, PathBuf) = files.get_rename_pair(fof.unwrap());
            assert_eq!(PathBuf::from("test,U=1:2,S"), a);
            assert_eq!(p.join(f3), b);
        }

        {
            let fof = files.original_files.pop_front();
            let (a, b): (PathBuf, PathBuf) = files.get_rename_pair(fof.unwrap());
            assert_eq!(f4, a);
            assert_eq!(p.join(f4), b);
        }
    }

    #[test]
    fn new() {
        let files: Mbsync = Mover::new(PathBuf::from("test"));

        assert_eq!(files.target_path, PathBuf::from("test"));
    }

    #[test]
    fn dry_rename() {
        let mut files: Mbsync = Mover::new(PathBuf::from("test"));
        // let file1: PathBuf = PathBuf
        files.add_file_path(PathBuf::from("test1"));
        files.add_file_path(PathBuf::from("test2"));
        files.add_file_path(PathBuf::from("test3"));
        files.add_file_path(PathBuf::from("test4"));

        assert_eq!(files.dry_rename().unwrap(), i32::from(4));
    }
}