-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
73 lines (65 loc) · 1.71 KB
/
main.rs
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
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
match args.len() {
1 | 2 => panic!("Please add an argument"),
3 => {
println!("Transforming argument");
let shift: u8 = args[2].parse().unwrap();
println!(
"{} --> {}",
args[1],
caesar_decrypt(args[1].as_str(), shift)
);
}
_ => panic!("Too many arguments"),
}
}
pub fn caesar_encrypt(word: &str, shift: u8) -> String {
word.chars()
.map(|c| {
if c.is_alphabetic() {
let start = if c.is_ascii_lowercase() { b'a' } else { b'A' };
(start + (c as u8 + shift - start) % 26) as char
} else {
c
}
})
.collect()
}
pub fn caesar_decrypt(word: &str, shift: u8) -> String {
word.chars()
.map(|c| {
if c.is_alphabetic() {
let start = if c.is_ascii_lowercase() { b'a' } else { b'A' };
let mut shifted_c = c as u8 - shift;
if shifted_c < start {
shifted_c += 26;
}
shifted_c as char
} else {
c
}
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_encrypt() {
assert_eq!(caesar_encrypt("", 13), "");
}
#[test]
fn caesar_rot_13_encrypt() {
assert_eq!(caesar_encrypt("rust", 13), "ehfg");
}
#[test]
fn empty_decrypt() {
assert_eq!(caesar_decrypt("", 13), "");
}
#[test]
fn caesar_rot_13_decrypt() {
assert_eq!(caesar_decrypt("ehfg", 13), "rust");
}
}