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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
//! Module dedicated to backend management.
//!
//! The core concept of this module is the [`Backend`] trait, which is
//! an abstraction over emails manipulation.
//!
//! Then you have the [`BackendConfig`] which represents the
//! backend-specific configuration, mostly used by the
//! [AccountConfiguration](crate::account::config::AccountConfig).

use async_trait::async_trait;
#[allow(unused)]
use std::sync::Arc;
use thiserror::Error;

#[cfg(feature = "envelope-get")]
use crate::envelope::get::GetEnvelope;
#[cfg(feature = "envelope-list")]
use crate::envelope::list::ListEnvelopes;
#[cfg(feature = "envelope-watch")]
use crate::envelope::watch::WatchEnvelopes;
#[cfg(feature = "flag-add")]
use crate::flag::add::AddFlags;
#[cfg(feature = "flag-remove")]
use crate::flag::remove::RemoveFlags;
#[cfg(feature = "flag-set")]
use crate::flag::set::SetFlags;
#[cfg(feature = "folder-add")]
use crate::folder::add::AddFolder;
#[cfg(feature = "folder-delete")]
use crate::folder::delete::DeleteFolder;
#[cfg(feature = "folder-expunge")]
use crate::folder::expunge::ExpungeFolder;
#[cfg(feature = "folder-list")]
use crate::folder::list::ListFolders;
#[cfg(feature = "folder-purge")]
use crate::folder::purge::PurgeFolder;
#[cfg(feature = "message-add")]
use crate::message::add::AddMessage;
#[cfg(feature = "message-copy")]
use crate::message::copy::CopyMessages;
#[cfg(all(feature = "message-delete", feature = "flag-add"))]
use crate::message::delete::default_delete_messages;
#[cfg(feature = "message-delete")]
use crate::message::delete::DeleteMessages;
#[cfg(all(feature = "message-get", feature = "flag-add"))]
use crate::message::get::default_get_messages;
#[cfg(feature = "message-get")]
use crate::message::get::GetMessages;
#[cfg(feature = "message-move")]
use crate::message::move_::MoveMessages;
#[cfg(feature = "message-peek")]
use crate::message::peek::PeekMessages;
#[cfg(feature = "message-send")]
use crate::message::send::SendMessage;
#[allow(unused)]
use crate::{
    account::config::AccountConfig,
    envelope::{Envelope, Envelopes},
    envelope::{Id, SingleId},
    flag::{Flag, Flags},
    folder::Folders,
    message::Messages,
    Result,
};

/// Errors related to backend.
#[derive(Debug, Error)]
pub enum Error {
    #[cfg(feature = "folder-add")]
    #[error("cannot add folder: feature not available")]
    AddFolderNotAvailableError,
    #[cfg(feature = "folder-list")]
    #[error("cannot list folders: feature not available")]
    ListFoldersNotAvailableError,
    #[cfg(feature = "folder-expunge")]
    #[error("cannot expunge folder: feature not available")]
    ExpungeFolderNotAvailableError,
    #[cfg(feature = "folder-purge")]
    #[error("cannot purge folder: feature not available")]
    PurgeFolderNotAvailableError,
    #[cfg(feature = "folder-delete")]
    #[error("cannot delete folder: feature not available")]
    DeleteFolderNotAvailableError,

    #[cfg(feature = "envelope-list")]
    #[error("cannot list envelopes: feature not available")]
    ListEnvelopesNotAvailableError,
    #[cfg(feature = "envelope-watch")]
    #[error("cannot watch for envelopes changes: feature not available")]
    WatchEnvelopesNotAvailableError,
    #[cfg(feature = "envelope-get")]
    #[error("cannot get envelope: feature not available")]
    GetEnvelopeNotAvailableError,

    #[cfg(feature = "flag-add")]
    #[error("cannot add flag(s): feature not available")]
    AddFlagsNotAvailableError,
    #[cfg(feature = "flag-set")]
    #[error("cannot set flag(s): feature not available")]
    SetFlagsNotAvailableError,
    #[cfg(feature = "flag-remove")]
    #[error("cannot remove flag(s): feature not available")]
    RemoveFlagsNotAvailableError,

    #[cfg(feature = "message-add")]
    #[error("cannot add message: feature not available")]
    AddMessageNotAvailableError,
    #[cfg(feature = "message-add")]
    #[error("cannot add message with flags: feature not available")]
    AddMessageWithFlagsNotAvailableError,
    #[cfg(feature = "message-get")]
    #[error("cannot get messages: feature not available")]
    GetMessagesNotAvailableError,
    #[cfg(feature = "message-peek")]
    #[error("cannot peek messages: feature not available")]
    PeekMessagesNotAvailableError,
    #[cfg(feature = "message-copy")]
    #[error("cannot copy messages: feature not available")]
    CopyMessagesNotAvailableError,
    #[cfg(feature = "message-move")]
    #[error("cannot move messages: feature not available")]
    MoveMessagesNotAvailableError,
    #[cfg(feature = "message-delete")]
    #[error("cannot delete messages: feature not available")]
    DeleteMessagesNotAvailableError,
    #[cfg(feature = "message-send")]
    #[error("cannot send message: feature not available")]
    SendMessageNotAvailableError,
}

