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
use crate::DebugApi;

use super::*;

use alloc::vec::Vec;
use std::{collections::HashMap, fmt};

pub struct ContractMap {
    contract_objs: HashMap<Vec<u8>, ContractContainer>,
}

impl fmt::Debug for ContractMap {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ContractMap").finish()
    }
}

impl ContractMap {
    pub fn new() -> Self {
        ContractMap {
            contract_objs: HashMap::new(),
        }
    }

    pub fn get_contract(
        &self,
        contract_identifier: &[u8],
        _debug_api: DebugApi,
    ) -> &ContractContainer {
        if let Some(contract_contatiner) = self.contract_objs.get(contract_identifier) {
            contract_contatiner
        } else {
            unknown_contract_panic(contract_identifier)
        }
    }

    pub fn register_contract(
        &mut self,
        contract_bytes: Vec<u8>,
        contract_container: ContractContainer,
    ) {
        let previous_entry = self
            .contract_objs
            .insert(contract_bytes, contract_container);
        assert!(previous_entry.is_none(), "contract inserted twice");
    }

    pub fn contains_contract(&self, contract_bytes: &[u8]) -> bool {
        self.contract_objs.contains_key(contract_bytes)
    }
}

fn unknown_contract_panic(contract_identifier: &[u8]) -> ! {
    if let Ok(s) = std::str::from_utf8(contract_identifier) {
        panic!("Unknown contract: {s}")
    } else {
        panic!(
            "Unknown contract of length {} bytes",
            contract_identifier.len()
        )
    }
}

impl Default for ContractMap {
    fn default() -> Self {
        Self::new()
    }
}