use std::collections::hash_map::RandomState;
use std::hash::BuildHasher;
#[derive(Debug, Clone)]
pub struct Inspiring {
quotes: Vec<String>,
}
impl Default for Inspiring {
fn default() -> Self {
Self::new(QUOTES.iter().map(|quote| (*quote).to_owned()))
}
}
impl Inspiring {
#[track_caller]
pub fn new(quotes: impl IntoIterator<Item = String>) -> Self {
let quotes: Vec<String> = quotes.into_iter().collect();
assert!(
!quotes.is_empty(),
"`Inspiring` needs at least one quote to choose from"
);
Self { quotes }
}
#[must_use]
pub fn quote(&self) -> &str {
let random = RandomState::new().hash_one(self.quotes.len());
let index = (random % self.quotes.len() as u64) as usize;
&self.quotes[index]
}
#[must_use]
pub fn quotes(&self) -> &[String] {
&self.quotes
}
}
const QUOTES: [&str; 20] = [
"Act only according to that maxim whereby you can, at the same time, will that it should become a universal law. - Immanuel Kant",
"An unexamined life is not worth living. - Socrates",
"Be present above all else. - Naval Ravikant",
"Do what you can, with what you have, where you are. - Theodore Roosevelt",
"He who is contented is rich. - Laozi",
"I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison",
"It always seems impossible until it is done. - Nelson Mandela",
"It is never too late to be what you might have been. - George Eliot",
"Knowing is not enough; we must apply. Being willing is not enough; we must do. - Leonardo da Vinci",
"Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi",
"Make it work, make it right, make it fast. - Kent Beck",
"No surplus words or unnecessary actions. - Marcus Aurelius",
"Nothing worth having comes easy. - Theodore Roosevelt",
"Simplicity is the ultimate sophistication. - Leonardo da Vinci",
"Smile, breathe, and go slowly. - Thich Nhat Hanh",
"The best way to predict the future is to invent it. - Alan Kay",
"The only way to do great work is to love what you do. - Steve Jobs",
"Very little is needed to make a happy life. - Marcus Aurelius",
"Waste no more time arguing what a good man should be, be one. - Marcus Aurelius",
"Well begun is half done. - Aristotle",
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic(expected = "needs at least one quote")]
fn an_empty_list_is_a_mistake() {
let _ = Inspiring::new(Vec::new());
}
}