#[async_trait]
pub trait BackendContextBuilder: Clone + Send + Sync {
    type Context: Send + Sync;

    async fn build(self) -> Result<Self::Context>;
}

#[async_trait]
impl BackendContextBuilder for () {
    type Context = ();

    async fn build(self) -> Result<Self::Context> {
        Ok(())
    }
}

#[async_trait]
impl<T: BackendContextBuilder, U: BackendContextBuilder> BackendContextBuilder for (T, U) {
    type Context = (T::Context, U::Context);

    async fn build(self) -> Result<Self::Context> {
        Ok((self.0.build().await?, self.1.build().await?))
    }
}

#[async_trait]
impl<T: BackendContextBuilder, U: BackendContextBuilder, V: BackendContextBuilder>
    BackendContextBuilder for (T, U, V)
{
    type Context = (T::Context, U::Context, V::Context);

    async fn build(self) -> Result<Self::Context> {
        Ok((
            self.0.build().await?,
            self.1.build().await?,
            self.2.build().await?,
        ))
    }
}

pub struct BackendBuilder<B: BackendContextBuilder> {
    pub account_config: AccountConfig,
    pub context_builder: B,

    #[cfg(feature = "folder-add")]
    add_folder: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn AddFolder>> + Send + Sync>>,
    #[cfg(feature = "folder-list")]
    list_folders: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn ListFolders>> + Send + Sync>>,
    #[cfg(feature = "folder-expunge")]
    expunge_folder:
        Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn ExpungeFolder>> + Send + Sync>>,
    #[cfg(feature = "folder-purge")]
    purge_folder: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn PurgeFolder>> + Send + Sync>>,
    #[cfg(feature = "folder-delete")]
    delete_folder: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn DeleteFolder>> + Send + Sync>>,

    #[cfg(feature = "envelope-list")]
    list_envelopes:
        Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn ListEnvelopes>> + Send + Sync>>,
    #[cfg(feature = "envelope-watch")]
    watch_envelopes:
        Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn WatchEnvelopes>> + Send + Sync>>,
    #[cfg(feature = "envelope-get")]
    get_envelope: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn GetEnvelope>> + Send + Sync>>,

    #[cfg(feature = "flag-add")]
    add_flags: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn AddFlags>> + Send + Sync>>,
    #[cfg(feature = "flag-set")]
    set_flags: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn SetFlags>> + Send + Sync>>,
    #[cfg(feature = "flag-remove")]
    remove_flags: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn RemoveFlags>> + Send + Sync>>,

    #[cfg(feature = "message-add")]
    add_message: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn AddMessage>> + Send + Sync>>,
    #[cfg(feature = "message-peek")]
    peek_messages: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn PeekMessages>> + Send + Sync>>,
    #[cfg(feature = "message-get")]
    get_messages: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn GetMessages>> + Send + Sync>>,
    #[cfg(feature = "message-copy")]
    copy_messages: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn CopyMessages>> + Send + Sync>>,
    #[cfg(feature = "message-move")]
    move_messages: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn MoveMessages>> + Send + Sync>>,
    #[cfg(feature = "message-delete")]
    delete_messages:
        Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn DeleteMessages>> + Send + Sync>>,
    #[cfg(feature = "message-send")]
    send_message: Option<Arc<dyn Fn(&B::Context) -> Option<Box<dyn SendMessage>> + Send + Sync>>,
}

impl<C, B: BackendContextBuilder<Context = C>> BackendBuilder<B> {
    pub fn new(account_config: AccountConfig, context_builder: B) -> Self {
        Self {
            account_config,
            context_builder,

            #[cfg(feature = "folder-add")]
            add_folder: Default::default(),
            #[cfg(feature = "folder-list")]
            list_folders: Default::default(),
            #[cfg(feature = "folder-expunge")]
            expunge_folder: Default::default(),
            #[cfg(feature = "folder-purge")]
            purge_folder: Default::default(),
            #[cfg(feature = "folder-delete")]
            delete_folder: Default::default(),

            #[cfg(feature = "envelope-list")]
            list_envelopes: Default::default(),
            #[cfg(feature = "envelope-watch")]
            watch_envelopes: Default::default(),
            #[cfg(feature = "envelope-get")]
            get_envelope: Default::default(),

            #[cfg(feature = "flag-add")]
            add_flags: Default::default(),
            #[cfg(feature = "flag-set")]
            set_flags: Default::default(),
            #[cfg(feature = "flag-remove")]
            remove_flags: Default::default(),

            #[cfg(feature = "message-add")]
            add_message: Default::default(),
            #[cfg(feature = "message-peek")]
            peek_messages: Default::default(),
            #[cfg(feature = "message-get")]
            get_messages: Default::default(),
            #[cfg(feature = "message-copy")]
            copy_messages: Default::default(),
            #[cfg(feature = "message-move")]
            move_messages: Default::default(),
            #[cfg(feature = "message-delete")]
            delete_messages: Default::default(),
            #[cfg(feature = "message-send")]
            send_message: Default::default(),
        }
    }

