tiger_lib/vic3/data/
buy_packages.rs

1use crate::block::Block;
2use crate::db::{Db, DbKind};
3use crate::everything::Everything;
4use crate::game::GameFlags;
5use crate::item::{Item, ItemLoader};
6use crate::report::{ErrorKey, err};
7use crate::token::Token;
8use crate::validator::Validator;
9
10#[derive(Clone, Debug)]
11pub struct BuyPackage {}
12
13inventory::submit! {
14    ItemLoader::Normal(GameFlags::Vic3, Item::BuyPackage, BuyPackage::add)
15}
16
17impl BuyPackage {
18    pub fn add(db: &mut Db, key: Token, block: Block) {
19        db.add(Item::BuyPackage, key, block, Box::new(Self {}));
20    }
21
22    pub fn crosscheck(_data: &Everything) {
23        // TODO: check that wealth_1 through wealth_99 are all present.
24        // Problem: what would be the loc for the warning about a missing one?
25    }
26}
27
28impl DbKind for BuyPackage {
29    fn validate(&self, key: &Token, block: &Block, data: &Everything) {
30        let mut vd = Validator::new(block, data);
31
32        // Check that this is one of the expected keys, wealth_1 through wealth_99.
33        if let Some(wealth_nr) = key.strip_prefix("wealth_") {
34            if let Some(nr) = wealth_nr.expect_integer() {
35                if !(1..=99).contains(&nr) {
36                    let msg = "expected wealth between 1 and 99";
37                    err(ErrorKey::Range).msg(msg).loc(key).push();
38                }
39            }
40        } else {
41            let msg = "expected wealth_NN format for buy_package keys";
42            err(ErrorKey::Validation).msg(msg).loc(key).push();
43        }
44
45        vd.field_numeric("political_strength");
46        vd.multi_field_validated_block("goods", validate_goods);
47    }
48}
49
50fn validate_goods(block: &Block, data: &Everything) {
51    let mut vd = Validator::new(block, data);
52    vd.unknown_value_fields(|key, value| {
53        data.verify_exists(Item::PopNeed, key);
54        value.expect_integer();
55    });
56}