Add owofier

This commit is contained in:
lemon-sh 2021-12-28 21:04:55 +01:00
parent d5226b5f3f
commit 8466245497
3 changed files with 60 additions and 1 deletions

5
Cargo.lock generated
View file

@ -46,6 +46,10 @@ dependencies = [
"tokio",
"url",
]
name = "arrayvec"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
[[package]]
name = "async-stream"
@ -1342,6 +1346,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"async-circe",
"arrayvec",
"fancy-regex",
"htmlescape",
"reqwest",

View file

@ -15,6 +15,8 @@ rspotify = "0.11"
htmlescape = "0.3"
toml = "0.5"
serde = "1.0"
arrayvec = "0.7"
rand = "0.8"
[dependencies.async-circe]
path = "async-circe/"

View file

@ -1,5 +1,8 @@
use arrayvec::{ArrayString, CapacityError};
use rand::Rng;
use serde_json::Value;
use tracing::debug;
use std::result::Result;
pub async fn get_waifu_pic(category: &str) -> anyhow::Result<Option<String>> {
let api_resp = reqwest::get(format!("https://api.waifu.pics/sfw/{}", category))
@ -13,4 +16,53 @@ pub async fn get_waifu_pic(category: &str) -> anyhow::Result<Option<String>> {
Ok(url)
}
// TODO: add owofier
pub struct OwoCapacityError(CapacityError);
impl<T> From<CapacityError<T>> for OwoCapacityError {
fn from(e: CapacityError<T>) -> Self {
Self { 0: e.simplify() }
}
}
pub fn owoify_out_of_place(input: &str, output: &mut ArrayString<512>) -> Result<(), OwoCapacityError> {
let input: ArrayString<512> = ArrayString::from(input)?;
let mut rng = rand::thread_rng();
let mut last_char = '\0';
for byte in input.bytes() {
let mut ch = char::from(byte);
if !ch.is_ascii() {
continue
}
// owoify character
ch = match ch.to_ascii_lowercase() {
'r' | 'l' => 'w',
_ => ch
};
// stutter (e.g. "o-ohayou gozaimasu!")
if last_char == ' ' && rng.gen_bool(0.2) {
output.try_push(ch)?;
output.try_push('-')?;
}
match ch {
// nya-ify
'a' | 'e' | 'i' | 'o' | 'u' if last_char == 'n' => {
output.try_push('y')?;
},
// textmoji
'.' => {
output.try_push_str(match rng.gen_range(0..6) {
1 => " OwO",
2 => " :3",
3 => " >w<",
4 => " >_<",
5 => " ^•ﻌ•^",
_ => " ^^",
})?;
}
_ => {}
}
output.try_push(ch)?;
last_char = ch;
}
Ok(())
}