    #[cfg(feature = "folder-add")]
    pub fn set_add_folder(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn AddFolder>> + Send + Sync + 'static,
    ) {
        self.add_folder = Some(Arc::new(feature));
    }
    #[cfg(feature = "folder-add")]
    pub fn with_add_folder(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn AddFolder>> + Send + Sync + 'static,
    ) -> Self {
        self.set_add_folder(feature);
        self
    }

    #[cfg(feature = "folder-list")]
    pub fn set_list_folders(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn ListFolders>> + Send + Sync + 'static,
    ) {
        self.list_folders = Some(Arc::new(feature));
    }
    #[cfg(feature = "folder-list")]
    pub fn with_list_folders(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn ListFolders>> + Send + Sync + 'static,
    ) -> Self {
        self.set_list_folders(feature);
        self
    }

    #[cfg(feature = "folder-expunge")]
    pub fn set_expunge_folder(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn ExpungeFolder>> + Send + Sync + 'static,
    ) {
        self.expunge_folder = Some(Arc::new(feature));
    }
    #[cfg(feature = "folder-expunge")]
    pub fn with_expunge_folder(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn ExpungeFolder>> + Send + Sync + 'static,
    ) -> Self {
        self.set_expunge_folder(feature);
        self
    }

    #[cfg(feature = "folder-purge")]
    pub fn set_purge_folder(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn PurgeFolder>> + Send + Sync + 'static,
    ) {
        self.purge_folder = Some(Arc::new(feature));
    }
    #[cfg(feature = "folder-purge")]
    pub fn with_purge_folder(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn PurgeFolder>> + Send + Sync + 'static,
    ) -> Self {
        self.set_purge_folder(feature);
        self
    }

    #[cfg(feature = "folder-delete")]
    pub fn set_delete_folder(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn DeleteFolder>> + Send + Sync + 'static,
    ) {
        self.delete_folder = Some(Arc::new(feature));
    }
    #[cfg(feature = "folder-delete")]
    pub fn with_delete_folder(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn DeleteFolder>> + Send + Sync + 'static,
    ) -> Self {
        self.set_delete_folder(feature);
        self
    }

    #[cfg(feature = "envelope-list")]
    pub fn set_list_envelopes(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn ListEnvelopes>> + Send + Sync + 'static,
    ) {
        self.list_envelopes = Some(Arc::new(feature));
    }
    #[cfg(feature = "envelope-list")]
    pub fn with_list_envelopes(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn ListEnvelopes>> + Send + Sync + 'static,
    ) -> Self {
        self.set_list_envelopes(feature);
        self
    }

    #[cfg(feature = "envelope-watch")]
    pub fn set_watch_envelopes(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn WatchEnvelopes>> + Send + Sync + 'static,
    ) {
        self.watch_envelopes = Some(Arc::new(feature));
    }
    #[cfg(feature = "envelope-watch")]
    pub fn with_watch_envelopes(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn WatchEnvelopes>> + Send + Sync + 'static,
    ) -> Self {
        self.set_watch_envelopes(feature);
        self
    }

    #[cfg(feature = "envelope-get")]
    pub fn set_get_envelope(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn GetEnvelope>> + Send + Sync + 'static,
    ) {
        self.get_envelope = Some(Arc::new(feature));
    }
    #[cfg(feature = "envelope-get")]
    pub fn with_get_envelope(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn GetEnvelope>> + Send + Sync + 'static,
    ) -> Self {
        self.set_get_envelope(feature);
        self
    }

    #[cfg(feature = "flag-add")]
    pub fn set_add_flags(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn AddFlags>> + Send + Sync + 'static,
    ) {
        self.add_flags = Some(Arc::new(feature));
    }
    #[cfg(feature = "flag-add")]
    pub fn with_add_flags(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn AddFlags>> + Send + Sync + 'static,
    ) -> Self {
        self.set_add_flags(feature);
        self
    }

    #[cfg(feature = "flag-set")]
    pub fn set_set_flags(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn SetFlags>> + Send + Sync + 'static,
    ) {
        self.set_flags = Some(Arc::new(feature));
    }
    #[cfg(feature = "flag-set")]
    pub fn with_set_flags(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn SetFlags>> + Send + Sync + 'static,
    ) -> Self {
        self.set_set_flags(feature);
        self
    }

    #[cfg(feature = "flag-remove")]
    pub fn set_remove_flags(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn RemoveFlags>> + Send + Sync + 'static,
    ) {
        self.remove_flags = Some(Arc::new(feature));
    }
    #[cfg(feature = "flag-remove")]
    pub fn with_remove_flags(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn RemoveFlags>> + Send + Sync + 'static,
    ) -> Self {
        self.set_remove_flags(feature);
        self
    }

