diff --git a/README.md b/README.md index 2a6a522..dc830e4 100644 --- a/README.md +++ b/README.md @@ -37,13 +37,30 @@ Separating out the letters and words: > ] > ] +## Usage + +To use the library, just import the relevant functions, and run it like this: + +```rust +use cat2text::base4::{encode, decode}; + +let encoded = encode("i love cats".to_string()); +assert_eq!(encoded, "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"); +let decoded = decode(encoded); +assert_eq!(decoded, "i love cats"); +``` + +### Binary usage + +TODO + ## Limitations This currently only supports lowercase text in the latin alphabet without numbers or anything - however, using `cat2text::core`, you can convert anything to meows, as long as you can put it in integers - which, you can. ## TODO -- Add functionality for converting `Vec<u8>` to catspeak. - Add more bases - any base option? - Make `core::{cat_to_num, num_to_cat}` usage consistent with each other - Improve CLI +- Add error handling diff --git a/src/base4.rs b/src/base4.rs index bd99416..957df74 100644 --- a/src/base4.rs +++ b/src/base4.rs @@ -17,7 +17,7 @@ pub fn char_length() -> u32 { /// ``` /// use cat2text::base4::encode; /// -/// 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().)) +/// 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 { // makes it lowercase and split by spaces diff --git a/src/bytes.rs b/src/bytes.rs new file mode 100644 index 0000000..874e993 --- /dev/null +++ b/src/bytes.rs @@ -0,0 +1,37 @@ +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/core.rs b/src/core.rs index 9a427eb..d5c0dba 100644 --- a/src/core.rs +++ b/src/core.rs @@ -13,6 +13,7 @@ pub fn num_to_cat(num: u32, alphabet: Vec<String>, char_length: u32) -> String { let base: u32 = alphabet.len() as u32; // base*n*-ifying logic + // FIXME: With base 10 this loops 100 times? see `anybase::test_anybase_encode()` let mut nums: Vec<u32> = Vec::new(); while (nums.len() as u32) < char_length { nums.push((num as u32) % base); @@ -74,9 +75,10 @@ pub fn split_every_x(text: String, x: usize) -> Vec<String> { output.push(String::new()) } output[i / x] += tmp[i].as_str(); - if i % x != 2 { - output[i / x] += " "; - } + output[i / x] += " "; } + + // trim everything before sending it back + output = output.into_iter().map(|item| item.trim().to_string()).collect(); return output; } diff --git a/src/lib.rs b/src/lib.rs index 0de5ea9..d5ac1ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![doc = include_str!("../README.md")] -pub mod anybase; +//pub mod anybase; pub mod base4; pub mod core; +pub mod bytes;