minehut 1.0.1

Simple Rust wrapper for the Minehut API
Documentation
use crate::request;
use once_cell::sync::OnceCell;
use crate::models::Product;

/// Products can be of 9 different types, this enumerates
/// those specific types for later.
pub enum Category {
    /// product is a .jar file
    Plugin, 
    /// created map or world
    World,
    /// scripts/skripts
    Script, 
    /// complete server setup
    ServerSetup,
    /// server minigame setup
    Minigame,
    /// .schema files used for building
    Schematic, 
    /// configuration files??
    Configuration,
    /// bundle of mods
    Modpack, 
    /// plugin that costs money
    PremiumPlugin
}

/// Gets all products from Minehut asynchronously. Caches 
/// product information for later use.
/// 
/// # Example
/// 
/// ```
/// async fn print_expensive_products() {
///     use minehut::products;
/// 
///     // print all icons with price over 700
///     products::all().await.into_iter().for_each(|p| {
///         if(p.price > 700) {
///             println!("{} is expensive", p.title);
///         }    
///     })
/// }
/// ```
pub async fn all() -> &'static [Product] {
    static PRODUCTS: OnceCell<Vec<Product>> = OnceCell::new();

    if PRODUCTS.get().is_none() {
        let products = request::get_data::<Vec<Product>>(
            "https://facade-service-prod.superleague.com/facade/v1/client/products"
        ).await;

        PRODUCTS.set(products).unwrap();
    }

    PRODUCTS.get().unwrap()
}

/// Gets all products of a specific category asynchronously.
/// 
/// # Arguments
/// 
/// * `category` - Category to filter products with
/// 
/// # Example
/// 
/// ```
/// #[tokio::main]
/// async fn main() {
///     use minehut::products;
///     
///     // printing all available plugins
///     products::of_category(products::Category::Plugin).await.into_iter().for_each(|p| {
///         println!("{} is a plugin", p.title);
///     })
/// }
/// ```
pub async fn of_category(category: Category) -> Vec<&'static Product> {
    let category = match category {
        Category::Plugin => "Plugin",
        Category::World =>"World",
        Category::Script => "Script",
        Category::ServerSetup => "Server Setup",
        Category::Minigame => "Minigame",
        Category::Schematic => "Schematic",
        Category::Configuration => "Configuration",
        Category::Modpack => "Modpack",
        Category::PremiumPlugin => "Premium Plugin"
    };

    all().await.into_iter().filter(
        |p| p.category == category

    ).collect()
}

/// Returns all products which contain a specific string slice.
/// 
/// # Arguments
/// 
/// * `query` - String slice to query products
/// 
/// # Example
/// 
/// ```
/// #[tokio::main]
/// async fn main() {
///     use minehut::products;
///     
///     // getting all products with "Skript" in its name
///     products::search("Skript").await.into_iter().for_each(|p| {
///         println!("{}", p.title);
///     })
/// }
/// ```
pub async fn search(query: &str) -> Vec<&'static Product> {
    all().await.into_iter().filter(
        |p| p.title.contains(query)

    ).collect()
}