parent
e03e8adf1f
commit
e183ed7524
@ -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)
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
pub mod item;
|
||||
pub mod recipe;
|
||||
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…
Reference in New Issue