~deciduously/fcalc

ref: 5ba32ecfb93e39f3b11e2abaa2ba6d5c7c848544 fcalc/src/item.rs -rw-r--r-- 2.4 KiB
5ba32ecfBen Lovy Recipe lookup 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! An item is something the player can hold.  Some are raw, some are craftable.

use crate::{
    recipe::{Recipe, RECIPES},
    FcalcError, Result,
};
use lazy_static::lazy_static;
use serde::Deserialize;
use std::fmt;

lazy_static! {
    pub static ref ITEMS: Items = Items::new();
}

#[derive(Clone, Debug, Deserialize)]
pub enum Item {
    Raw(RawMaterial),
    Product(Product),
}

impl Item {
    pub fn recipe(&self) -> Option<&Recipe> {
        match self {
            Item::Raw(_) => None,
            Item::Product(p) => RECIPES.lookup(&p),
        }
    }

    pub fn as_product(&self) -> Result<Product> {
        match self {
            Item::Product(p) => Ok(p.clone()),
            _ => Err(FcalcError::ProductTypeError),
        }
    }

    pub fn as_raw(&self) -> Result<RawMaterial> {
        match self {
            Item::Raw(r) => Ok(r.clone()),
            _ => Err(FcalcError::ProductTypeError),
        }
    }
}

/// A base-level raw material, not craftable.
#[derive(Clone, Debug, Deserialize)]
pub struct RawMaterial {
    name: String,
}

impl fmt::Display for RawMaterial {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)]
pub struct Product {
    name: String,
}

impl fmt::Display for Product {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

/// The top-level Items struct, containing all supported items read from TOML.
#[derive(Debug, Deserialize)]
pub struct Items {
    raw_materials: Vec<RawMaterial>,
    products: Vec<Product>,
}

impl Items {
    fn new() -> Self {
        Self::default()
    }

    /// Find an item by name
    pub fn lookup(&self, name: &str) -> Option<Item> {
        for r in &self.raw_materials {
            if r.name == name {
                return Some(Item::Raw(r.clone()));
            }
        }

        for p in &self.products {
            if p.name == name {
                return Some(Item::Product(p.clone()));
            }
        }

        None
    }
}

impl Default for Items {
    fn default() -> Self {
        let input = include_str!("items.toml");
        toml::from_str(input).expect("Could not read items file")
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use pretty_assertions::assert_eq;
    #[test]
    fn it_loads_all_items() {
        assert_eq!(ITEMS.raw_materials.len(), 2);
        assert_eq!(ITEMS.products.len(), 1)
    }
}