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
use std::ops::{Bound, RangeBounds};
use bee_block::payload::milestone::{MilestoneId, MilestoneIndex};
use inx::proto;
pub enum MilestoneRequest {
MilestoneIndex(MilestoneIndex),
MilestoneId(MilestoneId),
}
impl From<MilestoneRequest> for proto::MilestoneRequest {
fn from(value: MilestoneRequest) -> Self {
match value {
MilestoneRequest::MilestoneIndex(MilestoneIndex(milestone_index)) => Self {
milestone_index,
milestone_id: None,
},
MilestoneRequest::MilestoneId(milestone_id) => Self {
milestone_index: 0,
milestone_id: Some(milestone_id.into()),
},
}
}
}
impl From<u32> for MilestoneRequest {
fn from(value: u32) -> Self {
Self::MilestoneIndex(MilestoneIndex(value))
}
}
fn to_milestone_range_request<T, I>(range: T) -> proto::MilestoneRangeRequest
where
T: RangeBounds<I>,
I: Into<u32> + Copy,
{
let start_milestone_index = match range.start_bound() {
Bound::Included(&idx) => idx.into(),
Bound::Excluded(&idx) => idx.into() + 1,
Bound::Unbounded => 0,
};
let end_milestone_index = match range.end_bound() {
Bound::Included(&idx) => idx.into(),
Bound::Excluded(&idx) => idx.into() - 1,
Bound::Unbounded => 0,
};
proto::MilestoneRangeRequest {
start_milestone_index,
end_milestone_index,
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct MilestoneRangeRequest(proto::MilestoneRangeRequest);
impl<T> From<T> for MilestoneRangeRequest
where
T: RangeBounds<u32>,
{
fn from(value: T) -> MilestoneRangeRequest {
MilestoneRangeRequest(to_milestone_range_request(value))
}
}
impl From<MilestoneRangeRequest> for proto::MilestoneRangeRequest {
fn from(value: MilestoneRangeRequest) -> Self {
value.0
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn exclusive() {
let range = MilestoneRangeRequest::from(17..43);
assert_eq!(
range,
MilestoneRangeRequest(proto::MilestoneRangeRequest {
start_milestone_index: 17,
end_milestone_index: 42
})
);
}
#[test]
fn inclusive() {
let range = MilestoneRangeRequest::from(17..=42);
assert_eq!(
range,
MilestoneRangeRequest(proto::MilestoneRangeRequest {
start_milestone_index: 17,
end_milestone_index: 42
})
);
}
}