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
use crate::{wire, Error};
use std::num::Wrapping;
#[derive(Copy, Clone, Debug)]
pub struct RotateBytes {
pub amount: u8,
}
impl RotateBytes {
pub const ROT13: RotateBytes = RotateBytes { amount: 13 };
}
impl wire::Middleware for RotateBytes {
fn encode_data(&mut self, data: Vec<u8>) -> Result<Vec<u8>, Error> {
Ok(data.into_iter().map(|b| (Wrapping(b) + Wrapping(self.amount)).0).collect())
}
fn decode_data(&mut self, data: Vec<u8>) -> Result<Vec<u8>, Error> {
Ok(data.into_iter().map(|b| (Wrapping(b) - Wrapping(self.amount)).0).collect())
}
}
#[cfg(test)]
mod test {
use super::RotateBytes;
use crate::wire::Middleware;
#[test]
fn bytes_encoded_correctly() {
assert_eq!(RotateBytes::ROT13.clone().encode_data(vec![0, 1, 2, 3, 4]).unwrap(),
vec![13, 14, 15, 16, 17]);
}
#[test]
fn bytes_decoded_correctly() {
assert_eq!(RotateBytes::ROT13.clone().decode_data(vec![13, 14, 15, 16, 17]).unwrap(),
vec![0, 1, 2, 3, 4]);
}
#[test]
fn bytes_read_back_correctly() {
assert_eq!(RotateBytes::ROT13.clone().encode_data(RotateBytes::ROT13.clone().decode_data(vec![2, 4, 8, 32, 254]).unwrap()).unwrap(),
vec![2, 4, 8, 32, 254]);
}
}