From 45a2cba2b8a459877fd1c3a51967f0c93c2bac2b Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 18 Apr 2021 15:41:57 +0200 Subject: [PATCH] Add foods api Signed-off-by: trivernis --- src/api/foods.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/api/mod.rs | 8 ++++++-- src/api/tests/foods.rs | 23 +++++++++++++++++++++++ src/api/tests/mod.rs | 3 ++- src/data/mod.rs | 1 + src/models/food.rs | 20 ++++++++++++++++++++ src/models/mod.rs | 1 + 7 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 src/api/foods.rs create mode 100644 src/api/tests/foods.rs create mode 100644 src/models/food.rs diff --git a/src/api/foods.rs b/src/api/foods.rs new file mode 100644 index 0000000..28c108e --- /dev/null +++ b/src/api/foods.rs @@ -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, +} + +impl Foods { + pub fn new(version: Arc) -> Self { + Self { version } + } + + /// Returns the unindexed list of food + pub fn foods_array(&self) -> DataResult> { + 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> { + 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> { + let foods = self.foods_array()?; + let food_map = HashMap::from_iter(foods.into_iter().map(|f| (f.name.clone(), f))); + + Ok(food_map) + } +} diff --git a/src/api/mod.rs b/src/api/mod.rs index 6e9e594..10eeb9c 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -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)), } } } diff --git a/src/api/tests/foods.rs b/src/api/tests/foods.rs new file mode 100644 index 0000000..5f36c0b --- /dev/null +++ b/src/api/tests/foods.rs @@ -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()); + } +} diff --git a/src/api/tests/mod.rs b/src/api/tests/mod.rs index bdc8339..62c222f 100644 --- a/src/api/tests/mod.rs +++ b/src/api/tests/mod.rs @@ -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) diff --git a/src/data/mod.rs b/src/data/mod.rs index b82d635..3e601b1 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -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"; diff --git a/src/models/food.rs b/src/models/food.rs new file mode 100644 index 0000000..cbd99a5 --- /dev/null +++ b/src/models/food.rs @@ -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>, +} + +#[derive(Deserialize, Debug, Clone)] +#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))] +pub struct Variation { + pub metadata: u32, + pub display_name: String, +} diff --git a/src/models/mod.rs b/src/models/mod.rs index 3997629..03f9ff4 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -5,3 +5,4 @@ pub mod entity_loot; pub mod item; pub mod recipe; pub mod version; +pub mod food;