diff --git a/src/base4.rs b/src/base4.rs index cbd0716..8230c65 100644 --- a/src/base4.rs +++ b/src/base4.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; + use crate::core; pub fn alphabet() -> Vec<String> { @@ -19,7 +21,8 @@ pub fn char_length() -> u32 { /// /// assert_eq!("meow mreow mrrp; meow mrow meow meow mrow mrow mrrp mrrp mreow meow mrrp mrrp; meow meow mrow meow meow mrrp mrrp mrrp meow mrrp meow mrow", encode("i love cats".to_string())) /// ``` -pub fn encode(text: String) -> String { +pub fn encode(text: impl AsRef<str>) -> String { + let text = text.as_ref(); // makes it lowercase and split by spaces let words: Vec<String> = text .to_ascii_lowercase() @@ -83,3 +86,44 @@ pub fn decode(text: String) -> String { return output.trim().to_string(); } + + +pub mod bytes { + use crate::core; + use crate::base4::alphabet; +/// Encodes from bytes into catspeak +/// +/// ``` +/// use cat2text::base4::bytes::encode; +/// +/// assert_eq!("meow meow mreow mrrp meow meow meow mrrp", encode(&[9, 1])); +/// ``` +pub fn encode(bytes: impl AsRef<[u8]>) -> String { + let mut output = String::new(); + for byte in bytes.as_ref() { + output += core::num_to_cat(*byte as u32, alphabet(), 4).as_str(); + output += " "; + } + return output.trim().to_string(); +} + +/// Decodes catspeak into bytes +/// +/// ``` +/// use cat2text::base4::bytes::decode; +/// +/// assert_eq!(vec![9, 1], decode("meow meow mreow mrrp meow meow meow mrrp".to_string())); +/// ``` +pub fn decode(text: String) -> Vec<u8> { + let mut output: Vec<u8> = Vec::new(); + for byte in core::split_every_x(text.clone(), 4) { + output.push(core::cat_to_num( + byte.split(" ").map(|item| item.to_string()).collect(), + alphabet(), + 4, + ) as u8); + } + return output; +} + +} \ No newline at end of file diff --git a/src/bytes.rs b/src/bytes.rs deleted file mode 100644 index 874e993..0000000 --- a/src/bytes.rs +++ /dev/null @@ -1,37 +0,0 @@ -use crate::base4::alphabet; -use crate::core; - -/// Encodes from bytes into catspeak -/// -/// ``` -/// use cat2text::bytes::from_bytes; -/// -/// assert_eq!("meow meow mreow mrrp meow meow meow mrrp", from_bytes(vec![9, 1])); -/// ``` -pub fn from_bytes(bytes: Vec<u8>) -> String { - let mut output = "".to_string(); - for byte in bytes { - output += core::num_to_cat(byte as u32, alphabet(), 4).as_str(); - output += " "; - } - return output.trim().to_string(); -} - -/// Decodes catspeak into bytes -/// -/// ``` -/// use cat2text::bytes::to_bytes; -/// -/// assert_eq!(vec![9, 1], to_bytes("meow meow mreow mrrp meow meow meow mrrp".to_string())); -/// ``` -pub fn to_bytes(text: String) -> Vec<u8> { - let mut output: Vec<u8> = Vec::new(); - for byte in core::split_every_x(text.clone(), 4) { - output.push(core::cat_to_num( - byte.split(" ").map(|item| item.to_string()).collect(), - alphabet(), - 4, - ) as u8); - } - return output; -} diff --git a/src/lib.rs b/src/lib.rs index 974d5ed..354181a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,5 +2,4 @@ pub mod anybase; pub mod base4; pub mod core; -pub mod bytes; mod tests;