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
use serde::Serialize;
/// What happened to one request to the endpoint.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SubmissionResult {
/// The host the URLs belong to.
pub host: String,
/// The URLs that were sent.
pub urls: Vec<String>,
/// Whether the endpoint accepted them.
pub successful: bool,
/// The HTTP status, when the endpoint answered.
pub status: Option<u16>,
/// A message written to be read by a human.
pub message: String,
/// Whether nothing was sent, for instance because the host has no key.
pub skipped: bool,
}
impl SubmissionResult {
/// The endpoint accepted the URLs.
pub fn success(host: &str, urls: &[String], status: u16, message: impl Into<String>) -> Self {
Self {
host: host.to_owned(),
urls: urls.to_vec(),
successful: true,
status: Some(status),
message: message.into(),
skipped: false,
}
}
/// The endpoint, or the pipeline, rejected the URLs for good.
pub fn failure(host: &str, urls: &[String], status: Option<u16>, message: impl Into<String>) -> Self {
Self {
host: host.to_owned(),
urls: urls.to_vec(),
successful: false,
status,
message: message.into(),
skipped: false,
}
}
/// Nothing was sent.
pub fn skipped(host: &str, urls: &[String], message: impl Into<String>) -> Self {
Self {
host: host.to_owned(),
urls: urls.to_vec(),
successful: false,
status: None,
message: message.into(),
skipped: true,
}
}
/// Whether the request failed, as opposed to succeeding or being skipped.
pub fn failed(&self) -> bool {
!self.successful && !self.skipped
}
/// How many URLs the request carried.
pub fn count(&self) -> usize {
self.urls.len()
}
}
/// A failure that retrying may fix: a rate limit, a server error or a connection that did not
/// complete. A rejected key or a malformed URL is never one of these.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RetryableSubmission {
/// What went wrong.
pub message: String,
/// The host the URLs belong to.
pub host: String,
/// The URLs that were being sent.
pub urls: Vec<String>,
/// The seconds the endpoint asked us to wait, from its `Retry-After` header.
pub retry_after: Option<u64>,
}
impl RetryableSubmission {
/// Describe a retryable failure.
pub fn new(message: impl Into<String>, host: &str, urls: &[String], retry_after: Option<u64>) -> Self {
Self {
message: message.into(),
host: host.to_owned(),
urls: urls.to_vec(),
retry_after,
}
}
}
impl std::fmt::Display for RetryableSubmission {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(&self.message)
}
}
impl std::error::Error for RetryableSubmission {}