Minor refactoring

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/6/head
trivernis 3 years ago
parent 660a077847
commit 2ef427ced8
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,6 +1,6 @@
[package] [package]
name = "minecraft-data-rs" name = "minecraft-data-rs"
version = "0.4.4" version = "0.4.5"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"

@ -3,7 +3,6 @@ use crate::models::biome::Biome;
use crate::models::version::Version; use crate::models::version::Version;
use crate::DataResult; use crate::DataResult;
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::FromIterator;
use std::sync::Arc; use std::sync::Arc;
pub struct Biomes { pub struct Biomes {
@ -26,7 +25,7 @@ impl Biomes {
/// Returns the biomes indexed by id /// Returns the biomes indexed by id
pub fn biomes(&self) -> DataResult<HashMap<u32, Biome>> { pub fn biomes(&self) -> DataResult<HashMap<u32, Biome>> {
let biomes = self.biomes_array()?; let biomes = self.biomes_array()?;
let biomes_map = HashMap::from_iter(biomes.into_iter().map(|b| (b.id, b))); let biomes_map = biomes.into_iter().map(|b| (b.id, b)).collect();
Ok(biomes_map) Ok(biomes_map)
} }
@ -34,7 +33,7 @@ impl Biomes {
/// Returns the biomes indexed by name /// Returns the biomes indexed by name
pub fn biomes_by_name(&self) -> DataResult<HashMap<String, Biome>> { pub fn biomes_by_name(&self) -> DataResult<HashMap<String, Biome>> {
let biomes = self.biomes_array()?; let biomes = self.biomes_array()?;
let biomes_map = HashMap::from_iter(biomes.into_iter().map(|b| (b.name.clone(), b))); let biomes_map = biomes.into_iter().map(|b| (b.name.clone(), b)).collect();
Ok(biomes_map) Ok(biomes_map)
} }

@ -4,7 +4,6 @@ use crate::models::block_collision_shapes::BlockCollisionShapes;
use crate::models::version::Version; use crate::models::version::Version;
use crate::DataResult; use crate::DataResult;
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::FromIterator;
use std::sync::Arc; use std::sync::Arc;
pub struct Blocks { pub struct Blocks {
@ -27,7 +26,7 @@ impl Blocks {
/// Returns the blocks indexed by ID /// Returns the blocks indexed by ID
pub fn blocks(&self) -> DataResult<HashMap<u32, Block>> { pub fn blocks(&self) -> DataResult<HashMap<u32, Block>> {
let blocks = self.blocks_array()?; let blocks = self.blocks_array()?;
let blocks_map = HashMap::from_iter(blocks.into_iter().map(|b| (b.id, b))); let blocks_map = blocks.into_iter().map(|b| (b.id, b)).collect();
Ok(blocks_map) Ok(blocks_map)
} }
@ -35,7 +34,7 @@ impl Blocks {
/// Returns the blocks indexed by name /// Returns the blocks indexed by name
pub fn blocks_by_name(&self) -> DataResult<HashMap<String, Block>> { pub fn blocks_by_name(&self) -> DataResult<HashMap<String, Block>> {
let blocks = self.blocks_array()?; let blocks = self.blocks_array()?;
let blocks_map = HashMap::from_iter(blocks.into_iter().map(|b| (b.name.clone(), b))); let blocks_map = blocks.into_iter().map(|b| (b.name.clone(), b)).collect();
Ok(blocks_map) Ok(blocks_map)
} }

@ -4,7 +4,6 @@ use crate::models::version::Version;
use crate::{DataError, DataResult}; use crate::{DataError, DataResult};
use itertools::*; use itertools::*;
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::FromIterator;
use std::sync::Arc; use std::sync::Arc;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -26,28 +25,30 @@ impl Enchantments {
/// Returns a map of enchantments indexed by ID /// Returns a map of enchantments indexed by ID
pub fn enchantments(&self) -> DataResult<HashMap<u32, Enchantment>> { pub fn enchantments(&self) -> DataResult<HashMap<u32, Enchantment>> {
Ok(HashMap::from_iter( Ok(self
self.enchantments_array()?.into_iter().map(|e| (e.id, e)), .enchantments_array()?
)) .into_iter()
.map(|e| (e.id, e))
.collect())
} }
/// Returns a map of enchantments indexed by Name /// Returns a map of enchantments indexed by Name
pub fn enchantments_by_name(&self) -> DataResult<HashMap<String, Enchantment>> { pub fn enchantments_by_name(&self) -> DataResult<HashMap<String, Enchantment>> {
Ok(HashMap::from_iter( Ok(self
self.enchantments_array()? .enchantments_array()?
.into_iter() .into_iter()
.map(|e| (e.name.clone(), e)), .map(|e| (e.name.clone(), e))
)) .collect())
} }
/// Returns enchantments grouped by category /// Returns enchantments grouped by category
pub fn enchantments_by_category(&self) -> DataResult<HashMap<String, Vec<Enchantment>>> { pub fn enchantments_by_category(&self) -> DataResult<HashMap<String, Vec<Enchantment>>> {
Ok(HashMap::from_iter( Ok(self
self.enchantments_array()? .enchantments_array()?
.into_iter() .into_iter()
.group_by(|e| e.category.clone()) .group_by(|e| e.category.clone())
.into_iter() .into_iter()
.map(|(key, group)| (key, group.collect())), .map(|(key, group)| (key, group.collect()))
)) .collect())
} }
} }

@ -3,7 +3,6 @@ use crate::models::entity::Entity;
use crate::models::version::Version; use crate::models::version::Version;
use crate::DataResult; use crate::DataResult;
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::FromIterator;
use std::sync::Arc; use std::sync::Arc;
pub struct Entities { pub struct Entities {
@ -26,7 +25,7 @@ impl Entities {
/// Returns entities indexed by name /// Returns entities indexed by name
pub fn entities_by_name(&self) -> DataResult<HashMap<String, Entity>> { pub fn entities_by_name(&self) -> DataResult<HashMap<String, Entity>> {
let entities = self.entities_array()?; let entities = self.entities_array()?;
let entities_map = HashMap::from_iter(entities.into_iter().map(|e| (e.name.clone(), e))); let entities_map = entities.into_iter().map(|e| (e.name.clone(), e)).collect();
Ok(entities_map) Ok(entities_map)
} }
@ -34,7 +33,7 @@ impl Entities {
/// Returns entities indexed by id /// Returns entities indexed by id
pub fn entities(&self) -> DataResult<HashMap<u32, Entity>> { pub fn entities(&self) -> DataResult<HashMap<u32, Entity>> {
let entities = self.entities_array()?; let entities = self.entities_array()?;
let entities_map = HashMap::from_iter(entities.into_iter().map(|e| (e.id, e))); let entities_map = entities.into_iter().map(|e| (e.id, e)).collect();
Ok(entities_map) Ok(entities_map)
} }

@ -3,7 +3,6 @@ use crate::models::food::Food;
use crate::models::version::Version; use crate::models::version::Version;
use crate::DataResult; use crate::DataResult;
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::FromIterator;
use std::sync::Arc; use std::sync::Arc;
pub struct Foods { pub struct Foods {
@ -26,7 +25,7 @@ impl Foods {
/// Returns food indexed by id /// Returns food indexed by id
pub fn foods(&self) -> DataResult<HashMap<u32, Food>> { pub fn foods(&self) -> DataResult<HashMap<u32, Food>> {
let foods = self.foods_array()?; let foods = self.foods_array()?;
let food_map = HashMap::from_iter(foods.into_iter().map(|f| (f.id, f))); let food_map = foods.into_iter().map(|f| (f.id, f)).collect();
Ok(food_map) Ok(food_map)
} }
@ -34,7 +33,7 @@ impl Foods {
/// Returns food indexed by name /// Returns food indexed by name
pub fn foods_by_name(&self) -> DataResult<HashMap<String, Food>> { pub fn foods_by_name(&self) -> DataResult<HashMap<String, Food>> {
let foods = self.foods_array()?; let foods = self.foods_array()?;
let food_map = HashMap::from_iter(foods.into_iter().map(|f| (f.name.clone(), f))); let food_map = foods.into_iter().map(|f| (f.name.clone(), f)).collect();
Ok(food_map) Ok(food_map)
} }

@ -3,7 +3,6 @@ use crate::models::item::Item;
use crate::models::version::Version; use crate::models::version::Version;
use crate::{DataError, DataResult}; use crate::{DataError, DataResult};
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::FromIterator;
use std::sync::Arc; use std::sync::Arc;
/// API to access item information /// API to access item information
@ -25,15 +24,19 @@ impl Items {
/// Returns the items indexed by name /// Returns the items indexed by name
pub fn items_by_name(&self) -> DataResult<HashMap<String, Item>> { pub fn items_by_name(&self) -> DataResult<HashMap<String, Item>> {
Ok(HashMap::from_iter( Ok(self
self.items_array()?.into_iter().map(|i| (i.name.clone(), i)), .items_array()?
)) .into_iter()
.map(|i| (i.name.clone(), i))
.collect())
} }
/// Returns the items indexed by ID /// Returns the items indexed by ID
pub fn items(&self) -> DataResult<HashMap<u32, Item>> { pub fn items(&self) -> DataResult<HashMap<u32, Item>> {
Ok(HashMap::from_iter( Ok(self
self.items_array()?.into_iter().map(|i| (i.id.clone(), i)), .items_array()?
)) .into_iter()
.map(|i| (i.id.clone(), i))
.collect())
} }
} }

@ -4,7 +4,6 @@ use crate::models::entity_loot::EntityLoot;
use crate::models::version::Version; use crate::models::version::Version;
use crate::DataResult; use crate::DataResult;
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::FromIterator;
use std::sync::Arc; use std::sync::Arc;
/// API to access item information /// API to access item information
@ -28,7 +27,7 @@ impl Loot {
/// Returns the entity loot indexed by entity name /// Returns the entity loot indexed by entity name
pub fn entity_loot(&self) -> DataResult<HashMap<String, EntityLoot>> { pub fn entity_loot(&self) -> DataResult<HashMap<String, EntityLoot>> {
let loot = self.entity_loot_array()?; let loot = self.entity_loot_array()?;
let loot_map = HashMap::from_iter(loot.into_iter().map(|l| (l.entity.clone(), l))); let loot_map = loot.into_iter().map(|l| (l.entity.clone(), l)).collect();
Ok(loot_map) Ok(loot_map)
} }
@ -44,7 +43,7 @@ impl Loot {
/// Returns the block loot indexed by block name /// Returns the block loot indexed by block name
pub fn block_loot(&self) -> DataResult<HashMap<String, BlockLoot>> { pub fn block_loot(&self) -> DataResult<HashMap<String, BlockLoot>> {
let loot = self.block_loot_array()?; let loot = self.block_loot_array()?;
let loot_map = HashMap::from_iter(loot.into_iter().map(|l| (l.block.clone(), l))); let loot_map = loot.into_iter().map(|l| (l.block.clone(), l)).collect();
Ok(loot_map) Ok(loot_map)
} }

@ -3,7 +3,6 @@ use crate::models::version::Version;
use crate::{DataError, DataResult}; use crate::{DataError, DataResult};
use itertools::Itertools; use itertools::Itertools;
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::FromIterator;
/// Returns the unsorted list of versions /// Returns the unsorted list of versions
pub fn versions() -> DataResult<Vec<Version>> { pub fn versions() -> DataResult<Vec<Version>> {
@ -15,11 +14,10 @@ pub fn versions() -> DataResult<Vec<Version>> {
/// Returns the versions indexed by minecraft version /// Returns the versions indexed by minecraft version
pub fn versions_by_minecraft_version() -> DataResult<HashMap<String, Version>> { pub fn versions_by_minecraft_version() -> DataResult<HashMap<String, Version>> {
let indexed_versions = HashMap::from_iter( let indexed_versions = versions()?
versions()? .into_iter()
.into_iter() .map(|v| (v.minecraft_version.clone(), v))
.map(|v| (v.minecraft_version.clone(), v)), .collect();
);
Ok(indexed_versions) Ok(indexed_versions)
} }

Loading…
Cancel
Save