    #[cfg(feature = "message-add")]
    pub fn set_add_message(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn AddMessage>> + Send + Sync + 'static,
    ) {
        self.add_message = Some(Arc::new(feature));
    }
    #[cfg(feature = "message-add")]
    pub fn with_add_message(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn AddMessage>> + Send + Sync + 'static,
    ) -> Self {
        self.set_add_message(feature);
        self
    }

    #[cfg(feature = "message-peek")]
    pub fn set_peek_messages(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn PeekMessages>> + Send + Sync + 'static,
    ) {
        self.peek_messages = Some(Arc::new(feature));
    }
    #[cfg(feature = "message-peek")]
    pub fn with_peek_messages(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn PeekMessages>> + Send + Sync + 'static,
    ) -> Self {
        self.set_peek_messages(feature);
        self
    }

    #[cfg(feature = "message-get")]
    pub fn set_get_messages(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn GetMessages>> + Send + Sync + 'static,
    ) {
        self.get_messages = Some(Arc::new(feature));
    }
    #[cfg(feature = "message-get")]
    pub fn with_get_messages(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn GetMessages>> + Send + Sync + 'static,
    ) -> Self {
        self.set_get_messages(feature);
        self
    }

    #[cfg(feature = "message-copy")]
    pub fn set_copy_messages(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn CopyMessages>> + Send + Sync + 'static,
    ) {
        self.copy_messages = Some(Arc::new(feature));
    }
    #[cfg(feature = "message-copy")]
    pub fn with_copy_messages(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn CopyMessages>> + Send + Sync + 'static,
    ) -> Self {
        self.set_copy_messages(feature);
        self
    }

    #[cfg(feature = "message-move")]
    pub fn set_move_messages(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn MoveMessages>> + Send + Sync + 'static,
    ) {
        self.move_messages = Some(Arc::new(feature));
    }
    #[cfg(feature = "message-move")]
    pub fn with_move_messages(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn MoveMessages>> + Send + Sync + 'static,
    ) -> Self {
        self.set_move_messages(feature);
        self
    }

    #[cfg(feature = "message-delete")]
    pub fn set_delete_messages(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn DeleteMessages>> + Send + Sync + 'static,
    ) {
        self.delete_messages = Some(Arc::new(feature));
    }
    #[cfg(feature = "message-delete")]
    pub fn with_delete_messages(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn DeleteMessages>> + Send + Sync + 'static,
    ) -> Self {
        self.set_delete_messages(feature);
        self
    }

    #[cfg(feature = "message-send")]
    pub fn set_send_message(
        &mut self,
        feature: impl Fn(&C) -> Option<Box<dyn SendMessage>> + Send + Sync + 'static,
    ) {
        self.send_message = Some(Arc::new(feature));
    }
    #[cfg(feature = "message-send")]
    pub fn with_send_message(
        mut self,
        feature: impl Fn(&C) -> Option<Box<dyn SendMessage>> + Send + Sync + 'static,
    ) -> Self {
        self.set_send_message(feature);
        self
    }

    pub async fn build(self) -> Result<Backend<C>> {
        let context = self.context_builder.build().await?;
        #[allow(unused_mut)]
        let mut backend = Backend::new(self.account_config.clone(), context);

        #[cfg(feature = "folder-add")]
        if let Some(feature) = &self.add_folder {
            backend.set_add_folder(feature(&backend.context));
        }
        #[cfg(feature = "folder-list")]
        if let Some(feature) = &self.list_folders {
            backend.set_list_folders(feature(&backend.context));
        }
        #[cfg(feature = "folder-expunge")]
        if let Some(feature) = &self.expunge_folder {
            backend.set_expunge_folder(feature(&backend.context));
        }
        #[cfg(feature = "folder-purge")]
        if let Some(feature) = &self.purge_folder {
            backend.set_purge_folder(feature(&backend.context));
        }
        #[cfg(feature = "folder-delete")]
        if let Some(feature) = &self.delete_folder {
            backend.set_delete_folder(feature(&backend.context));
        }

        #[cfg(feature = "envelope-list")]
        if let Some(feature) = &self.list_envelopes {
            backend.set_list_envelopes(feature(&backend.context));
        }
        #[cfg(feature = "envelope-watch")]
        if let Some(feature) = &self.watch_envelopes {
            backend.set_watch_envelopes(feature(&backend.context));
        }
        #[cfg(feature = "envelope-get")]
        if let Some(feature) = &self.get_envelope {
            backend.set_get_envelope(feature(&backend.context));
        }

        #[cfg(feature = "flag-add")]
        if let Some(feature) = &self.add_flags {
            backend.set_add_flags(feature(&backend.context));
        }
        #[cfg(feature = "flag-set")]
        if let Some(feature) = &self.set_flags {
            backend.set_set_flags(feature(&backend.context));
        }
        #[cfg(feature = "flag-remove")]
        if let Some(feature) = &self.remove_flags {
            backend.set_remove_flags(feature(&backend.context));
        }

        #[cfg(feature = "message-add")]
        if let Some(feature) = &self.add_message {
            backend.set_add_message(feature(&backend.context));
        }
        #[cfg(feature = "message-get")]
        if let Some(feature) = &self.get_messages {
            backend.set_get_messages(feature(&backend.context));
        }
        #[cfg(feature = "message-peek")]
        if let Some(feature) = &self.peek_messages {
            backend.set_peek_messages(feature(&backend.context));
        }
        #[cfg(feature = "message-copy")]
        if let Some(feature) = &self.copy_messages {
            backend.set_copy_messages(feature(&backend.context));
        }
        #[cfg(feature = "message-move")]
        if let Some(feature) = &self.move_messages {
            backend.set_move_messages(feature(&backend.context));
        }
        #[cfg(feature = "message-delete")]
        if let Some(feature) = &self.delete_messages {
            backend.set_delete_messages(feature(&backend.context));
        }
        #[cfg(feature = "message-send")]
        if let Some(feature) = self.send_message {
            backend.set_send_message(feature(&backend.context));
        }

        Ok(backend)
    }
}

