FrenBot/src/insult_compliment.rs
2022-12-30 22:33:48 -06:00

26 lines
728 B
Rust

use crate::error::Error;
use rand::seq::SliceRandom;
use rand::thread_rng;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tera::{Context, Tera};
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
pub struct InsultComplimentTemplate {
pub template: String,
pub word_bank: HashMap<String, Vec<String>>,
}
impl InsultComplimentTemplate {
pub fn render(&self, target: &str) -> Result<String, Error> {
let mut context = Context::new();
context.insert("target", target);
for (key, words) in &self.word_bank {
context.insert(key, &words.choose(&mut thread_rng()).unwrap());
}
Ok(Tera::one_off(&self.template, &context, false)?)
}
}