half-functional prototype

This commit is contained in:
askiiart 2025-01-11 23:20:00 -06:00
commit 91dbec05d3
Signed by untrusted user who does not match committer: askiiart
GPG key ID: 6A32977DAF31746A
8 changed files with 175 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "cat2text"
version = "0.1.0"

16
Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "cat2text"
version = "0.1.0"
edition = "2021"
license = "GPL-3.0-or-later"
repository = "https://git.askiiart.net/askiiart/cat2text-rs"
[lib]
name = "cat2text"
path = "src/lib.rs"
[[bin]]
name = "cat2text"
path = "src/main.rs"
[dependencies]

3
README.md Normal file
View file

@ -0,0 +1,3 @@
# Cat2Text-rs
This is a port of [Cat2Text](https://github.com/askiiart/Cat2Text) to Rust, with extra functionality, better documentation, and support for using it as a library as well.

49
src/base4.rs Normal file
View file

@ -0,0 +1,49 @@
/*
def decode(string):
"""Decode a Base 4 encoded string into the number
Arguments:
- `string`: The encoded string
- `alphabet`: The alphabet to use for decoding
"""
base = len(alphabet)
strlen = len(string)
num = 0
idx = 0
for char in string:
power = (strlen - (idx + 1))
num += alphabet.index(char) * (base ** power)
idx += 1
return num
*/
pub fn alphabet() -> Vec<String> {
return vec!["meow", "mrrp", "mreow", "mrow"]
.into_iter()
.map(|a| a.to_string())
.collect();
}
/// How many words long a character is when translated to catspeak
pub fn char_length() -> u32 {
return 3;
}
// TODO: Add uppercase support, maybe possible?
pub fn encode(text: String) {
// makes it lowercase and split by spaces
let words: Vec<String> = text
.to_ascii_lowercase()
.split(" ")
.map(|item| return item.to_string())
.collect();
let mut words_as_bytes: Vec<Vec<u8>> = Vec::new();
for word in words {
// convert to bytes then subtract by 96
words_as_bytes.push(word.as_bytes().into_iter().map(|item| item - 96).collect());
}
let translation: String = "".to_string();
for word in words_as_bytes {}
}

50
src/core.rs Normal file
View file

@ -0,0 +1,50 @@
//! Handles conversion one word at a time for any base
use std::ops::Index;
use crate::base4::alphabet;
pub fn num_to_cat(num: u32, alphabet: Vec<String>, char_length: u32) -> String {
let mut num: u32 = num.clone();
let base: u32 = alphabet.len() as u32;
// base*n*-ifying logic
let mut nums: Vec<u32> = Vec::new();
while (nums.len() as u32) < char_length {
nums.push((num as u32) % base);
num /= base;
}
nums.reverse();
let mut result = "".to_string();
for i in nums.clone() {
result += alphabet[i as usize].as_str();
// put a space in between if it's not the last one
if i != nums.len() as u32 - 1 {
result += " "
}
}
return result;
}
/// Converts catspeak to a [`u32`]
pub fn cat_to_num(text: Vec<String>, alphabet: Vec<String>, char_length: u32) -> u32 {
let mut nums: Vec<u32> = Vec::new();
for word in text.clone() {
for i in 0..alphabet.len() {
if word == alphabet[i] {
nums.push(i as u32);
}
}
}
let base = alphabet.len();
let idx = 0;
let mut num = 0 as u32;
for n in nums {
let power = (text.len() - (idx + 1)) as u32;
num += n * (base as u32).pow(power);
}
return num;
}

5
src/lib.rs Normal file
View file

@ -0,0 +1,5 @@
//! # Cat2Text-rs
//!
//! This is a port of [Cat2Text](https://github.com/askiiart/Cat2Text) to Rust, with extra functionality, better documentation, and support for using it as a library as well.
pub mod base4;
pub mod core;

44
src/main.rs Normal file
View file

@ -0,0 +1,44 @@
extern crate cat2text;
use cat2text::{base4, core};
use std::{io, process::exit};
fn main() {
let stdin = io::stdin();
let mut input = String::new();
loop {
println!("Pick your translation:");
println!("1) cat to text");
println!("2) text to cat");
input = "".to_string();
stdin.read_line(&mut input).unwrap();
let trimmed = input.trim();
if trimmed == "1".to_string() {
input = "".to_string();
stdin.read_line(&mut input).unwrap();
println!(
"{}",
core::cat_to_num(
input.trim().to_string().split(" ").into_iter().map(|item| item.to_string()).collect(),
base4::alphabet(),
base4::char_length()
)
)
} else if trimmed == "2".to_string() {
input = "".to_string();
stdin.read_line(&mut input).unwrap();
println!(
"{}",
core::num_to_cat(
input.trim().parse().unwrap(),
base4::alphabet(),
base4::char_length()
)
)
} else {
println!("Invalid input, exiting...");
break;
}
println!();
}
}