laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
use std::collections::hash_map::RandomState;
use std::hash::BuildHasher;

/// Inspiring quotes, each written as `"The quote. - Its author"`.
///
/// Every application registers `Inspiring`, so a command or handler can take it as an argument:
///
/// ```
/// use laravel::console::{Console, command};
/// use laravel::container::Inject;
/// use laravel::foundation::Inspiring;
///
/// /// Display an inspiring quote.
/// #[command("inspire")]
/// async fn inspire(console: &mut Console, inspiring: Inject<Inspiring>) {
///     console.info(inspiring.quote());
/// }
/// # fn main() {}
/// ```
///
/// To choose the quotes, register an instance of your own with
/// [`Registrar::instance`](crate::container::Registrar::instance).
#[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 {
    /// Create an `Inspiring` with quotes of your own instead of the built-in ones.
    ///
    /// # Panics
    ///
    /// Panics when there are no quotes to choose from.
    #[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 }
    }

    /// A quote chosen at random.
    #[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]
    }

    /// Every quote.
    #[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());
    }
}