Skip to content

Commit

Permalink
add rot13
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotcubit committed Jul 30, 2021
1 parent 14e3426 commit 6f06c95
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rc"
version = "0.2.2"
version = "0.3.0"
authors = ["Elliot Cubit <[email protected]>"]
description = "Text encoding swiss army knife"
edition = "2018"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ base 64: "aGVsbG8gbmV0d29yaw=="
- [x] binary
- [ ] ascii85
- [ ] url
- [ ] spelling alphabet
- [x] spelling alphabet

### Number bases

Expand All @@ -93,7 +93,7 @@ base 64: "aGVsbG8gbmV0d29yaw=="

- [ ] caeser
- [ ] vigenère
- [ ] rot13
- [x] rot13
- [ ] substitution

### Crypto
Expand Down
3 changes: 3 additions & 0 deletions src/codecs/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ pub trait Codec {
fn decode(&self, s: Vec<u8>) -> Result<Vec<u8>, Error>;
fn encode(&self, data: Vec<u8>) -> Result<String, Error>;
fn format(&self) -> Format;
fn inferrable(&self) -> bool {
true
}
}
1 change: 1 addition & 0 deletions src/codecs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ pub mod codec;
pub mod error;
pub mod hex;
pub mod raw;
pub mod rot13;
pub mod spelling;
pub mod utf8;
51 changes: 51 additions & 0 deletions src/codecs/rot13.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use super::codec::Codec;
use super::error::Error;
use crate::Format;

pub struct Rot13Codec {}

impl Codec for Rot13Codec {
fn format(&self) -> Format {
Format::Rot13
}

fn decode(&self, s: Vec<u8>) -> Result<Vec<u8>, Error> {
String::from_utf8(s.clone())
.map(|s| {
s.to_lowercase()
.chars()
.map(Self::cnv)
.collect::<Result<String, Error>>()
.map(|s| s.into_bytes())
})
.unwrap_or(Err(Error::new("input data is not utf8".to_string())))
}

fn encode(&self, data: Vec<u8>) -> Result<String, Error> {
String::from_utf8(data.clone())
.map(|s| {
s.to_lowercase()
.chars()
.map(Self::cnv)
.collect::<Result<String, Error>>()
})
.unwrap_or(Err(Error::new("input data is not utf8".to_string())))
}

fn inferrable(&self) -> bool {
false
}
}

impl Rot13Codec {
fn cnv(c: char) -> Result<char, Error> {
let val = c as u8;
if val >= 'a' as u8 && val <= ('z' as u8 - 13) {
Ok((val + 13) as char)
} else if val > ('z' as u8 - 13) && val <= 'z' as u8 {
Ok((((val + 12) % ('z' as u8)) + 'a' as u8) as char)
} else {
Ok(val as char)
}
}
}
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,18 @@ fn decode_encode(from: &str, to: Vec<&str>, _as: &str, verbosity: u64, value: Ve
// and inferring codecs. Order is significant.
fn codecs_preferred_order() -> Vec<Box<dyn Codec>> {
vec![
Box::new(codecs::spelling::SpellingCodec {}),
/*
Codecs that can't be assumed must be first
*/
Box::new(codecs::rot13::Rot13Codec {}),
/*
Inferrable codecs
*/
// Rule out binary before assuming hex
Box::new(codecs::binary::BinaryCodec {}),
// Rule out hex before assuming base 64
Box::new(codecs::hex::HexCodec {}),
Box::new(codecs::spelling::SpellingCodec {}),
// Rule out base 64 before assuming utf8
Box::new(codecs::base64::Base64Codec {}),
// Rule out utf8 before assuming it's nothing
Expand Down
7 changes: 6 additions & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ pub enum Format {
Base64,
Binary,
Spelling,
Rot13,
Raw,
Inferred,
}

impl Format {
pub fn all_variants() -> Vec<&'static str> {
vec!["utf8", "hex", "base64", "binary", "spelling", "raw"]
vec![
"utf8", "hex", "base64", "binary", "spelling", "raw", "rot13",
]
}

pub fn from_str(s: &str) -> Option<Self> {
Expand All @@ -33,6 +36,7 @@ impl Format {
"base64" => Some(Self::Base64),
"binary" => Some(Self::Binary),
"spelling" => Some(Self::Spelling),
"rot13" => Some(Self::Rot13),
"raw" => Some(Self::Raw),
"__infer" => Some(Self::Inferred),
_ => None,
Expand All @@ -51,6 +55,7 @@ impl Format {
Self::Base64 => "base 64",
Self::Binary => "binary",
Self::Spelling => "spelling",
Self::Rot13 => "rot13",
Self::Raw => "raw bytes",
// This shouldn't really happen, since if we ever have an inferred enum
// we should be converting it to a relevant one
Expand Down

0 comments on commit 6f06c95

Please sign in to comment.