impl<B: BackendContextBuilder> Clone for BackendBuilder<B> {
    fn clone(&self) -> Self {
        Self {
            context_builder: self.context_builder.clone(),

            account_config: self.account_config.clone(),

            #[cfg(feature = "folder-add")]
            add_folder: self.add_folder.clone(),
            #[cfg(feature = "folder-list")]
            list_folders: self.list_folders.clone(),
            #[cfg(feature = "folder-expunge")]
            expunge_folder: self.expunge_folder.clone(),
            #[cfg(feature = "folder-purge")]
            purge_folder: self.purge_folder.clone(),
            #[cfg(feature = "folder-delete")]
            delete_folder: self.delete_folder.clone(),

            #[cfg(feature = "envelope-list")]
            list_envelopes: self.list_envelopes.clone(),
            #[cfg(feature = "envelope-watch")]
            watch_envelopes: self.watch_envelopes.clone(),
            #[cfg(feature = "envelope-get")]
            get_envelope: self.get_envelope.clone(),

            #[cfg(feature = "flag-add")]
            add_flags: self.add_flags.clone(),
            #[cfg(feature = "flag-set")]
            set_flags: self.set_flags.clone(),
            #[cfg(feature = "flag-remove")]
            remove_flags: self.remove_flags.clone(),

            #[cfg(feature = "message-add")]
            add_message: self.add_message.clone(),
            #[cfg(feature = "message-peek")]
            peek_messages: self.peek_messages.clone(),
            #[cfg(feature = "message-get")]
            get_messages: self.get_messages.clone(),
            #[cfg(feature = "message-copy")]
            copy_messages: self.copy_messages.clone(),
            #[cfg(feature = "message-move")]
            move_messages: self.move_messages.clone(),
            #[cfg(feature = "message-delete")]
            delete_messages: self.delete_messages.clone(),
            #[cfg(feature = "message-send")]
            send_message: self.send_message.clone(),
        }
    }
}

impl Default for BackendBuilder<()> {
    fn default() -> Self {
        Self {
            context_builder: (),

            account_config: Default::default(),

            #[cfg(feature = "folder-add")]
            add_folder: Default::default(),
            #[cfg(feature = "folder-list")]
            list_folders: Default::default(),
            #[cfg(feature = "folder-expunge")]
            expunge_folder: Default::default(),
            #[cfg(feature = "folder-purge")]
            purge_folder: Default::default(),
            #[cfg(feature = "folder-delete")]
            delete_folder: Default::default(),

            #[cfg(feature = "envelope-list")]
            list_envelopes: Default::default(),
            #[cfg(feature = "envelope-watch")]
            watch_envelopes: Default::default(),
            #[cfg(feature = "envelope-get")]
            get_envelope: Default::default(),

            #[cfg(feature = "flag-add")]
            add_flags: Default::default(),
            #[cfg(feature = "flag-set")]
            set_flags: Default::default(),
            #[cfg(feature = "flag-remove")]
            remove_flags: Default::default(),

            #[cfg(feature = "message-add")]
            add_message: Default::default(),
            #[cfg(feature = "message-peek")]
            peek_messages: Default::default(),
            #[cfg(feature = "message-get")]
            get_messages: Default::default(),
            #[cfg(feature = "message-copy")]
            copy_messages: Default::default(),
            #[cfg(feature = "message-move")]
            move_messages: Default::default(),
            #[cfg(feature = "message-delete")]
            delete_messages: Default::default(),
            #[cfg(feature = "message-send")]
            send_message: Default::default(),
        }
    }
}

pub struct Backend<C> {
    #[allow(dead_code)]
    context: C,

    pub account_config: AccountConfig,

