minehut 2.0.0

Simple, easy to use Rust wrapper for the Minehut API
Documentation
use serde::Deserialize;

#[derive(Deserialize)]
/// This struct represents the Minehut API server response. It
/// only contains a server struct
pub struct ServerResponse {
    /// Actual server data.
    pub server: Server
}

#[derive(Debug, Deserialize)]
/// This struct represents a Minehut server, contains most 
/// information returned by the Minehut API.
pub struct Server {
    /// Categories of the server
    pub categories: Vec<String>,
    #[serde(rename = "inheritedCategories")]
    pub inherited_categories: Vec<String>,
    /// Icons purchased by the owner.
    pub purchased_icons: Vec<String>,
    /// Backup slots for the server.
    pub backup_slots: usize,
    /// Whether ser
    /// ver is suspended.
    pub suspended: bool,
    /// What server type it is.
    pub server_version_type: String,
    /// Wether it uses a proxy.
    pub proxy: bool,
    /// All linked servers.
    #[serde(rename = "connectedServers")]
    pub connected_servers: Vec<String>,
    /// ID of the server.
    #[serde(rename = "_id")]
    pub id: String,
    /// MOTD of the server.
    pub motd: String,
    /// Wether the server is allowed to be seen on the server list.
    pub visibility: bool,
    /// The Minehut plan it is using.
    pub server_plan: String,
    /// Its storage node.
    pub storage_node: String,
    /// UUID of the owner.
    pub owner: String,
    /// Name of the server.
    pub name: String,
    /// Lower case name of the server.
    pub name_lower: String,
    /// Time it was created in.
    pub creation: usize,
    /// What platform it is on.
    pub platform: String,
    /// Credits spent per day to host it.
    pub credits_per_day: f32,
    __v: isize,
    /// Time it was last online.
    pub last_online: usize,
    /// Its icon.
    pub active_icon: Option<String>,
    /// Icon of the server.
    pub icon: Option<String>, 
    /// Wether it is online or not.
    pub online: bool,
    /// The maximum number of players it supports.
    #[serde(rename = "maxPlayers")]
    pub max_players: usize,
    /// Current number of players on it.
    #[serde(rename = "playerCount")]
    pub player_count: usize,
    /// Raw Minehut plan.
    #[serde(rename = "rawPlan")]
    pub raw_plan: String,
    /// Current server plan.
    #[serde(rename = "activeServerPlan")]
    pub active_server_plan: String
}

#[derive(Debug, Deserialize)]
/// This struct represents a server icon response from the 
/// Minehut API. Contains useful information about icons such
/// as the ID, name, price and rank.
pub struct ServerIcon {
    /// ID of the server icon.
    #[serde(rename = "_id")]
    pub id: String,
    /// Display name of it.
    pub display_name: String,
    /// Name of it.
    pub icon_name: String,
    /// Credits to buy icon.
    pub price: usize,
    /// Rarity of the icon.
    pub rank: String,
    /// Whether it can be bought.
    pub available: bool,
    /// Wether it is disabled.
    pub disabled: bool,
    /// When it was created.
    pub created: usize,
    /// When it was last updated.
    pub last_updated: usize,
    __v: isize
}

#[derive(Deserialize)]
/// This struct represents the response for a server list from the
/// Minehut API, it only contains a list of server data.
pub struct ServerListResponse {
    /// List of server data.
    pub servers: Vec<ServerData> 
}

