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
//! Execution transformation logs.

use std::{iter::IntoIterator, vec::IntoIter};

use datasize::DataSize;

use casper_types::{
    ExecutionEffect as JsonExecutionEffect, Key, TransformEntry as JsonTransformEntry,
};

use crate::shared::transform::Transform;

/// A log of all transforms produced during execution.
#[derive(Debug, Default, Clone, Eq, PartialEq, DataSize)]
pub struct ExecutionJournal(Vec<(Key, Transform)>);

impl ExecutionJournal {
    /// Constructs a new `ExecutionJournal`.
    pub fn new(inner: Vec<(Key, Transform)>) -> Self {
        ExecutionJournal(inner)
    }

    /// Whether the journal is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// How many transforms are recorded in the journal.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Adds a transform to the journal.
    pub fn push(&mut self, entry: (Key, Transform)) {
        self.0.push(entry)
    }

    /// Returns an iterator over the journal entries.
    pub fn iter(&self) -> impl Iterator<Item = &(Key, Transform)> {
        self.0.iter()
    }
}

impl From<&ExecutionJournal> for JsonExecutionEffect {
    fn from(execution_journal: &ExecutionJournal) -> Self {
        Self::new(
            execution_journal
                .0
                .iter()
                .map(|(key, transform)| JsonTransformEntry {
                    key: key.to_formatted_string(),
                    transform: transform.into(),
                })
                .collect(),
        )
    }
}

impl From<ExecutionJournal> for JsonExecutionEffect {
    fn from(execution_journal: ExecutionJournal) -> Self {
        Self::new(
            execution_journal
                .0
                .iter()
                .map(|(key, transform)| JsonTransformEntry {
                    key: key.to_formatted_string(),
                    transform: transform.into(),
                })
                .collect(),
        )
    }
}

impl IntoIterator for ExecutionJournal {
    type Item = (Key, Transform);
    type IntoIter = IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl Extend<(Key, Transform)> for ExecutionJournal {
    fn extend<I: IntoIterator<Item = (Key, Transform)>>(&mut self, iter: I) {
        self.0.extend(iter)
    }
}