    #[cfg(feature = "folder-add")]
    pub add_folder: Option<Box<dyn AddFolder>>,
    #[cfg(feature = "folder-list")]
    pub list_folders: Option<Box<dyn ListFolders>>,
    #[cfg(feature = "folder-expunge")]
    pub expunge_folder: Option<Box<dyn ExpungeFolder>>,
    #[cfg(feature = "folder-purge")]
    pub purge_folder: Option<Box<dyn PurgeFolder>>,
    #[cfg(feature = "folder-delete")]
    pub delete_folder: Option<Box<dyn DeleteFolder>>,

    #[cfg(feature = "envelope-list")]
    pub list_envelopes: Option<Box<dyn ListEnvelopes>>,
    #[cfg(feature = "envelope-watch")]
    pub watch_envelopes: Option<Box<dyn WatchEnvelopes>>,
    #[cfg(feature = "envelope-get")]
    pub get_envelope: Option<Box<dyn GetEnvelope>>,

    #[cfg(feature = "flag-add")]
    pub add_flags: Option<Box<dyn AddFlags>>,
    #[cfg(feature = "flag-set")]
    pub set_flags: Option<Box<dyn SetFlags>>,
    #[cfg(feature = "flag-remove")]
    pub remove_flags: Option<Box<dyn RemoveFlags>>,

    #[cfg(feature = "message-add")]
    pub add_message: Option<Box<dyn AddMessage>>,
    #[cfg(feature = "message-peek")]
    pub peek_messages: Option<Box<dyn PeekMessages>>,
    #[cfg(feature = "message-get")]
    pub get_messages: Option<Box<dyn GetMessages>>,
    #[cfg(feature = "message-copy")]
    pub copy_messages: Option<Box<dyn CopyMessages>>,
    #[cfg(feature = "message-move")]
    pub move_messages: Option<Box<dyn MoveMessages>>,
    #[cfg(feature = "message-delete")]
    pub delete_messages: Option<Box<dyn DeleteMessages>>,
    #[cfg(feature = "message-send")]
    pub send_message: Option<Box<dyn SendMessage>>,
}

impl<C> Backend<C> {
    pub fn new(account_config: AccountConfig, context: C) -> Backend<C> {
        Backend {
            context,

            account_config,

            #[cfg(feature = "folder-add")]
            add_folder: None,
            #[cfg(feature = "folder-list")]
            list_folders: None,
            #[cfg(feature = "folder-expunge")]
            expunge_folder: None,
            #[cfg(feature = "folder-purge")]
            purge_folder: None,
            #[cfg(feature = "folder-delete")]
            delete_folder: None,

            #[cfg(feature = "envelope-list")]
            list_envelopes: None,
            #[cfg(feature = "envelope-watch")]
            watch_envelopes: None,
            #[cfg(feature = "envelope-get")]
            get_envelope: None,

            #[cfg(feature = "flag-add")]
            add_flags: None,
            #[cfg(feature = "flag-set")]
            set_flags: None,
            #[cfg(feature = "flag-remove")]
            remove_flags: None,

            #[cfg(feature = "message-add")]
            add_message: None,
            #[cfg(feature = "message-peek")]
            peek_messages: None,
            #[cfg(feature = "message-get")]
            get_messages: None,
            #[cfg(feature = "message-copy")]
            copy_messages: None,
            #[cfg(feature = "message-move")]
            move_messages: None,
            #[cfg(feature = "message-delete")]
            delete_messages: None,
            #[cfg(feature = "message-send")]
            send_message: None,
        }
    }

    #[cfg(feature = "folder-add")]
    pub fn set_add_folder(&mut self, feature: Option<Box<dyn AddFolder>>) {
        self.add_folder = feature;
    }
    #[cfg(feature = "folder-list")]
    pub fn set_list_folders(&mut self, feature: Option<Box<dyn ListFolders>>) {
        self.list_folders = feature;
    }
    #[cfg(feature = "folder-expunge")]
    pub fn set_expunge_folder(&mut self, feature: Option<Box<dyn ExpungeFolder>>) {
        self.expunge_folder = feature;
    }
    #[cfg(feature = "folder-purge")]
    pub fn set_purge_folder(&mut self, feature: Option<Box<dyn PurgeFolder>>) {
        self.purge_folder = feature;
    }
    #[cfg(feature = "folder-delete")]
    pub fn set_delete_folder(&mut self, feature: Option<Box<dyn DeleteFolder>>) {
        self.delete_folder = feature;
    }

    #[cfg(feature = "envelope-list")]
    pub fn set_list_envelopes(&mut self, feature: Option<Box<dyn ListEnvelopes>>) {
        self.list_envelopes = feature;
    }
    #[cfg(feature = "envelope-watch")]
    pub fn set_watch_envelopes(&mut self, feature: Option<Box<dyn WatchEnvelopes>>) {
        self.watch_envelopes = feature;
    }
    #[cfg(feature = "envelope-get")]
    pub fn set_get_envelope(&mut self, feature: Option<Box<dyn GetEnvelope>>) {
        self.get_envelope = feature;
    }

