Add dynamic latest version

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/6/head
trivernis 3 years ago
parent 225be978c2
commit aa151453de
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,6 +1,6 @@
[package] [package]
name = "minecraft-data-rs" name = "minecraft-data-rs"
version = "0.4.2" version = "0.4.3"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "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 The data itself hosted in the [minecraft-data](https://github.com/PrismarineJS/minecraft-data) repository
and included into the library at compile time. 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<Food> = api.foods.foods_array().unwrap();
for food in food {
println!("When eating {} you gain {} food points", food.name, food.food_points);
}
```
# License # License

@ -6,7 +6,9 @@ use crate::api::foods::Foods;
use crate::api::items::Items; use crate::api::items::Items;
use crate::api::loot::Loot; use crate::api::loot::Loot;
use crate::api::recipes::Recipes; use crate::api::recipes::Recipes;
use crate::api::versions::latest_stable;
use crate::models::version::Version; use crate::models::version::Version;
use crate::DataResult;
use std::sync::Arc; use std::sync::Arc;
#[cfg(test)] #[cfg(test)]
@ -22,6 +24,8 @@ pub mod loot;
pub mod recipes; pub mod recipes;
pub mod versions; pub mod versions;
/// A type wrapping access to all the metadata
/// about the selected minecraft version
pub struct Api { pub struct Api {
pub version: Arc<Version>, pub version: Arc<Version>,
pub items: Items, pub items: Items,
@ -35,6 +39,12 @@ pub struct Api {
} }
impl Api { impl Api {
/// Creates a new API wrapper for the latest version
pub fn latest() -> DataResult<Self> {
Ok(Self::new(latest_stable()?))
}
/// Creates a new API wrapper for the provided version
pub fn new(version: Version) -> Self { pub fn new(version: Version) -> Self {
let version = Arc::new(version); let version = Arc::new(version);
Self { Self {

@ -1,4 +1,5 @@
use crate::api::versions::{latest_stable, versions, versions_by_minecraft_version}; use crate::api::versions::{latest_stable, versions, versions_by_minecraft_version};
use crate::Api;
#[test] #[test]
fn test_versions() { fn test_versions() {
@ -16,5 +17,6 @@ fn test_versions_by_minecraft_version() {
#[test] #[test]
fn test_latest_stable_version() { fn test_latest_stable_version() {
assert!(latest_stable().is_ok()) assert!(latest_stable().is_ok());
assert!(Api::latest().is_ok());
} }

@ -1,6 +1,7 @@
use crate::data::{get_common_file, PROTOCOL_VERSIONS_FILE, VERSIONS_FILE}; use crate::data::{get_common_file, PROTOCOL_VERSIONS_FILE, VERSIONS_FILE};
use crate::models::version::Version; use crate::models::version::Version;
use crate::{DataError, DataResult}; use crate::{DataError, DataResult};
use itertools::Itertools;
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::FromIterator; use std::iter::FromIterator;
@ -25,10 +26,27 @@ pub fn versions_by_minecraft_version() -> DataResult<HashMap<String, Version>> {
/// Returns the latest stable version (hardcoded at the moment) /// Returns the latest stable version (hardcoded at the moment)
pub fn latest_stable() -> DataResult<Version> { pub fn latest_stable() -> DataResult<Version> {
versions_by_minecraft_version()? let latest = versions()?
.get("1.18.1") .into_iter()
.cloned() .filter_map(|v| {
.ok_or(DataError::NotFoundError("1.18.1".to_string())) let version_string = v.minecraft_version.clone();
let mut parts = version_string.split(".");
Some((
v,
parts.next()?.parse::<u32>().ok()?,
parts.next()?.parse::<u32>().ok()?,
parts.next().and_then(|p| p.parse::<u32>().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 /// Returns a list of available version information

@ -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<Food> = api.foods.foods_array().unwrap();
//!
//! for food in food {
//! println!("When eating {} you gain {} food points", food.name, food.food_points);
//! }
//! ```
//!
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
@ -6,5 +25,6 @@ pub(crate) mod data;
pub mod models; pub mod models;
pub(crate) mod utils; pub(crate) mod utils;
pub use api::Api;
pub use utils::error::DataError; pub use utils::error::DataError;
pub use utils::error::DataResult; pub use utils::error::DataResult;

Loading…
Cancel
Save