演習: ROT13

この例では、古典的な “ROT13” 暗号 を実装します。このコードを プレイグラウンドにコピーし、不足している部分を実装してください。結果が引き続き有効な UTF-8 になるように、ASCII の英字のみを回転させてください。

// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0

use std::io::Read;

struct RotDecoder<R: Read> {
    input: R,
    rot: u8,
}

// `RotDecoder` に対して `Read` トレイトを実装します。

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn joke() {
        let mut rot =
            RotDecoder { input: "Gb trg gb gur bgure fvqr!".as_bytes(), rot: 13 };
        let mut result = String::new();
        rot.read_to_string(&mut result).unwrap();
        assert_eq!(&result, "To get to the other side!");
    }

    #[test]
    fn binary() {
        let input: Vec<u8> = (0..=255u8).collect();
        let mut rot = RotDecoder::<&[u8]> { input: input.as_slice(), rot: 13 };
        let mut buf = [0u8; 256];
        assert_eq!(rot.read(&mut buf).unwrap(), 256);
        for i in 0..=255 {
            if input[i] != buf[i] {
                assert!(input[i].is_ascii_alphabetic());
                assert!(buf[i].is_ascii_alphabetic());
            }
        }
    }
}

それぞれ 13 文字回転する 2 つの RotDecoder インスタンスを連結すると、何が起こるでしょうか?