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
use alloc::vec::Vec;

use super::{AccountStorageDelta, StorageMapDelta, Word};
use crate::AccountDeltaError;

#[derive(Clone, Debug, Default)]
pub struct AccountStorageDeltaBuilder {
    pub cleared_items: Vec<u8>,
    pub updated_items: Vec<(u8, Word)>,
    pub updated_maps: Vec<(u8, StorageMapDelta)>,
}

impl AccountStorageDeltaBuilder {
    // CONSTRUCTORS
    // -------------------------------------------------------------------------------------------
    pub fn new() -> Self {
        Self::default()
    }

    // MODIFIERS
    // -------------------------------------------------------------------------------------------
    pub fn add_cleared_items<I>(mut self, items: I) -> Self
    where
        I: IntoIterator<Item = u8>,
    {
        self.cleared_items.extend(items);
        self
    }

    pub fn add_updated_items<I>(mut self, items: I) -> Self
    where
        I: IntoIterator<Item = (u8, Word)>,
    {
        self.updated_items.extend(items);
        self
    }

    pub fn add_updated_maps<I>(mut self, items: I) -> Self
    where
        I: IntoIterator<Item = (u8, StorageMapDelta)>,
    {
        self.updated_maps.extend(items);
        self
    }

    // BUILDERS
    // -------------------------------------------------------------------------------------------
    pub fn build(self) -> Result<AccountStorageDelta, AccountDeltaError> {
        let delta = AccountStorageDelta {
            cleared_items: self.cleared_items,
            updated_items: self.updated_items,
            updated_maps: self.updated_maps,
        };
        delta.validate()?;
        Ok(delta)
    }
}