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
//! # Backend feature
//!
//! A [`BackendFeature`] is an action like adding folder, listing
//! envelopes or sending message. A feature needs a backend context to
//! be executed.

use async_trait::async_trait;
use std::sync::Arc;

use crate::{
    account::config::HasAccountConfig,
    envelope::{get::GetEnvelope, list::ListEnvelopes, watch::WatchEnvelopes},
    flag::{add::AddFlags, remove::RemoveFlags, set::SetFlags},
    folder::{
        add::AddFolder, delete::DeleteFolder, expunge::ExpungeFolder, list::ListFolders,
        purge::PurgeFolder,
    },
    message::{
        add::AddMessage, copy::CopyMessages, delete::DeleteMessages, get::GetMessages,
        peek::PeekMessages, r#move::MoveMessages, send::SendMessage,
    },
};

use super::{context::BackendContext, AnyResult};

/// Backend builder feature for checking up configuration and context
/// integrity.
///
/// This feature is used to check the integrity of the context.
#[async_trait]
pub trait CheckUp: Send + Sync {
    /// Define how the no operation should be executed.
    async fn check_up(&self) -> AnyResult<()> {
        Ok(())
    }
}

/// The backend feature.
///
/// A backend feature is a function that takes a reference to a
/// backend context as parameter and returns a feature.
pub type BackendFeature<C, F> = Arc<dyn Fn(&C) -> Option<Box<F>> + Send + Sync>;

/// The backend feature source.
///
/// This enum is used by the backend builder to determine where a
/// specific backend feature should be taken from.
#[derive(Default)]
pub enum BackendFeatureSource<C: BackendContext, F: ?Sized> {
    /// The feature should be disabled.
    None,

    /// The feature should be taken from the
    /// [`super::BackendContextBuilder`].
    #[default]
    Context,

    /// The feature should be taken from the
    /// [`super::BackendBuilder`], using the given feature.
    Backend(BackendFeature<C, F>),
}

impl<C, F> Clone for BackendFeatureSource<C, F>
where
    C: BackendContext,
    F: ?Sized,
{
    fn clone(&self) -> Self {
        match self {
            Self::None => Self::None,
            Self::Context => Self::Context,
            Self::Backend(f) => Self::Backend(f.clone()),
        }
    }
}

impl<C, F, T> From<T> for BackendFeatureSource<C, F>
where
    C: BackendContext,
    F: ?Sized,
    T: Fn(&C) -> Option<Box<F>> + Send + Sync + 'static,
{
    fn from(value: T) -> Self {
        Self::Backend(Arc::new(value))
    }
}

/// The backend features supertrait.
///
/// This trait is just an alias for all existing backend features.
pub trait BackendFeatures:
    HasAccountConfig
    + AddFolder
    + ListFolders
    + ExpungeFolder
    + PurgeFolder
    + DeleteFolder
    + GetEnvelope
    + ListEnvelopes
    + WatchEnvelopes
    + AddFlags
    + SetFlags
    + RemoveFlags
    + AddMessage
    + SendMessage
    + PeekMessages
    + GetMessages
    + CopyMessages
    + MoveMessages
    + DeleteMessages
{
}

/// Automatically implement [`BackendFeatures`] for structures
/// implementing all existing backend features.
impl<T> BackendFeatures for T where
    T: HasAccountConfig
        + AddFolder
        + ListFolders
        + ExpungeFolder
        + PurgeFolder
        + DeleteFolder
        + GetEnvelope
        + ListEnvelopes
        + WatchEnvelopes
        + AddFlags
        + SetFlags
        + RemoveFlags
        + AddMessage
        + SendMessage
        + PeekMessages
        + GetMessages
        + CopyMessages
        + MoveMessages
        + DeleteMessages
{
}

/// The backend implementation builder.
///
/// This trait defines how to build a backend implementation from a
/// [`BackendFeatures`] implementation.
#[async_trait]
pub trait AsyncTryIntoBackendFeatures<B>
where
    B: BackendFeatures,
{
    async fn try_into_backend(self) -> AnyResult<B>;
}