email-lib 0.27.0

Cross-platform, asynchronous Rust library to manage emails
Documentation
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
//! Module dedicated to account configuration.
//!
//! This module contains the representation of the user's current
//! account configuration named [`AccountConfig`].

#[cfg(feature = "oauth2")]
pub mod oauth2;
pub mod passwd;
#[cfg(feature = "pgp")]
pub mod pgp;

use std::{
    collections::HashMap,
    env::temp_dir,
    ffi::OsStr,
    fs, io,
    path::{Path, PathBuf},
    vec,
};

#[cfg(feature = "derive")]
use crate::serde::deserialize_shell_expanded_string;
#[cfg(feature = "sync")]
use dirs::data_dir;
use dirs::download_dir;
use mail_builder::headers::address::{Address, EmailAddress};
use mail_parser::Address::*;
use mml::MimeInterpreterBuilder;
#[cfg(feature = "notify")]
use notify_rust::Notification;
use process::Command;
use shellexpand_utils::{shellexpand_path, shellexpand_str, try_shellexpand_path};
use tracing::debug;

#[cfg(feature = "pgp")]
use self::pgp::PgpConfig;
#[cfg(feature = "sync")]
use super::sync::config::SyncConfig;
#[doc(inline)]
pub use super::{Error, Result};
use crate::{
    date::from_mail_parser_to_chrono_datetime,
    email::config::EmailTextPlainFormat,
    envelope::{config::EnvelopeConfig, Envelope},
    flag::config::FlagConfig,
    folder::{config::FolderConfig, FolderKind, DRAFTS, INBOX, SENT, TRASH},
    message::config::MessageConfig,
    template::{
        config::TemplateConfig,
        forward::config::{ForwardTemplatePostingStyle, ForwardTemplateSignatureStyle},
        new::config::NewTemplateSignatureStyle,
        reply::config::{ReplyTemplatePostingStyle, ReplyTemplateSignatureStyle},
    },
    watch::config::WatchHook,
};

pub const DEFAULT_PAGE_SIZE: usize = 10;
pub const DEFAULT_SIGNATURE_DELIM: &str = "-- \n";

pub trait HasAccountConfig {
    fn account_config(&self) -> &AccountConfig;
}

/// The user's account configuration.
///
/// It represents everything that the user can customize for a given
/// account. It is the main configuration used by all other
/// modules. Usually, it serves as a reference for building config
/// file structure.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[cfg_attr(
    feature = "derive",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "kebab-case", deny_unknown_fields)
)]
pub struct AccountConfig {
    /// The name of the user account.
    ///
    /// The account name is used as an unique identifier for a given
    /// configuration.
    pub name: String,

    /// The email address of the user account.
    #[cfg_attr(
        feature = "derive",
        serde(deserialize_with = "deserialize_shell_expanded_string")
    )]
    pub email: String,

    /// The display name of the user.
    ///
    /// It usually corresponds to the full name of the user.
    pub display_name: Option<String>,

    /// The email signature of the user.
    ///
    /// It can be either a path to a file (usually `~/.signature`) or
    /// a raw string.
    pub signature: Option<String>,

    /// The email signature delimiter of the user signature.
    ///
    /// Defaults to `-- \n`.
    pub signature_delim: Option<String>,

    /// The downloads directory.
    ///
    /// It is mostly used for downloading messages
    /// attachments. Defaults to the system temporary directory
    /// (usually `/tmp`).
    pub downloads_dir: Option<PathBuf>,

    /// The folder configuration.
    pub folder: Option<FolderConfig>,

    /// The envelope configuration.
    pub envelope: Option<EnvelopeConfig>,

    /// The flag configuration.
    pub flag: Option<FlagConfig>,

    /// The message configuration.
    pub message: Option<MessageConfig>,

    /// The message configuration.
    pub template: Option<TemplateConfig>,

    /// The account synchronization configuration.
    #[cfg(feature = "sync")]
    pub sync: Option<SyncConfig>,

    /// The PGP configuration.
    #[cfg(feature = "pgp")]
    pub pgp: Option<PgpConfig>,
}

