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
use serde::Serialize;
#[derive(Debug, Clone, Serialize, Eq, PartialEq, Hash)]
#[must_use]
pub struct Tip {
max_tip_amount: u32,
#[serde(skip_serializing_if = "Option::is_none")]
suggested_tip_amounts: Option<Vec<u32>>,
}
impl Tip {
pub const fn with_max(max_tip: u32) -> Self {
Self {
max_tip_amount: max_tip,
suggested_tip_amounts: None,
}
}
pub fn suggested_tips(mut self, tips: impl Into<Vec<u32>>) -> Self {
let mut tips = tips.into();
assert!(
(1..=4).contains(&tips.len()),
"[tbot] Received invalid `tips` in `Tip::suggested_tips`: \
must contain 1..=4 tips, contains {} instead.",
tips.len(),
);
tips.sort_unstable();
assert!(
tips.windows(2).all(|win| win[0] != win[1]),
"[tbot] Received invalid `tips` in `Tip::suggested_tips`: \
must contain only unique tips.",
);
assert!(
*tips.last().unwrap() <= self.max_tip_amount,
"[tbot] Received invalid `tips` in `Tip::suggested_tips`: \
the maximum value {} exceeds `max_tip_amount` ({}).",
tips.last().unwrap(),
self.max_tip_amount,
);
self.suggested_tip_amounts = Some(tips);
self
}
}