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
use crate::{
    versioning::{MipComponent, MipStore},
    versioning_factory::{FactoryError, FactoryStrategy, VersioningFactory},
};
use massa_hash::Hash;
use massa_models::address::{Address, SCAddress, SCAddressV0, UserAddress, UserAddressV0};

#[derive(Clone)]
pub struct AddressFactory {
    pub mip_store: MipStore,
}

pub enum AddressArgs {
    User { hash: Hash },
    SC { hash: Hash },
}

impl VersioningFactory for AddressFactory {
    type Output = Address;
    type Error = FactoryError;
    type Arguments = AddressArgs;

    fn get_component() -> MipComponent {
        MipComponent::Address
    }

    fn get_versioning_store(&self) -> MipStore {
        self.mip_store.clone()
    }

    fn create(
        &self,
        args: &Self::Arguments,
        strategy: FactoryStrategy,
    ) -> Result<Self::Output, Self::Error> {
        let version = self.get_component_version_with_strategy(strategy)?;

        let output: Address = match version {
            0 => match args {
                AddressArgs::User { hash } => {
                    Address::User(UserAddress::UserAddressV0(UserAddressV0(*hash)))
                }
                AddressArgs::SC { hash } => Address::SC(SCAddress::SCAddressV0(SCAddressV0(*hash))),
            },
            v => return Err(FactoryError::UnimplementedVersion(v)),
        };

        Ok(output)
    }
}