iota_bundle_preview/bundle/
bundle.rs1use crate::transaction::{
2 Address,
3 Hash,
4 Transaction,
5 TransactionField,
6 Transactions,
7};
8
9use std::collections::HashMap;
10
11pub struct Bundle(pub(crate) Transactions);
12
13impl Bundle {
14 pub fn get(&self, index: usize) -> Option<&Transaction> {
16 self.0.get(index)
17 }
18
19 pub fn len(&self) -> usize {
21 self.0.len()
22 }
23
24 pub fn hash(&self) -> &Hash {
26 self.get(0).unwrap().bundle()
28 }
29
30 pub fn tail(&self) -> &Transaction {
32 self.get(0).unwrap()
34 }
35
36 pub fn head(&self) -> &Transaction {
38 self.get(self.len() - 1).unwrap()
40 }
41
42 pub fn trunk(&self) -> &Hash {
44 self.head().trunk()
45 }
46
47 pub fn branch(&self) -> &Hash {
49 self.head().branch()
50 }
51
52 pub fn ledger_diff(&self) -> HashMap<Address, i64> {
54 let mut diff = HashMap::new();
55
56 for transaction in self {
57 if *transaction.value.to_inner() != 0 {
58 *diff.entry(transaction.address().clone()).or_insert(0) += *transaction.value.to_inner();
59 }
60 }
61
62 diff
63 }
64}
65
66impl IntoIterator for Bundle {
67 type Item = Transaction;
68 type IntoIter = std::vec::IntoIter<Transaction>;
69
70 fn into_iter(self) -> Self::IntoIter {
72 (self.0).0.into_iter()
73 }
74}
75
76impl<'a> IntoIterator for &'a Bundle {
77 type Item = &'a Transaction;
78 type IntoIter = std::slice::Iter<'a, Transaction>;
79
80 fn into_iter(self) -> Self::IntoIter {
82 (&(self.0).0).into_iter()
83 }
84}
85
86impl std::ops::Index<usize> for Bundle {
87 type Output = Transaction;
88
89 fn index(&self, index: usize) -> &Self::Output {
91 self.get(index).unwrap()
93 }
94}
95
96#[cfg(test)]
97mod tests {}