-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14e3426
commit 6f06c95
Showing
8 changed files
with
73 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters