minehut 2.0.0

Simple, easy to use Rust wrapper for the Minehut API
Documentation
//! This module has functions related to getting server icons data from the
//! Minehut API. It provides useful functionalities to filter icons.

use crate::http;
use crate::models::ServerIcon;

/// All icons have a "rank", these ranks are of 5 types. This enumerates
/// those ranks for later use.
pub enum Rarity {
    /// Not valuable.
    Common,
    /// Costs a bit.
    Uncommon,
    /// Valuable.
    Rare,
    /// Very valuable.
    Epic,
    /// Incredibly valuable.
    Legendary
}

/// Gets all Minehut server icons asynchronously as a `ServerIcon` struct.
/// 
/// # Example
/// 
/// ```
/// use minehut::servers::icons;
/// 
/// async fn print_all_icons() {
///     // Looping through icons
///     icons::all().await.unwrap().into_iter().for_each(|i| {
///         println!("{:?}", i)     
///     });
/// }
/// ```
/// 
/// # Error
/// 
/// Returns an error if Reqwest fails to get data from path or if it cannot read 
/// JSON response. Both of these are usually network issues.
pub async fn all() -> Result<Vec<ServerIcon>, crate::Error> {
    http::req_data::<Vec<ServerIcon>>("servers/icons").await
}

/// Returns all `ServerIcon`s that match a specific rarity.
/// 
/// # Arguments
/// 
/// * `rarity` - Rarity to filter icons with.
/// 
/// # Example
/// ```
/// use minehut::servers::icons;
/// use minehut::models::ServerIcon;
/// 
/// async fn common_icons() -> Vec<ServerIcon> {
///     // Getting all icons of rarity Common
///     icons::of_rarity(icons::Rarity::Common).await.unwrap() 
/// }
/// ```
/// 
/// # Errors
/// 
/// Returns an error if Reqwest cannot read data from page. Usually a network
/// issue.
pub async fn of_rarity(rarity: Rarity) -> Result<Vec<ServerIcon>, crate::Error> {
    let rarity = match rarity {
        Rarity::Common => "COMMON",
        Rarity::Uncommon => "UNCOMMON",
        Rarity::Rare => "RARE",
        Rarity::Epic => "EPIC",
        Rarity::Legendary => "LEGENDARY"
    };

    let filtered_icons = all().await?.into_iter().filter(
        |i| i.rank == rarity

    ).collect();

    Ok(filtered_icons)
}

/// Gets a `ServerIcon` from the specified ID. 
/// 
/// # Arguments
/// 
/// * `id` - ID of the icon.
/// 
/// # Example
/// 
/// ```
/// use minehut::servers::icons;
/// 
/// #[tokio::main]
/// async fn main() {
///     // Get icon from ID
///     let icon = icons::icon_from_id("some-weird-id").await;
/// 
///     match icon {
///         None => println!("Didn't find an icon"),
///         Some(i) => println!("Found icon: {i:?}")
///     }
/// }
/// ```
/// 
/// # Errors
/// 
/// Returns `None` if passed ID does not link to any server icons.
pub async fn icon_from_id(id: &str) -> Option<ServerIcon> {
    all().await.unwrap().into_iter().find(|i| i.id == id)
}

/// Gets an icon from the name specified asynchronously.
/// 
/// # Arguments
/// 
/// * `name` - Name of the icon.
///
/// # Example
/// 
/// ```
/// use minehut::servers::icons;
/// 
/// #[tokio::main]
/// async fn main() {
///     let icon = icons::icon_from_name("IRON_BOOTS").await;
/// 
///     match icon {
///         None => println!("Didn't find an icon"),
///         Some(i) => println!("Found icon: {i:?}")
///     }
/// }
/// ```
/// 
/// # Errors
/// 
/// Returns `None` only if the name given does not match to a server icon.
pub async fn icon_from_name(name: &str) -> Option<ServerIcon> {
    all().await.unwrap().into_iter().find(|i| i.icon_name == name)
}