use std::fmt::Display;
use rand::seq::SliceRandom; use rand::Rng;
pub struct Randaam {
pub person: String,
pub age: u8,
pub location: String,
pub salary: u32,
pub rarity: Rarity,
pub emoji: char,
}
impl Display for Randaam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}\n\n {}\n {}\n {} jaar oud \n €{} per week \n woont in een {}",
self.rarity, self.emoji, self.person, self.age, self.salary, self.location
)
}
}
impl Randaam {
pub fn build(
person: String,
age: u8,
location: String,
salary: u32,
rarity: Rarity,
emoji: char,
) -> Randaam {
Randaam {
person,
age,
location,
salary,
rarity,
emoji,
}
}
}
#[derive(PartialEq, Debug)]
pub enum Rarity {
Normal,
Rare,
Epic,
Legendary,
SuperLegendary,
}
impl Display for Rarity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let r = match self {
Rarity::SuperLegendary => String::from("SUPER LEGENDARISCH!!!"),
Rarity::Legendary => String::from("Legendarisch!!"),
Rarity::Epic => String::from("Episch!"),
Rarity::Rare => String::from("Zeldzaam"),
Rarity::Normal => String::from("normaal"),
};
write!(f, "{}", r)
}
}
pub fn name() -> String {
let names = include_str!("./content/names.txt")
.lines()
.collect::<Vec<_>>();
names.choose(&mut rand::thread_rng()).unwrap().to_string()
}
pub fn object() -> String {
let objects = include_str!("./content/objects.txt")
.lines()
.collect::<Vec<_>>();
objects.choose(&mut rand::thread_rng()).unwrap().to_string()
}
pub fn action() -> String {
let actions = include_str!("./content/actions.txt")
.lines()
.collect::<Vec<_>>();
actions.choose(&mut rand::thread_rng()).unwrap().to_string()
}
pub fn person() -> String {
format!("{} de {} {}", name(), object(), action())
}
pub fn emoji() -> char {
let emojis = include_str!("./content/emojis.txt")
.chars()
.filter(|c| c != &'\n')
.collect::<Vec<_>>();
emojis.choose(&mut rand::thread_rng()).unwrap().clone()
}
pub fn location() -> String {
let locations = include_str!("./content/locations.txt")
.lines()
.collect::<Vec<_>>();
locations
.choose(&mut rand::thread_rng())
.unwrap()
.to_string()
}
pub fn age() -> u8 {
rand::thread_rng().gen_range(1..101)
}
pub fn rarity() -> Rarity {
let rarity_num = rand::thread_rng().gen_range(1..10001);
decide_rarity(rarity_num)
}
pub fn decide_rarity(x: u32) -> Rarity {
if x > 10000 {
panic!("x should be lower or equal to 10000");
} else if x == 10000 {
Rarity::SuperLegendary
} else if x > 9990 {
Rarity::Legendary
} else if x > 9900 {
Rarity::Epic
} else if x > 9000 {
Rarity::Rare
} else if x > 0 {
Rarity::Normal
} else {
panic!("x should be higher or equal to 1");
}
}
pub fn salary() -> u32 {
rand::thread_rng().gen_range(1..2001)
}
impl Randaam {
pub fn gen() -> Self {
Randaam::build(person(), age(), location(), salary(), rarity(), emoji())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test_name() {
assert!(name().len() > 0);
}
#[test]
pub fn test_object() {
assert!(object().len() > 0);
}
#[test]
pub fn test_action() {
assert!(action().len() > 0);
}
#[test]
pub fn test_location() {
assert!(location().len() > 0);
}
#[test]
pub fn test_person() {
assert!(person().contains(" de "));
}
#[test]
pub fn test_age() {
let age = age();
assert!(age > 0 && 101 > age);
}
#[test]
#[should_panic]
pub fn too_high_for_rarity() {
decide_rarity(10001);
}
#[test]
#[should_panic]
pub fn too_low_for_rarity() {
decide_rarity(0);
}
#[test]
pub fn decide_rarity_works() {
assert_eq!(decide_rarity(9901), Rarity::Epic);
assert_eq!(decide_rarity(10000), Rarity::SuperLegendary);
assert_eq!(decide_rarity(9991), Rarity::Legendary);
assert_eq!(decide_rarity(9001), Rarity::Rare);
assert_eq!(decide_rarity(1), Rarity::Normal);
}
#[test]
pub fn test_salary() {
let sample = salary();
assert!(sample > 0 && 2001 > sample);
}
}