/// This struct represents the server data obtained from server list.
/// It is different from the normal Server struct due to how Minehut
/// structured the response.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerData {
    /// More information about the server.
    pub static_info: StaticInfo,
    /// Name of the server.
    pub name: String,
    /// MOTD of the server.
    pub motd: String,
    /// Icon of the server.
    pub icon: Option<String>,
    /// Information about players playing on server.
    pub player_data: PlayerData,
    /// Can be connected to, might return nothing.
    pub connectable: Option<bool>,
    /// Whether server can be seen on server list.
    pub visibility: bool
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Static information for the ServerListData struct. Contains
/// useful information about the server
pub struct StaticInfo {
    /// ID of server.
    #[serde(rename = "_id")]
    pub id: String,
    /// Current server plan.
    pub server_plan: String,
    /// Plan start date.
    pub service_start_date: usize,
    /// Platform hosted on.
    pub platform: String,
    /// Maximum amount of players it can support.
    #[serde(rename = "planMaxPlayers")]
    pub max_players: usize,
    /// Raw plan.
    pub raw_plan: String,
    /// Linked servers (network).
    pub connected_servers: Vec<String>
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Player data struct to deserialize from ServerListData. Struct
/// Only stores player count.
pub struct PlayerData {
    /// Number of players on server.
    pub player_count: usize,
}

#[derive(Deserialize)]
/// This is the struct representation of the server data response from the server/id/server_data 
/// in Minehut.
pub struct OwnedServerRes {
    pub server: OwnedServer
}

#[derive(Debug, Deserialize)]
/// This represents servers owned by an authenticated user using the Client struct. Closely resembles
/// the Server struct with slight modifications allowing more information to be seen.
pub struct OwnedServer {
    // ID of the server.
    #[serde(rename = "_id")]
    pub id: String,
    /// List of plugins.
    pub active_plugins: Vec<String>,
    /// Number of back up slots.
    pub backup_slots: usize,
    /// List of categories.
    pub categories: Vec<String>,
    /// List of connected server IDs.
    #[serde(rename = "connectedServers")]
    pub connected_servers: Vec<String>,
    /// Time the server was created.
    pub creation: usize,
    /// Credits spent to host server per day.
    pub credits_per_day: f32,
    /// List of installed contents.
    pub installed_content: Vec<InstalledContent>,
    /// When it was last online.
    pub last_online: usize,
    /// MOTD of the server.
    pub motd: String,
    /// Name of server.
    pub name: String,
    /// Lower case name of server
    pub name_lower: String,
    /// Owner of server.
    pub owner: String,
    /// Platform of server.
    pub platform: String,
    /// Whether server uses a proxy.
    pub proxy: bool,
    /// List of purchased icon IDs.
    pub purchased_icons: Vec<String>,
    /// Server plan name.
    pub server_plan: String,
    /// Server properties of the server.
    pub server_properties: ServerProperties,
    /// Server version type.
    pub server_version_type: String,
    /// Wether server is suspended.
    pub suspended: bool,
    /// Wether server was visible.
    pub visibility: bool
}

#[derive(Debug, Deserialize)]
/// This is a struct that stores all server properties, used in owned server.
pub struct ServerProperties {
    /// Wether server allows fly.
    #[serde(rename = "allow-flight")]
    pub allow_flight: bool,
    /// Nether is enabled or not.
    #[serde(rename = "allow-nether")]
    pub allow_nether: bool,
    /// Announce player achievements.
    #[serde(rename = "announce-player-achievements")]
    pub announce_player_achievements: bool,
    /// Difficulty of the server.
    pub difficulty: i8,
    /// Command blocks are allowed or not.
    #[serde(rename = "enable-command-block")]
    pub allow_cmd_block: bool,
    /// Enforce white list.
    #[serde(rename = "enforce-whitelist")]
    pub enforce_whitelist: bool,
    /// Force game mode on join.
    #[serde(rename = "force-gamemode")]
    pub force_gamemode: bool,
    /// Gamemode.
    pub gamemode: usize,
    /// Hardcore mode enabled or not.
    pub hardcore: bool,
    /// Main server name.
    #[serde(rename = "level-name")]
    pub level_name: String,
    /// Max amounbt of players.
    #[serde(rename = "max-players")]
    pub max_players: usize,
    /// Tick time.
    #[serde(rename = "max-tick-time")]
    pub max_tick_time: usize,
    /// OP permissions level.
    #[serde(rename = "op-permission-level")]
    pub permission_level: u8,
    /// PVP enabled or not.
    pub pvp: bool,
    /// Spawn animals.
    #[serde(rename = "spawn-mobs")]
    pub spawn_mobs: bool,
    /// Spawn NPCs.
    #[serde(rename = "spawn-npcs")]
    pub spawn_npcs: bool,
    /// Spawn protection.
    #[serde(rename = "spawn-protection")]
    pub spawn_prot: usize,
    /// White list is on or not.
    #[serde(rename = "white-list")]
    pub whitelist: bool
}

#[derive(Debug, Deserialize)]
/// This represents installed content response from Minehut, used in owned servers to see all
/// content on the server.
pub struct InstalledContent {
    /// ID of content.
    #[serde(rename = "_id")]
    pub id: String,
    /// Content ID.
    pub content_id: String,
    /// Content version ID.
    pub content_version_id: String,
    /// When content was installed.
    pub install_date: String,
    /// When it was last updated.
    pub last_updated: String,
    /// Whether it is pinned.
    pub pinned: bool
}

use crate::{Client, Error, http};

impl OwnedServer {
    /// Start an owned server and takes it out of hibernation.
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of calling Client struct.
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// match client.my_server("server-name").await?.start_service(&client).await {
    ///     Err(_) => println!("Could not start server"),
    ///     Ok(_) => prinln!("Started server!")
    /// }
    /// ```
    /// 
    /// # Error
    /// 
    /// Returns a FailedOp error if it could not post data. This usually happens if
    /// you are not allowed to start the server.
    pub async fn start_service(&self, client: &Client) -> Result<(), Error> {
        http::post(&client, &format!("server/{}/start_service", &self.id)).await
    }

    /// Stop an owned server and hibernate it.
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of calling Client struct.
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// match client.my_server("server-name").await?.destroy_service(&client).await {
    ///     Err(_) => println!("Failed to stop server"),
    ///     Ok(_) => println!("Hibernating server")
    /// }
    /// ```
    /// 
    /// # Error
    /// 
    /// Returns a FailedOp error if it could not post data. This usually happens if
    /// you are not allowed to stop the server.
    pub async fn destroy_service(&self, client: &Client) -> Result<(), Error> {
        http::post(&client, &format!("server/{}/destroy_service", &self.id)).await
    }

    /// Start an owned server and hibernate it.
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of calling Client struct.
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// match client.my_server("server-name").await?.start(&client).await {
    ///     Err(e) => println!("Something went wrong whilst starting server: {e}"),
    ///     Ok(_) => println!("Started server, nice.")
    /// }
    /// ```
    /// 
    /// # Error
    /// 
    /// Returns a FailedOp error if it could not post data. This usually happens if 
    /// you are not allowed to start the server.
    pub async fn start(&self, client: &Client) -> Result<(), Error> {
        http::post(&client, &format!("server/{}/start", &self.id)).await
    }

    /// Stops a server without hibernating the server.
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of calling Client struct.
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// if let Ok(_) = client.my_server("server-name").await?.shut_down(&client).await {
    ///     println!("Successfully stopped server");
    /// }
    /// ```
    /// 
    /// # Error
    /// 
    /// Returns a FailedOp error if it could not post data. This usually happens if
    /// you are not allowed to shut the server down/
    pub async fn shut_down(&self, client: &Client) -> Result<(), Error> {
        http::post(&client, &format!("server/{}/shutdown", &self.id)).await
    }
}