    #[cfg(feature = "flag-add")]
    pub fn set_add_flags(&mut self, feature: Option<Box<dyn AddFlags>>) {
        self.add_flags = feature;
    }
    #[cfg(feature = "flag-set")]
    pub fn set_set_flags(&mut self, feature: Option<Box<dyn SetFlags>>) {
        self.set_flags = feature;
    }
    #[cfg(feature = "flag-remove")]
    pub fn set_remove_flags(&mut self, feature: Option<Box<dyn RemoveFlags>>) {
        self.remove_flags = feature;
    }

    #[cfg(feature = "message-add")]
    pub fn set_add_message(&mut self, feature: Option<Box<dyn AddMessage>>) {
        self.add_message = feature;
    }
    #[cfg(feature = "message-peek")]
    pub fn set_peek_messages(&mut self, feature: Option<Box<dyn PeekMessages>>) {
        self.peek_messages = feature;
    }
    #[cfg(feature = "message-get")]
    pub fn set_get_messages(&mut self, feature: Option<Box<dyn GetMessages>>) {
        self.get_messages = feature;
    }
    #[cfg(feature = "message-copy")]
    pub fn set_copy_messages(&mut self, feature: Option<Box<dyn CopyMessages>>) {
        self.copy_messages = feature;
    }
    #[cfg(feature = "message-move")]
    pub fn set_move_messages(&mut self, feature: Option<Box<dyn MoveMessages>>) {
        self.move_messages = feature;
    }
    #[cfg(feature = "message-delete")]
    pub fn set_delete_messages(&mut self, feature: Option<Box<dyn DeleteMessages>>) {
        self.delete_messages = feature;
    }
    #[cfg(feature = "message-send")]
    pub fn set_send_message(&mut self, feature: Option<Box<dyn SendMessage>>) {
        self.send_message = feature;
    }

    #[cfg(feature = "folder-add")]
    pub async fn add_folder(&self, folder: &str) -> Result<()> {
        self.add_folder
            .as_ref()
            .ok_or(Error::AddFolderNotAvailableError)?
            .add_folder(folder)
            .await
    }

    #[cfg(feature = "folder-list")]
    pub async fn list_folders(&self) -> Result<Folders> {
        self.list_folders
            .as_ref()
            .ok_or(Error::ListFoldersNotAvailableError)?
            .list_folders()
            .await
    }

    #[cfg(feature = "folder-expunge")]
    pub async fn expunge_folder(&self, folder: &str) -> Result<()> {
        self.expunge_folder
            .as_ref()
            .ok_or(Error::ExpungeFolderNotAvailableError)?
            .expunge_folder(folder)
            .await
    }

    #[cfg(feature = "folder-purge")]
    pub async fn purge_folder(&self, folder: &str) -> Result<()> {
        self.purge_folder
            .as_ref()
            .ok_or(Error::PurgeFolderNotAvailableError)?
            .purge_folder(folder)
            .await
    }

    #[cfg(feature = "folder-delete")]
    pub async fn delete_folder(&self, folder: &str) -> Result<()> {
        self.delete_folder
            .as_ref()
            .ok_or(Error::DeleteFolderNotAvailableError)?
            .delete_folder(folder)
            .await
    }

    #[cfg(feature = "envelope-list")]
    pub async fn list_envelopes(
        &self,
        folder: &str,
        page_size: usize,
        page: usize,
    ) -> Result<Envelopes> {
        self.list_envelopes
            .as_ref()
            .ok_or(Error::ListEnvelopesNotAvailableError)?
            .list_envelopes(folder, page_size, page)
            .await
    }

    #[cfg(feature = "envelope-watch")]
    pub async fn watch_envelopes(&self, folder: &str) -> Result<()> {
        self.watch_envelopes
            .as_ref()
            .ok_or(Error::WatchEnvelopesNotAvailableError)?
            .watch_envelopes(folder)
            .await
    }

    #[cfg(feature = "envelope-get")]
    pub async fn get_envelope(&self, folder: &str, id: &Id) -> Result<Envelope> {
        self.get_envelope
            .as_ref()
            .ok_or(Error::GetEnvelopeNotAvailableError)?
            .get_envelope(folder, id)
            .await
    }

    #[cfg(feature = "flag-add")]
    pub async fn add_flags(&self, folder: &str, id: &Id, flags: &Flags) -> Result<()> {
        self.add_flags
            .as_ref()
            .ok_or(Error::AddFlagsNotAvailableError)?
            .add_flags(folder, id, flags)
            .await
    }

    #[cfg(feature = "flag-add")]
    pub async fn add_flag(&self, folder: &str, id: &Id, flag: Flag) -> Result<()> {
        self.add_flags
            .as_ref()
            .ok_or(Error::AddFlagsNotAvailableError)?
            .add_flag(folder, id, flag)
            .await
    }

    #[cfg(feature = "flag-set")]
    pub async fn set_flags(&self, folder: &str, id: &Id, flags: &Flags) -> Result<()> {
        self.set_flags
            .as_ref()
            .ok_or(Error::SetFlagsNotAvailableError)?
            .set_flags(folder, id, flags)
            .await
    }

