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
#![warn(clippy::all, clippy::pedantic)]
pub mod deck;
pub mod error;
pub mod format;
use crate::deck::Deck;
use crate::error::*;
use base64::{decode, encode};
use integer_encoding::VarInt;
pub fn decode_deck_code(deck_code: &str) -> Result<Deck, DeckCodeError> {
let decoded: Vec<u32> = decode_code_to_u32_vec(deck_code)?;
Deck::new(&decoded)
}
pub fn encode_deck_code(deck: &Deck) -> String {
encode_u32_vec_to_deck_code(deck.to_byte_array())
}
fn decode_code_to_u32_vec(deck_code: &str) -> Result<Vec<u32>, DeckCodeError> {
let mut decoded = decode(deck_code)?;
let mut deck_code_decoded: Vec<u32> = vec![];
while !decoded.is_empty() {
let (read, size) = u32::decode_var(&decoded).ok_or(DeckCodeError::Unknown)?;
deck_code_decoded.push(read);
decoded = decoded[size..].to_vec();
}
Ok(deck_code_decoded)
}
fn encode_u32_vec_to_deck_code(byte_array: Vec<u32>) -> String {
let mut fixed_size_integers: Vec<u8> = Vec::new();
let mut encoded: [u8; 4] = [0, 0, 0, 0];
for i in byte_array {
let encoded_bytes = i.encode_var(&mut encoded[..]);
for encoded_index in 0..encoded_bytes {
fixed_size_integers.push(encoded[encoded_index]);
}
}
encode(&fixed_size_integers)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decode_code_to_u32_vec_correctly_decodes_a_simple_code() {
let result = decode_code_to_u32_vec("AAEBAQcAAAQBAwIDAwMEAw==");
let expected = vec![
0,
1,
1,
1,
7,
0,
0,
4,
1, 3,
2, 3,
3, 3,
4, 3,
];
assert!(result.is_ok());
assert_eq!(result.unwrap(), expected);
}
#[test]
fn encode_simple_byte_array_as_deck_code() {
let input = vec![
0,
1,
1,
1,
7,
0,
0,
4,
1, 3,
2, 3,
3, 3,
4, 3,
];
let expected = "AAEBAQcAAAQBAwIDAwMEAw==";
let result = encode_u32_vec_to_deck_code(input);
assert_eq!(result, expected);
}
#[test]
fn decode_code_to_u8_vec_correctly_decodes_a_complex_code() {
let result = decode_code_to_u32_vec(
"AAECAf0EBMABobcC3s0Cps4CDXHDAbsClQOrBJYF7AWjtgLXtgLpugKHvQLBwQKYxAIA",
);
let expected = vec![
0, 1, 2,
1, 637,
4,
192,
39841,
42718,
42790,
13,
113,
195,
315,
405,
555,
662,
748,
39715,
39767,
40297,
40583,
41153,
41496,
0,
];
assert!(result.is_ok());
assert_eq!(result.unwrap(), expected);
}
#[test]
fn encode_complex_byte_array_as_deck_code() {
let input = vec![
0, 1, 2,
1, 637,
4,
192,
39841,
42718,
42790,
13,
113,
195,
315,
405,
555,
662,
748,
39715,
39767,
40297,
40583,
41153,
41496,
0,
];
let expected = "AAECAf0EBMABobcC3s0Cps4CDXHDAbsClQOrBJYF7AWjtgLXtgLpugKHvQLBwQKYxAIA";
assert_eq!(encode_u32_vec_to_deck_code(input), expected);
}
}