diff --git a/Cargo.toml b/Cargo.toml index 56667cc..252c0cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minecraft-data-rs" -version = "0.4.2" +version = "0.4.3" authors = ["trivernis "] edition = "2018" readme = "README.md" diff --git a/README.md b/README.md index b96e5f3..9d8524a 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,22 @@ This repository is a rust library to access minecraft data. The data itself hosted in the [minecraft-data](https://github.com/PrismarineJS/minecraft-data) repository and included into the library at compile time. +## Usage + +```rust +use std::collections::HashMap; +use minecraft_data_rs::Api; +use minecraft_data_rs::models::food::Food; +use minecraft_data_rs::models::version::Version; + +// create an api wrapper for the latest stable version +let api = Api::latest().expect("failed to retrieve latest version"); +let food: Vec = api.foods.foods_array().unwrap(); + +for food in food { + println!("When eating {} you gain {} food points", food.name, food.food_points); +} +``` # License diff --git a/src/api/mod.rs b/src/api/mod.rs index f5490e6..5bc230c 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -6,7 +6,9 @@ use crate::api::foods::Foods; use crate::api::items::Items; use crate::api::loot::Loot; use crate::api::recipes::Recipes; +use crate::api::versions::latest_stable; use crate::models::version::Version; +use crate::DataResult; use std::sync::Arc; #[cfg(test)] @@ -22,6 +24,8 @@ pub mod loot; pub mod recipes; pub mod versions; +/// A type wrapping access to all the metadata +/// about the selected minecraft version pub struct Api { pub version: Arc, pub items: Items, @@ -35,6 +39,12 @@ pub struct Api { } impl Api { + /// Creates a new API wrapper for the latest version + pub fn latest() -> DataResult { + Ok(Self::new(latest_stable()?)) + } + + /// Creates a new API wrapper for the provided version pub fn new(version: Version) -> Self { let version = Arc::new(version); Self { diff --git a/src/api/tests/versions.rs b/src/api/tests/versions.rs index 023f753..75365ed 100644 --- a/src/api/tests/versions.rs +++ b/src/api/tests/versions.rs @@ -1,4 +1,5 @@ use crate::api::versions::{latest_stable, versions, versions_by_minecraft_version}; +use crate::Api; #[test] fn test_versions() { @@ -16,5 +17,6 @@ fn test_versions_by_minecraft_version() { #[test] fn test_latest_stable_version() { - assert!(latest_stable().is_ok()) + assert!(latest_stable().is_ok()); + assert!(Api::latest().is_ok()); } diff --git a/src/api/versions.rs b/src/api/versions.rs index 66fcc29..756b28b 100644 --- a/src/api/versions.rs +++ b/src/api/versions.rs @@ -1,6 +1,7 @@ use crate::data::{get_common_file, PROTOCOL_VERSIONS_FILE, VERSIONS_FILE}; use crate::models::version::Version; use crate::{DataError, DataResult}; +use itertools::Itertools; use std::collections::HashMap; use std::iter::FromIterator; @@ -25,10 +26,27 @@ pub fn versions_by_minecraft_version() -> DataResult> { /// Returns the latest stable version (hardcoded at the moment) pub fn latest_stable() -> DataResult { - versions_by_minecraft_version()? - .get("1.18.1") - .cloned() - .ok_or(DataError::NotFoundError("1.18.1".to_string())) + let latest = versions()? + .into_iter() + .filter_map(|v| { + let version_string = v.minecraft_version.clone(); + let mut parts = version_string.split("."); + + Some(( + v, + parts.next()?.parse::().ok()?, + parts.next()?.parse::().ok()?, + parts.next().and_then(|p| p.parse::().ok()), + )) + }) + .sorted_by_key(|(_, maj, min, patch)| { + format!("{:#05}.{:#05}.{:#05}", maj, min, patch.unwrap_or(0)) + }) + .map(|(v, _, _, _)| v) + .rev() + .next(); + + latest.ok_or_else(|| DataError::NotFoundError(String::from("latest version"))) } /// Returns a list of available version information diff --git a/src/lib.rs b/src/lib.rs index 88ed9b4..c0948d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,22 @@ +//! This crate is a wrapper for accessing information from [minecraft-data)(https://github.com/PrismarineJS/minecraft-data). +//! +//! Usage: +//! ``` +//! use std::collections::HashMap; +//! use minecraft_data_rs::Api; +//! use minecraft_data_rs::models::food::Food; +//! use minecraft_data_rs::models::version::Version; +//! +//! // create an api wrapper for the latest stable version +//! let api = Api::latest().expect("failed to retrieve latest version"); +//! let food: Vec = api.foods.foods_array().unwrap(); +//! +//! for food in food { +//! println!("When eating {} you gain {} food points", food.name, food.food_points); +//! } +//! ``` +//! + #[macro_use] extern crate serde_derive; @@ -6,5 +25,6 @@ pub(crate) mod data; pub mod models; pub(crate) mod utils; +pub use api::Api; pub use utils::error::DataError; pub use utils::error::DataResult;