Initial commit

This commit is contained in:
Daan Boerlage 2023-01-10 11:23:04 +01:00
commit c260b3cf3e
Signed by: daan
GPG key ID: FCE070E1E4956606
5 changed files with 72 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "rot13"
version = "0.1.0"

4
Cargo.toml Normal file
View file

@ -0,0 +1,4 @@
[package]
name = "rot13"
version = "0.1.0"
edition = "2021"

10
readme.md Normal file
View file

@ -0,0 +1,10 @@
# ROT13 CLI
A simple CLI tool that "encrypts" text using rot13
## Usage
```shell
rot13 [input]
echo [input] | rot13
```

50
src/main.rs Normal file
View file

@ -0,0 +1,50 @@
use std::env;
use std::io;
use std::io::BufRead;
use std::process;
fn main() {
// check for direct params
let args: Vec<String> = env::args().collect();
let arg_input = args.get(1);
if let Some(x) = arg_input {
println!("{}", rot13(x));
process::exit(0);
}
// fallback to stdin if no params have been supplied directly
let mut std_input = String::new();
let stdin = io::stdin();
let mut handle = stdin.lock();
handle.read_line(&mut std_input).unwrap();
if !std_input.is_empty() {
println!("{}", rot13(&std_input));
process::exit(0);
}
// exit with error if neither direct params nor stdin have been supplied
process::exit(1);
}
pub fn rot13(text: &str) -> String {
text.chars().map(|c| {
match c {
'A' ..= 'M' | 'a' ..= 'm' => ((c as u8) + 13) as char,
'N' ..= 'Z' | 'n' ..= 'z' => ((c as u8) - 13) as char,
_ => c
}
}).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rot13_test() {
assert_eq!(rot13("abc123"), "nop123");
assert_eq!(rot13("KlmnoP"), "XyzabC");
}
}