impl AccountConfig {
    /// Get the signature, including the delimiter.
    ///
    /// Uses the default delimiter `-- \n` in case no delimiter has
    /// been defined. Return `None` if no signature has been defined.
    pub fn find_full_signature(&self) -> Option<String> {
        let delim = self
            .signature_delim
            .as_deref()
            .unwrap_or(DEFAULT_SIGNATURE_DELIM);

        let signature = self.signature.as_ref();

        signature.map(|path_or_raw| {
            let signature = try_shellexpand_path(path_or_raw)
                .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))
                .and_then(fs::read_to_string)
                .unwrap_or_else(|_err| {
                    debug!("cannot read signature from path: {_err}");
                    debug!("{_err:?}");
                    shellexpand_str(path_or_raw)
                });
            format!("{}{}", delim, signature.trim())
        })
    }

    /// Get then expand the downloads directory path.
    ///
    /// Falls back to [`dirs::download_dir`].
    pub fn get_downloads_dir(&self) -> PathBuf {
        self.downloads_dir
            .as_ref()
            .map(shellexpand_path)
            .or_else(download_dir)
            .unwrap_or_else(temp_dir)
    }

    /// Build the downloadable version of the given path.
    ///
    /// The aim of this helper is to build a safe download path for a
    /// given path.
    ///
    /// First, only the file name of the give path is taken in order
    /// to prevent any interaction outside of the downloads directory.
    ///
    /// Then, a suffix may be added to the final path if it already
    /// exists on the filesystem in order to prevent any overriding or
    /// data loss.
    pub fn get_download_file_path(
        &self,
        downloads_dir: Option<&Path>,
        path: impl AsRef<Path>,
    ) -> Result<PathBuf> {
        let path = path.as_ref();

        let file_name = path
            .file_name()
            .ok_or_else(|| Error::GetFileNameFromPathSyncError(path.to_owned()))?;

        let final_path = match downloads_dir {
            Some(dir) => dir.join(file_name),
            None => self.get_downloads_dir().join(file_name),
        };

        rename_file_if_duplicate(&final_path, |path, _count| path.is_file())
    }

    /// Return `true` if the synchronization is enabled.
    #[cfg(feature = "sync")]
    pub fn is_sync_enabled(&self) -> bool {
        self.sync
            .as_ref()
            .and_then(|c| c.enable)
            .unwrap_or_default()
    }

    /// Return `true` if the synchronization directory already exists.
    #[cfg(feature = "sync")]
    pub fn does_sync_dir_exist(&self) -> bool {
        match self.sync.as_ref().and_then(|c| c.dir.as_ref()) {
            Some(dir) => try_shellexpand_path(dir).is_ok(),
            None => data_dir()
                .map(|dir| {
                    dir.join("pimalaya")
                        .join("email")
                        .join("sync")
                        .join(&self.name)
                        .is_dir()
                })
                .unwrap_or_default(),
        }
    }

    /// Execute the envelope received hook.
    #[cfg(feature = "watch")]
    pub async fn exec_received_envelope_hook(&self, envelope: &Envelope) {
        let hook = self
            .envelope
            .as_ref()
            .and_then(|c| c.watch.as_ref())
            .and_then(|c| c.received.as_ref());

        if let Some(hook) = hook.as_ref() {
            self.exec_envelope_hook(hook, envelope).await
        }
    }

    /// Execute the envelope any hook.
    #[cfg(feature = "watch")]
    pub async fn exec_any_envelope_hook(&self, envelope: &Envelope) {
        let hook = self
            .envelope
            .as_ref()
            .and_then(|c| c.watch.as_ref())
            .and_then(|c| c.any.as_ref());

        if let Some(hook) = hook.as_ref() {
            self.exec_envelope_hook(hook, envelope).await
        }
    }

    /// Execute the given envelope hook.
    pub async fn exec_envelope_hook(&self, hook: &WatchHook, envelope: &Envelope) {
        let sender = envelope.from.name.as_deref().unwrap_or(&envelope.from.addr);
        let sender_name = envelope.from.name.as_deref().unwrap_or("unknown");
        let recipient = envelope.to.name.as_deref().unwrap_or(&envelope.to.addr);
        let recipient_name = envelope.to.name.as_deref().unwrap_or("unknown");

        if let Some(cmd) = hook.cmd.as_ref() {
            let res = cmd
                .clone()
                .replace("{id}", &envelope.id)
                .replace("{subject}", &envelope.subject)
                .replace("{sender}", sender)
                .replace("{sender.name}", sender_name)
                .replace("{sender.address}", &envelope.from.addr)
                .replace("{recipient}", recipient)
                .replace("{recipient.name}", recipient_name)
                .replace("{recipient.address}", &envelope.to.addr)
                .run()
                .await;

            if let Err(_err) = res {
                debug!("error while executing watch command hook");
                debug!("{_err:?}");
            }
        }

        #[allow(unused_variables)]
        let replace = move |fmt: &str, envelope: &Envelope| -> String {
            fmt.replace("{id}", &envelope.id)
                .replace("{subject}", &envelope.subject)
                .replace("{sender}", sender)
                .replace("{sender.name}", sender_name)
                .replace("{sender.address}", &envelope.from.addr)
                .replace("{recipient}", recipient)
                .replace("{recipient.name}", recipient_name)
                .replace("{recipient.address}", &envelope.to.addr)
        };

        #[cfg(all(feature = "notify", target_os = "linux"))]
        if let Some(notify) = hook.notify.as_ref() {
            let res = Notification::new()
                .summary(&replace(&notify.summary, envelope))
                .body(&replace(&notify.body, envelope))
                .show_async()
                .await;
            if let Err(err) = res {
                debug!("error while sending system notification");
                debug!("{err:?}");
            }
        }

        #[cfg(all(feature = "notify", not(target_os = "linux")))]
        if let Some(notify) = hook.notify.as_ref() {
            let summary = replace(&notify.summary, &envelope);
            let body = replace(&notify.body, &envelope);

            let res = tokio::task::spawn_blocking(move || {
                Notification::new().summary(&summary).body(&body).show()
            })
            .await;

            if let Err(err) = res {
                debug!("cannot send system notification");
                debug!("{err:?}");
            } else {
                let res = res.unwrap();
                if let Err(err) = res {
                    debug!("error while sending system notification");
                    debug!("{err:?}");
                }
            }
        }

        if let Some(callback) = hook.callback.as_ref() {
            let res = callback(envelope).await;
            if let Err(_err) = res {
                debug!("error while executing callback");
                debug!("{_err:?}");
            }
        }
    }

    /// Find the alias of the given folder name.
    ///
    /// The alias is also shell expanded.
    pub fn find_folder_alias(&self, from_name: &str) -> Option<String> {
        self.folder
            .as_ref()
            .and_then(|c| c.aliases.as_ref())
            .and_then(|aliases| {
                aliases.iter().find_map(|(name, alias)| {
                    if name.eq_ignore_ascii_case(from_name.trim()) {
                        Some(shellexpand_str(alias))
                    } else {
                        None
                    }
                })
            })
    }

    /// Find the alias of the given folder, otherwise return the given
    /// folder itself.
    pub fn get_folder_alias(&self, folder: &str) -> String {
        self.find_folder_alias(folder)
            .unwrap_or_else(|| shellexpand_str(folder))
    }

    /// Get the inbox folder alias.
    pub fn get_inbox_folder_alias(&self) -> String {
        self.get_folder_alias(INBOX)
    }

    /// Get the sent folder alias.
    pub fn get_sent_folder_alias(&self) -> String {
        self.get_folder_alias(SENT)
    }

    /// Get the drafts folder alias.
    pub fn get_drafts_folder_alias(&self) -> String {
        self.get_folder_alias(DRAFTS)
    }

    /// Get the trash folder alias.
    pub fn get_trash_folder_alias(&self) -> String {
        self.get_folder_alias(TRASH)
    }

    /// Return `true` if the given folder matches the Trash folder.
    pub fn is_trash_folder(&self, folder: &str) -> bool {
        self.get_folder_alias(folder) == self.get_trash_folder_alias()
    }

    /// Return `true` if the delete message style matches the
    /// flag-based message deletion style.
    pub fn is_delete_message_style_flag(&self) -> bool {
        self.message
            .as_ref()
            .and_then(|c| c.delete.as_ref())
            .and_then(|c| c.style.as_ref())
            .filter(|c| c.is_flag())
            .is_some()
    }

    /// Get all folder aliases.
    pub fn get_folder_aliases(&self) -> Option<&HashMap<String, String>> {
        self.folder.as_ref().and_then(|c| c.aliases.as_ref())
    }

    /// Find the folder kind associated to the given folder alias.
    ///
    /// This function is the reverse of [`get_folder_alias`], as it
    /// tries to find a key (folder kind) matching the given value
    /// (folder alias).
    pub fn find_folder_kind_from_alias(&self, alias: &str) -> Option<FolderKind> {
        self.folder
            .as_ref()
            .and_then(|c| c.aliases.as_ref())
            .and_then(|aliases| {
                let from_alias = shellexpand_str(alias);
                aliases.iter().find_map(|(kind_or_name, alias)| {
                    if shellexpand_str(alias).eq_ignore_ascii_case(&from_alias) {
                        Some(kind_or_name.into())
                    } else {
                        None
                    }
                })
            })
    }

    /// Get the envelope listing page size if defined, otherwise
    /// return the default one.
    pub fn get_envelope_list_page_size(&self) -> usize {
        self.envelope
            .as_ref()
            .and_then(|c| c.list.as_ref())
            .and_then(|c| c.page_size)
            .unwrap_or(DEFAULT_PAGE_SIZE)
    }

    /// Get the envelope threading page size if defined, otherwise
    /// return the default one.
    #[cfg(feature = "thread")]
    pub fn get_envelope_thread_page_size(&self) -> usize {
        self.envelope
            .as_ref()
            .and_then(|c| c.thread.as_ref())
            .and_then(|c| c.page_size)
            .unwrap_or(DEFAULT_PAGE_SIZE)
    }

    /// Get the message reading format if defined, otherwise return
    /// the default one.
    pub fn get_message_read_format(&self) -> EmailTextPlainFormat {
        self.message
            .as_ref()
            .and_then(|c| c.read.as_ref())
            .and_then(|c| c.format.as_ref())
            .cloned()
            .unwrap_or_default()
    }

    /// Get the message reading headers if defined, otherwise return
    /// the default ones.
    pub fn get_message_read_headers(&self) -> Vec<String> {
        self.message
            .as_ref()
            .and_then(|c| c.read.as_ref())
            .and_then(|c| c.headers.as_ref())
            .cloned()
            .unwrap_or(vec![
                "From".into(),
                "To".into(),
                "Cc".into(),
                "Subject".into(),
            ])
    }

    /// Get the message writing headers if defined, otherwise return
    /// the default ones.
    pub fn get_message_write_headers(&self) -> Vec<String> {
        self.message
            .as_ref()
            .and_then(|c| c.write.as_ref())
            .and_then(|c| c.headers.as_ref())
            .cloned()
            .unwrap_or(vec![
                "From".into(),
                "To".into(),
                "In-Reply-To".into(),
                "Cc".into(),
                "Subject".into(),
            ])
    }

    /// Find the message pre-send hook.
    pub fn find_message_pre_send_hook(&self) -> Option<&Command> {
        self.message
            .as_ref()
            .and_then(|c| c.send.as_ref())
            .and_then(|c| c.pre_hook.as_ref())
    }

    /// Return `true` if a copy of sent messages should be saved in
    /// the sent folder.
    pub fn should_save_copy_sent_message(&self) -> bool {
        self.message
            .as_ref()
            .and_then(|c| c.send.as_ref())
            .and_then(|c| c.save_copy)
            .unwrap_or(true)
    }

    /// Generate a template interpreter with prefilled options from
    /// the current user account configuration.
    pub fn generate_tpl_interpreter(&self) -> MimeInterpreterBuilder {
        let builder =
            MimeInterpreterBuilder::new().with_save_attachments_dir(self.get_downloads_dir());

        #[cfg(feature = "pgp")]
        if let Some(ref pgp) = self.pgp {
            return builder.with_pgp(pgp.clone());
        }

        builder
    }

    /// Get the envelope listing datetime format, otherwise return the
    /// default one.
    pub fn get_envelope_list_datetime_fmt(&self) -> String {
        self.envelope
            .as_ref()
            .and_then(|c| c.list.as_ref())
            .and_then(|c| c.datetime_fmt.clone())
            .unwrap_or_else(|| String::from("%F %R%:z"))
    }

    /// Return `true` if the envelope listing datetime local timezone
    /// option is enabled.
    pub fn has_envelope_list_datetime_local_tz(&self) -> bool {
        self.envelope
            .as_ref()
            .and_then(|c| c.list.as_ref())
            .and_then(|c| c.datetime_local_tz)
            .unwrap_or_default()
    }

    /// Get the new template signature placement.
    pub fn get_new_template_signature_style(&self) -> NewTemplateSignatureStyle {
        self.template
            .as_ref()
            .and_then(|c| c.new.as_ref())
            .and_then(|c| c.signature_style.clone())
            .unwrap_or_default()
    }

    pub fn get_reply_template_signature_style(&self) -> ReplyTemplateSignatureStyle {
        self.template
            .as_ref()
            .and_then(|c| c.reply.as_ref())
            .and_then(|c| c.signature_style.clone())
            .unwrap_or_default()
    }

    pub fn get_reply_template_posting_style(&self) -> ReplyTemplatePostingStyle {
        self.template
            .as_ref()
            .and_then(|c| c.reply.as_ref())
            .and_then(|c| c.posting_style.clone())
            .unwrap_or_default()
    }

    pub fn get_reply_template_quote_headline(&self, msg: &mail_parser::Message) -> Option<String> {
        let date = from_mail_parser_to_chrono_datetime(msg.date()?)?;

        let senders = match (msg.from(), msg.sender()) {
            (Some(List(a)), _) if !a.is_empty() => {
                a.iter().fold(String::new(), |mut senders, sender| {
                    if let Some(name) = sender.name() {
                        if !senders.is_empty() {
                            senders.push_str(", ");
                        }
                        senders.push_str(name);
                    } else if let Some(addr) = sender.address() {
                        if !senders.is_empty() {
                            senders.push_str(", ");
                        }
                        senders.push_str(addr);
                    }
                    senders
                })
            }
            (Some(Group(g)), _) if !g.is_empty() => {
                g.iter().fold(String::new(), |mut senders, sender| {
                    if let Some(ref name) = sender.name {
                        if !senders.is_empty() {
                            senders.push_str(", ");
                        }
                        senders.push_str(name);
                    }
                    senders
                })
            }
            (_, Some(List(a))) if !a.is_empty() => {
                a.iter().fold(String::new(), |mut senders, sender| {
                    if let Some(name) = sender.name() {
                        if !senders.is_empty() {
                            senders.push_str(", ");
                        }
                        senders.push_str(name);
                    } else if let Some(addr) = sender.address() {
                        if !senders.is_empty() {
                            senders.push_str(", ");
                        }
                        senders.push_str(addr);
                    }
                    senders
                })
            }
            (_, Some(Group(g))) if !g.is_empty() => {
                g.iter().fold(String::new(), |mut senders, sender| {
                    if let Some(ref name) = sender.name {
                        if !senders.is_empty() {
                            senders.push_str(", ");
                        }
                        senders.push_str(name);
                    }
                    senders
                })
            }
            _ => String::new(),
        };

        let fmt = self
            .template
            .as_ref()
            .and_then(|c| c.reply.as_ref())
            .and_then(|c| c.quote_headline_fmt.clone())
            .unwrap_or_else(|| String::from("On %d/%m/%Y %H:%M, {senders} wrote:\n"));

        Some(date.format(&fmt.replace("{senders}", &senders)).to_string())
    }

    pub fn get_forward_template_signature_style(&self) -> ForwardTemplateSignatureStyle {
        self.template
            .as_ref()
            .and_then(|c| c.forward.as_ref())
            .and_then(|c| c.signature_style.clone())
            .unwrap_or_default()
    }

    pub fn get_forward_template_posting_style(&self) -> ForwardTemplatePostingStyle {
        self.template
            .as_ref()
            .and_then(|c| c.forward.as_ref())
            .and_then(|c| c.posting_style.clone())
            .unwrap_or_default()
    }

    pub fn get_forward_template_quote_headline(&self) -> String {
        self.template
            .as_ref()
            .and_then(|c| c.forward.as_ref())
            .and_then(|c| c.quote_headline.clone())
            .unwrap_or_else(|| String::from("-------- Forwarded Message --------\n"))
    }
}

