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
use super::Status;
use rayon::iter::plumbing::{Folder, UnindexedProducer};
use std::{collections::HashMap, time::Duration};
use thiserror::Error;

#[derive(Error, Debug, Clone)]
pub enum RequestFailure {
    #[error("General - {0}")]
    General(String),
    #[error("Rejected - {0}")]
    Rejected(String),
}

#[derive(Debug, Copy, Clone)]
pub enum RequestSendMode {
    Sync,
    Async,
}

pub type Id = String;

pub struct Request {
    pub ids: Vec<Option<Id>>,
    pub duration: Duration,
}

/// Not a type alias because the split method is interpreted differently
/// and we make assumptions about that which we don't want to make for the general
/// type
pub trait RequestGenerator: Send + Sized {
    fn split(self) -> (Self, Option<Self>);
    fn next(&mut self) -> Result<Request, RequestFailure>;
}

pub struct RayonWrapper<T>(T);

impl<T> From<T> for RayonWrapper<T> {
    fn from(other: T) -> Self {
        Self(other)
    }
}

impl<T: RequestGenerator> UnindexedProducer for RayonWrapper<T> {
    type Item = Result<Request, RequestFailure>;
    fn split(self) -> (Self, Option<Self>) {
        let (a, b) = self.0.split();
        (Self(a), b.map(Self))
    }

    fn fold_with<F>(mut self, mut folder: F) -> F
    where
        F: Folder<Self::Item>,
    {
        while !folder.full() {
            folder = folder.consume(self.0.next());
        }
        folder
    }
}

pub trait RequestStatusProvider {
    fn get_status(&self) -> HashMap<String, RequestStatus>;
}

#[derive(Debug, Clone)]
pub struct Response {
    id: Option<Id>,
    failure: Option<RequestFailure>,
    status: RequestStatus,
    duration: Duration,
}

impl Response {
    pub fn success(id: Option<Id>, duration: Duration) -> Self {
        Self {
            id,
            failure: None,
            status: RequestStatus::Success,
            duration,
        }
    }

    pub fn duration(&self) -> &Duration {
        &self.duration
    }

    pub fn id(&self) -> &Option<Id> {
        &self.id
    }

    pub fn is_pending(&self) -> bool {
        self.status().is_pending()
    }

    pub fn is_success(&self) -> bool {
        self.status().is_success()
    }

    pub fn is_failed(&self) -> bool {
        self.status().is_failed()
    }

    pub fn status(&self) -> &RequestStatus {
        &self.status
    }

    pub fn has_id(&self, other: &str) -> bool {
        if let Some(id) = &self.id {
            return *id == *other;
        }
        false
    }

    pub fn failure(id: Option<Id>, failure: RequestFailure, duration: Duration) -> Self {
        Self {
            id,
            failure: Some(failure.clone()),
            status: RequestStatus::Failed {
                message: failure.to_string(),
            },
            duration,
        }
    }

    pub fn pending(id: Option<Id>, duration: Duration) -> Self {
        Self {
            id,
            failure: None,
            status: RequestStatus::Pending,
            duration,
        }
    }

    pub fn into_failure(self, duration: Duration) -> Self {
        Self {
            id: self.id.clone(),
            failure: self.failure.clone(),
            status: RequestStatus::Failed {
                message: self.err().as_ref().unwrap().to_string(),
            },
            duration: self.duration + duration,
        }
    }

    pub fn into_success(self, duration: Duration) -> Self {
        Self {
            id: self.id,
            failure: None,
            status: RequestStatus::Success,
            duration: self.duration + duration,
        }
    }

    pub fn new_from_status(self, status: Status) -> Self {
        Self {
            id: self.id.clone(),
            failure: status.failure(),
            status: status.status().clone(),
            duration: *self.duration() + *status.duration(),
        }
    }

    pub fn update_status(&mut self, status: Status) {
        self.failure = status.failure();
        self.status = status.status().clone();
        self.duration = *self.duration() + *status.duration();
    }

    pub fn err(&self) -> Option<RequestFailure> {
        self.failure.clone()
    }

    pub fn is_err(&self) -> bool {
        self.err().is_some()
    }

    pub fn is_ok(&self) -> bool {
        self.err().is_none()
    }
}

#[derive(Debug, Clone)]
pub enum RequestStatus {
    Failed { message: String },
    Success,
    Pending,
}

impl RequestStatus {
    pub fn is_pending(&self) -> bool {
        match self {
            Self::Failed { .. } | Self::Success => false,
            Self::Pending => true,
        }
    }

    pub fn is_success(&self) -> bool {
        match self {
            Self::Failed { .. } | Self::Pending => false,
            Self::Success => true,
        }
    }

    pub fn is_failed(&self) -> bool {
        match self {
            Self::Pending | Self::Success => false,
            Self::Failed { .. } => true,
        }
    }
}