From c260b3cf3e9050c299cbbbadb7b8cb2574cffcfc Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Tue, 10 Jan 2023 11:23:04 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 7 +++++++ Cargo.toml | 4 ++++ readme.md | 10 ++++++++++ src/main.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 readme.md create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c7f5b43 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f1f0a6f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "rot13" +version = "0.1.0" +edition = "2021" diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e0bfaaa --- /dev/null +++ b/readme.md @@ -0,0 +1,10 @@ +# ROT13 CLI + +A simple CLI tool that "encrypts" text using rot13 + +## Usage + +```shell +rot13 [input] +echo [input] | rot13 +``` diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..695f8df --- /dev/null +++ b/src/main.rs @@ -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 = 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"); + } +} \ No newline at end of file