Add foods api

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 4 years ago
parent b815a9b06b
commit 45a2cba2b8
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -0,0 +1,41 @@
use crate::data::{get_version_specific_file, FOODS_FILE};
use crate::models::food::Food;
use crate::models::version::Version;
use crate::DataResult;
use std::collections::HashMap;
use std::iter::FromIterator;
use std::sync::Arc;
pub struct Foods {
version: Arc<Version>,
}
impl Foods {
pub fn new(version: Arc<Version>) -> Self {
Self { version }
}
/// Returns the unindexed list of food
pub fn foods_array(&self) -> DataResult<Vec<Food>> {
let content = get_version_specific_file(&self.version, FOODS_FILE)?;
let foods = serde_json::from_str(&content)?;
Ok(foods)
}
/// Returns food indexed by id
pub fn foods(&self) -> DataResult<HashMap<u32, Food>> {
let foods = self.foods_array()?;
let food_map = HashMap::from_iter(foods.into_iter().map(|f| (f.id, f)));
Ok(food_map)
}
/// Returns food indexed by name
pub fn foods_by_name(&self) -> DataResult<HashMap<String, Food>> {
let foods = self.foods_array()?;
let food_map = HashMap::from_iter(foods.into_iter().map(|f| (f.name.clone(), f)));
Ok(food_map)
}
}

@ -1,16 +1,18 @@
use crate::api::blocks::Blocks;
use crate::api::enchantments::Enchantments;
use crate::api::foods::Foods;
use crate::api::items::Items;
use crate::api::loot::Loot;
use crate::api::recipes::Recipes;
use crate::models::version::Version;
use std::sync::Arc;
use crate::api::blocks::Blocks;
#[cfg(test)]
mod tests;
pub mod blocks;
pub mod enchantments;
pub mod foods;
pub mod items;
pub mod loot;
pub mod recipes;
@ -23,6 +25,7 @@ pub struct Api {
pub enchantments: Enchantments,
pub loot: Loot,
pub blocks: Blocks,
pub foods: Foods,
}
impl Api {
@ -34,7 +37,8 @@ impl Api {
recipes: Recipes::new(Arc::clone(&version)),
enchantments: Enchantments::new(Arc::clone(&version)),
loot: Loot::new(Arc::clone(&version)),
blocks: Blocks::new(Arc::clone(&version))
blocks: Blocks::new(Arc::clone(&version)),
foods: Foods::new(Arc::clone(&version)),
}
}
}

@ -0,0 +1,23 @@
use crate::api::tests::{get_api, get_test_versions};
#[test]
pub fn test_foods_array() {
let versions = get_test_versions();
for version in versions {
let api = get_api(version);
assert_ne!(api.foods.foods_array().unwrap().len(), 0)
}
}
#[test]
pub fn test_foods_by_name() {
let versions = get_test_versions();
for version in versions {
let api = get_api(version);
let by_name = api.foods.foods_by_name().unwrap();
assert!(by_name.get("bread").is_some());
assert!(by_name.get("golden_carrot").is_some());
}
}

@ -2,12 +2,13 @@ use crate::api::versions::{available_versions, versions};
use crate::api::Api;
use crate::models::version::Version;
mod blocks;
mod enchantments;
mod foods;
mod items;
mod loot;
mod recipes;
mod versions;
mod blocks;
fn get_api(version: Version) -> Api {
Api::new(version)

@ -13,6 +13,7 @@ pub static BLOCKS_FILE: &str = "blocks";
pub static COMMANDS_FILE: &str = "commands";
pub static ENTITIES_FILE: &str = "entities";
pub static ENTITY_LOOT_FILE: &str = "entityLoot";
pub static FOODS_FILE: &str = "foods";
pub static ITEMS_FILE: &str = "items";
pub static LOGIN_PACKET_FILE: &str = "loginPacket";
pub static MATERIALS_FILE: &str = "materials";

@ -0,0 +1,20 @@
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
pub struct Food {
pub id: u32,
pub display_name: String,
pub stack_size: u8,
pub name: String,
pub food_points: u8,
pub saturation: f32,
pub effective_quality: f32,
pub saturation_ratio: f32,
pub variations: Option<Vec<Variation>>,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
pub struct Variation {
pub metadata: u32,
pub display_name: String,
}

@ -5,3 +5,4 @@ pub mod entity_loot;
pub mod item;
pub mod recipe;
pub mod version;
pub mod food;

Loading…
Cancel
Save