1. ```
use std::io::Read;
```
3. ```
struct RotDecoder<R: Read> {
```
4. ```
input: R,
```
5. ```
rot: u8,
```
6. ```
}
```
8. ```
impl<R: Read> Read for RotDecoder<R> {
```
9. ```
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
```
10. ```
let size = self.input.read(buf)?;
```
11. ```
for b in &mut buf[..size] {
```
12. ```
if b.is_ascii_alphabetic() {
```
13. ```
let base = if b.is_ascii_uppercase() { 'A' } else { 'a' } as u8;
```
14. ```
*b = (*b - base + self.rot) % 26 + base;
```
15. ```
}
```
16. ```
}
```
17. ```
Ok(size)
```
18. ```
}
```
19. ```
}
```
21. ```
fn main() {
```
22. ```
let mut rot =
```
23. ```
RotDecoder { input: "Gb trg gb gur bgure fvqr!".as_bytes(), rot: 13 };
```
24. ```
let mut result = String::new();
```
25. ```
rot.read_to_string(&mut result).unwrap();
```
26. ```
println!("{}", result);
```
27. ```
}
```
29. ```
#[cfg(test)]
```
30. ```
mod test {
```
31. ```
use super::*;
```
33. ```
#[test]
```
34. ```
fn joke() {
```
35. ```
let mut rot =
```
36. ```
RotDecoder { input: "Gb trg gb gur bgure fvqr!".as_bytes(), rot: 13 };
```
37. ```
let mut result = String::new();
```
38. ```
rot.read_to_string(&mut result).unwrap();
```
39. ```
assert_eq!(&result, "To get to the other side!");
```
40. ```
}
```
42. ```
#[test]
```
43. ```
fn binary() {
```
44. ```
let input: Vec<u8> = (0..=255u8).collect();
```
45. ```
let mut rot = RotDecoder::<&[u8]> { input: input.as_ref(), rot: 13 };
```
46. ```
let mut buf = [0u8; 256];
```
47. ```
assert_eq!(rot.read(&mut buf).unwrap(), 256);
```
48. ```
for i in 0..=255 {
```
49. ```
if input[i] != buf[i] {
```
50. ```
assert!(input[i].is_ascii_alphabetic());
```
51. ```
assert!(buf[i].is_ascii_alphabetic());
```
52. ```
}
```
53. ```
}
```
54. ```
}
```
55. ```
}
```