parent
b815a9b06b
commit
45a2cba2b8
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
@ -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,
|
||||||
|
}
|
Loading…
Reference in New Issue