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
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Error, Formatter, Write};
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone, Default)]
pub struct ChunkDetails {
pub chunk_num: usize,
pub hash: Vec<u8>,
pub pre_hash: Vec<u8>,
pub source_size: usize,
}
fn debug_bytes<V: AsRef<[u8]>>(input: V) -> String {
let input_ref = input.as_ref();
if input_ref.is_empty() {
return "<empty>".to_owned();
}
if input_ref.len() <= 6 {
let mut ret = String::new();
for byte in input_ref.iter() {
write!(ret, "{:02x}", byte).unwrap_or(());
}
return ret;
}
format!(
"{:02x}{:02x}{:02x}..{:02x}{:02x}{:02x}",
input_ref[0],
input_ref[1],
input_ref[2],
input_ref[input_ref.len() - 3],
input_ref[input_ref.len() - 2],
input_ref[input_ref.len() - 1]
)
}
impl ChunkDetails {
pub fn new() -> ChunkDetails {
ChunkDetails {
chunk_num: 0,
hash: vec![],
pre_hash: vec![],
source_size: 0,
}
}
}
impl Debug for ChunkDetails {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
write!(
formatter,
"ChunkDetails {{ chunk_num: {}, hash: {}, pre_hash: {}, source_size: {} }}",
self.chunk_num,
debug_bytes(&self.hash),
debug_bytes(&self.pre_hash),
self.source_size
)
}
}
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum DataMap {
Chunks(Vec<ChunkDetails>),
Content(Vec<u8>),
None,
}
#[allow(clippy::len_without_is_empty)]
impl DataMap {
pub fn len(&self) -> usize {
match *self {
DataMap::Chunks(ref chunks) => DataMap::chunks_size(chunks),
DataMap::Content(ref content) => content.len(),
DataMap::None => 0,
}
}
pub fn get_chunks(&self) -> Vec<ChunkDetails> {
match *self {
DataMap::Chunks(ref chunks) => chunks.to_vec(),
_ => panic!("no chunks"),
}
}
pub fn get_sorted_chunks(&self) -> Vec<ChunkDetails> {
match *self {
DataMap::Chunks(ref chunks) => {
let mut result = chunks.to_vec();
DataMap::chunks_sort(&mut result);
result
}
_ => panic!("no chunks"),
}
}
pub fn has_chunks(&self) -> bool {
match *self {
DataMap::Chunks(ref chunks) => DataMap::chunks_size(chunks) > 0,
_ => false,
}
}
pub fn chunks_sort(chunks: &mut [ChunkDetails]) {
chunks.sort_by(|a, b| a.chunk_num.cmp(&b.chunk_num));
}
fn chunks_size(chunks: &[ChunkDetails]) -> usize {
chunks.iter().fold(0, |acc, chunk| acc + chunk.source_size)
}
}
impl Debug for DataMap {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
match *self {
DataMap::Chunks(ref chunks) => {
writeln!(formatter, "DataMap::Chunks:")?;
let len = chunks.len();
for (index, chunk) in chunks.iter().enumerate() {
if index + 1 == len {
write!(formatter, " {:?}", chunk)?
} else {
writeln!(formatter, " {:?}", chunk)?
}
}
Ok(())
}
DataMap::Content(ref content) => {
write!(formatter, "DataMap::Content({})", debug_bytes(content))
}
DataMap::None => write!(formatter, "DataMap::None"),
}
}
}