impl<'a> From<&'a AccountConfig> for Address<'a> {
    fn from(config: &'a AccountConfig) -> Self {
        Address::Address(EmailAddress {
            name: config.display_name.as_ref().map(Into::into),
            email: config.email.as_str().into(),
        })
    }
}

/// Rename duplicated file by adding a auto-incremented counter
/// suffix.
///
/// Helper that check if the given file path already exists: if so,
/// creates a new path with an auto-incremented integer suffix and
/// returs it, otherwise returs the original file path.
pub(crate) fn rename_file_if_duplicate(
    origin_file_path: &Path,
    is_file: impl Fn(&PathBuf, u8) -> bool,
) -> Result<PathBuf> {
    let mut count = 0;

    let mut file_path = origin_file_path.to_owned();
    let file_stem = origin_file_path.file_stem().and_then(OsStr::to_str);
    let file_ext = origin_file_path
        .extension()
        .and_then(OsStr::to_str)
        .map(|fext| String::from(".") + fext)
        .unwrap_or_default();

    while is_file(&file_path, count) {
        count += 1;
        file_path.set_file_name(
            &file_stem
                .map(|fstem| format!("{}_{}{}", fstem, count, file_ext))
                .ok_or_else(|| Error::ParseDownloadFileNameError(file_path.to_owned()))?,
        );
    }

    Ok(file_path)
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    #[test]
    fn rename_file_if_duplicate() {
        let path = PathBuf::from("downloads/file.ext");

        // when file path is unique
        assert!(matches!(
            super::rename_file_if_duplicate(&path, |_, _| false),
            Ok(path) if path == PathBuf::from("downloads/file.ext")
        ));

        // when 1 file path already exist
        assert!(matches!(
            super::rename_file_if_duplicate(&path, |_, count| count <  1),
            Ok(path) if path == PathBuf::from("downloads/file_1.ext")
        ));

        // when 5 file paths already exist
        assert!(matches!(
            super::rename_file_if_duplicate(&path, |_, count| count < 5),
            Ok(path) if path == PathBuf::from("downloads/file_5.ext")
        ));

        // when file path has no extension
        let path = PathBuf::from("downloads/file");
        assert!(matches!(
            super::rename_file_if_duplicate(&path, |_, count| count < 5),
            Ok(path) if path == PathBuf::from("downloads/file_5")
        ));

        // when file path has 2 extensions
        let path = PathBuf::from("downloads/file.ext.ext2");
        assert!(matches!(
            super::rename_file_if_duplicate(&path, |_, count| count < 5),
            Ok(path) if path == PathBuf::from("downloads/file.ext_5.ext2")
        ));
    }
}