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
#![allow(dead_code)]

use crate::data::SnapshotEntry;
use crate::VotingKey;
use cardano_serialization_lib::address::RewardAddress;
use cardano_serialization_lib::crypto::PublicKey;

/// Allows [`SnapshotEntry`] struct to be assertable
pub trait VerifiableSnapshotOutput {
    /// returns assertion struct
    fn assert(&self) -> SnapshotOutputAssert;
}

impl VerifiableSnapshotOutput for SnapshotEntry {
    fn assert(&self) -> SnapshotOutputAssert {
        SnapshotOutputAssert::new(self)
    }
}

/// Fluent api for [`SnapshotEntry`] assertions
pub struct SnapshotOutputAssert<'a> {
    output: &'a SnapshotEntry,
}

impl<'a> SnapshotOutputAssert<'a> {
    /// Creates new instance based on [`SnapshotEntry`] reference
    #[must_use]
    pub fn new(output: &'a SnapshotEntry) -> Self {
        Self { output }
    }

    /// Asserts delegations address field from [`SnapshotEntry`]
    /// # Panics
    ///
    /// Panics on assertion failed
    pub fn voting_key(&self, voting_key: &VotingKey) {
        assert_eq!(voting_key, &self.output.voting_key, "delegation target");
    }

    /// Asserts reward address field from [`SnapshotEntry`]
    /// # Panics
    ///
    /// Panics on assertion failed
    pub fn reward_address(&self, reward_address: &RewardAddress) {
        assert_eq!(
            reward_address.to_address().to_hex(),
            hex::encode(&self.output.rewards_address.0),
            "different rewards address"
        );
    }

    /// Asserts stake public key field from [`SnapshotEntry`]
    /// # Panics
    ///
    /// Panics on assertion failed
    pub fn stake_key(&self, public_key: &PublicKey) {
        assert_eq!(
            &public_key.to_hex(),
            &hex::encode(self.output.stake_key.clone()),
            "different stake public key"
        );
    }
}