    #[cfg(feature = "flag-set")]
    pub async fn set_flag(&self, folder: &str, id: &Id, flag: Flag) -> Result<()> {
        self.set_flags
            .as_ref()
            .ok_or(Error::SetFlagsNotAvailableError)?
            .set_flag(folder, id, flag)
            .await
    }

    #[cfg(feature = "flag-remove")]
    pub async fn remove_flags(&self, folder: &str, id: &Id, flags: &Flags) -> Result<()> {
        self.remove_flags
            .as_ref()
            .ok_or(Error::RemoveFlagsNotAvailableError)?
            .remove_flags(folder, id, flags)
            .await
    }

    #[cfg(feature = "flag-remove")]
    pub async fn remove_flag(&self, folder: &str, id: &Id, flag: Flag) -> Result<()> {
        self.remove_flags
            .as_ref()
            .ok_or(Error::RemoveFlagsNotAvailableError)?
            .remove_flag(folder, id, flag)
            .await
    }

    #[cfg(feature = "message-add")]
    pub async fn add_message_with_flags(
        &self,
        folder: &str,
        raw_msg: &[u8],
        flags: &Flags,
    ) -> Result<SingleId> {
        self.add_message
            .as_ref()
            .ok_or(Error::AddMessageWithFlagsNotAvailableError)?
            .add_message_with_flags(folder, raw_msg, flags)
            .await
    }

    #[cfg(feature = "message-add")]
    pub async fn add_message_with_flag(
        &self,
        folder: &str,
        raw_msg: &[u8],
        flag: Flag,
    ) -> Result<SingleId> {
        self.add_message
            .as_ref()
            .ok_or(Error::AddMessageWithFlagsNotAvailableError)?
            .add_message_with_flag(folder, raw_msg, flag)
            .await
    }

    #[cfg(feature = "message-add")]
    pub async fn add_message(&self, folder: &str, raw_msg: &[u8]) -> Result<SingleId> {
        self.add_message
            .as_ref()
            .ok_or(Error::AddMessageWithFlagsNotAvailableError)?
            .add_message(folder, raw_msg)
            .await
    }

    #[cfg(feature = "message-peek")]
    pub async fn peek_messages(&self, folder: &str, id: &Id) -> Result<Messages> {
        self.peek_messages
            .as_ref()
            .ok_or(Error::PeekMessagesNotAvailableError)?
            .peek_messages(folder, id)
            .await
    }

    #[cfg(feature = "message-get")]
    pub async fn get_messages(&self, folder: &str, id: &Id) -> Result<Messages> {
        if let Some(f) = self.get_messages.as_ref() {
            return f.get_messages(folder, id).await;
        }

        #[cfg(all(feature = "message-peek", feature = "flag-add"))]
        if let (Some(a), Some(b)) = (self.peek_messages.as_ref(), self.add_flags.as_ref()) {
            return default_get_messages(a.as_ref(), b.as_ref(), folder, id).await;
        }

        Err(Error::PeekMessagesNotAvailableError.into())
    }

    #[cfg(feature = "message-copy")]
    pub async fn copy_messages(&self, from_folder: &str, to_folder: &str, id: &Id) -> Result<()> {
        self.copy_messages
            .as_ref()
            .ok_or(Error::CopyMessagesNotAvailableError)?
            .copy_messages(from_folder, to_folder, id)
            .await
    }

    #[cfg(feature = "message-move")]
    pub async fn move_messages(&self, from_folder: &str, to_folder: &str, id: &Id) -> Result<()> {
        self.move_messages
            .as_ref()
            .ok_or(Error::MoveMessagesNotAvailableError)?
            .move_messages(from_folder, to_folder, id)
            .await
    }

    #[cfg(feature = "message-delete")]
    pub async fn delete_messages(&self, folder: &str, id: &Id) -> Result<()> {
        if let Some(f) = self.delete_messages.as_ref() {
            return f.delete_messages(folder, id).await;
        }

        #[cfg(all(feature = "message-move", feature = "flag-add"))]
        if let (Some(a), Some(b)) = (self.move_messages.as_ref(), self.add_flags.as_ref()) {
            return default_delete_messages(
                &self.account_config,
                a.as_ref(),
                b.as_ref(),
                folder,
                id,
            )
            .await;
        }

        Err(Error::DeleteMessagesNotAvailableError.into())
    }

    #[cfg(feature = "message-send")]
    pub async fn send_message(&self, raw_msg: &[u8]) -> Result<()> {
        self.send_message
            .as_ref()
            .ok_or(Error::SendMessageNotAvailableError)?
            .send_message(raw_msg)
            .await?;

        #[cfg(feature = "message-add")]
        if self.account_config.should_save_copy_sent_message() {
            let folder = self.account_config.get_sent_folder_alias();
            log::debug!("saving copy of sent message to {folder}");
            self.add_message_with_flag(&folder, raw_msg, Flag::Seen)
                .await?;
        }

        Ok(())
    }
}