buup/transformers/
rot13.rs1use crate::{Transform, TransformError, TransformerCategory};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct Rot13;
6
7impl Transform for Rot13 {
8 fn name(&self) -> &'static str {
9 "Rot13"
10 }
11
12 fn id(&self) -> &'static str {
13 "rot13"
14 }
15
16 fn description(&self) -> &'static str {
17 "Applies the ROT13 substitution cipher to the input text."
18 }
19
20 fn category(&self) -> TransformerCategory {
21 TransformerCategory::Encoder }
23
24 fn transform(&self, input: &str) -> Result<String, TransformError> {
25 Ok(input
26 .chars()
27 .map(|c| {
28 if c.is_ascii_alphabetic() {
29 let base = if c.is_ascii_lowercase() { b'a' } else { b'A' };
30 (((c as u8 - base + 13) % 26) + base) as char
31 } else {
32 c
33 }
34 })
35 .collect())
36 }
37
38 fn default_test_input(&self) -> &'static str {
39 "The quick brown fox jumps over the lazy dog"
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn test_rot13_transformation() {
49 let transformer = Rot13;
50 let input = transformer.default_test_input();
51 let expected_output = "Gur dhvpx oebja sbk whzcf bire gur ynml qbt";
52 assert_eq!(transformer.transform(input).unwrap(), expected_output);
53
54 let double_transformed = transformer.transform(expected_output).unwrap();
56 assert_eq!(double_transformed, input);
57
58 let mixed_input = "Hello 123! - World?";
60 let expected_mixed_output = "Uryyb 123! - Jbeyq?";
61 assert_eq!(
62 transformer.transform(mixed_input).unwrap(),
63 expected_mixed_output
64 );
65 }
66
67 #[test]
68 fn test_rot13_metadata() {
69 let transformer = Rot13;
70 assert_eq!(transformer.name(), "Rot13");
71 assert_eq!(transformer.id(), "rot13");
72 assert_eq!(transformer.category(), TransformerCategory::Encoder);
73 }
74}