Add recipe api

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

@ -2,5 +2,6 @@
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/minecraft-data" vcs="Git" />
</component> </component>
</project> </project>

@ -1,4 +1,5 @@
use crate::api::items::Items; use crate::api::items::Items;
use crate::api::recipes::Recipes;
use crate::models::version::Version; use crate::models::version::Version;
use std::sync::Arc; use std::sync::Arc;
@ -6,11 +7,13 @@ use std::sync::Arc;
mod tests; mod tests;
pub mod items; pub mod items;
mod recipes;
pub mod versions; pub mod versions;
pub struct Api { pub struct Api {
version: Arc<Version>, pub version: Arc<Version>,
items: Items, pub items: Items,
pub recipes: Recipes,
} }
impl Api { impl Api {
@ -19,6 +22,7 @@ impl Api {
Self { Self {
version: Arc::clone(&version), version: Arc::clone(&version),
items: Items::new(Arc::clone(&version)), items: Items::new(Arc::clone(&version)),
recipes: Recipes::new(Arc::clone(&version)),
} }
} }
} }

@ -0,0 +1,23 @@
use crate::data::{get_version_specific_file, RECIPES_FILE};
use crate::models::recipe::Recipe;
use crate::models::version::Version;
use crate::{DataError, DataResult};
use std::collections::HashMap;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct Recipes {
version: Arc<Version>,
}
impl Recipes {
pub fn new(version: Arc<Version>) -> Self {
Self { version }
}
/// Returns a list of recipes indexed by item ID
pub fn recipes(&self) -> DataResult<HashMap<u32, Vec<Recipe>>> {
let content = get_version_specific_file(&self.version, RECIPES_FILE)?;
serde_json::from_str::<HashMap<u32, Vec<Recipe>>>(&*content).map_err(DataError::from)
}
}

@ -9,3 +9,16 @@ pub fn test_items_array() {
assert_ne!(api.items.items_array().unwrap().len(), 0) assert_ne!(api.items.items_array().unwrap().len(), 0)
} }
} }
#[test]
pub fn test_items_by_name() {
let versions = get_test_versions();
for version in versions {
let api = get_api(version);
let by_name = api.items.items_by_name().unwrap();
assert!(by_name.get("bread").is_some());
assert!(by_name.get("stone").is_some());
assert_eq!(by_name.get("ender_pearl").unwrap().stack_size, 16)
}
}

@ -3,6 +3,7 @@ use crate::api::Api;
use crate::models::version::Version; use crate::models::version::Version;
mod items; mod items;
mod recipes;
mod versions; mod versions;
fn get_api(version: Version) -> Api { fn get_api(version: Version) -> Api {

@ -0,0 +1,14 @@
use crate::api::tests::{get_api, get_test_versions};
#[test]
pub fn test_recipes() {
let versions = get_test_versions();
for version in versions {
let api = get_api(version);
let recipes = api.recipes.recipes().unwrap();
let bread_id = api.items.items_by_name().unwrap().get("bread").unwrap().id;
assert_ne!(recipes.len(), 0);
assert!(recipes.get(&bread_id).is_some());
}
}

@ -33,8 +33,6 @@ pub fn latest_stable() -> DataResult<Version> {
/// Returns a list of available version information /// Returns a list of available version information
pub(crate) fn available_versions() -> DataResult<Vec<String>> { pub(crate) fn available_versions() -> DataResult<Vec<String>> {
Ok(get_common_file(VERSIONS_FILE)? let content = get_common_file(VERSIONS_FILE)?;
.split_whitespace() serde_json::from_str::<Vec<String>>(&*content).map_err(DataError::from)
.map(|s| s.to_string())
.collect())
} }

@ -41,7 +41,6 @@ pub fn get_version_specific_file(version: &Version, filename: &str) -> DataResul
version.previous_major(), version.previous_major(),
version.previous_major_first(), version.previous_major_first(),
]; ];
println!("{:?}", search_folders);
let mut data = None; let mut data = None;
for folder in search_folders { for folder in search_folders {

@ -1,2 +1,3 @@
pub mod item; pub mod item;
pub mod recipe;
pub mod version; pub mod version;

@ -0,0 +1,46 @@
#[derive(Deserialize, Debug, Clone)]
#[serde(
rename_all(deserialize = "camelCase", serialize = "snake_case"),
untagged
)]
pub enum Recipe {
Shaped(ShapedRecipe),
Shapeless(ShapelessRecipe),
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
pub struct ShapedRecipe {
result: RecipeItem,
in_shape: Shape,
out_shape: Option<Shape>,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
pub struct ShapelessRecipe {
result: RecipeItem,
ingredients: Vec<RecipeItem>,
}
pub type Shape = Vec<Vec<RecipeItem>>;
#[derive(Deserialize, Debug, Clone)]
#[serde(
rename_all(deserialize = "camelCase", serialize = "snake_case"),
untagged
)]
pub enum RecipeItem {
ID(u32),
IDMetadataArray([u32; 2]),
IDMetadataCountObject(IDMetadataCountObject),
Null(Option<()>),
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all(deserialize = "camelCase", serialize = "snake_case"))]
pub struct IDMetadataCountObject {
id: i32,
metadata: Option<i32>,
count: Option<u32>,
}
Loading…
Cancel
Save