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
use core::fmt::{Display, Error as FmtError, Formatter};

use ibc_relayer_types::{core::ics02_client::events::NewBlock, Height};

use crate::event::source::EventBatch;

/// A command for a [`WorkerHandle`](crate::worker::WorkerHandle).
#[derive(Debug, Clone)]
pub enum WorkerCmd {
    /// A batch of packet events need to be relayed
    IbcEvents { batch: EventBatch },

    /// A new block has been committed
    NewBlock { height: Height, new_block: NewBlock },

    /// Trigger a pending packets clear
    ClearPendingPackets,
}

impl WorkerCmd {
    /// Returns `true` if the worker cmd is [`IbcEvents`].
    ///
    /// [`IbcEvents`]: WorkerCmd::IbcEvents
    #[must_use]
    pub fn is_ibc_events(&self) -> bool {
        matches!(self, Self::IbcEvents { .. })
    }

    /// Returns `true` if the worker cmd is [`NewBlock`].
    ///
    /// [`NewBlock`]: WorkerCmd::NewBlock
    #[must_use]
    pub fn is_new_block(&self) -> bool {
        matches!(self, Self::NewBlock { .. })
    }

    /// Returns `true` if the worker cmd is [`ClearPendingPackets`].
    ///
    /// [`ClearPendingPackets`]: WorkerCmd::ClearPendingPackets
    #[must_use]
    pub fn is_clear_pending_packets(&self) -> bool {
        matches!(self, Self::ClearPendingPackets)
    }
}

impl Display for WorkerCmd {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
        match self {
            WorkerCmd::IbcEvents { batch } => {
                write!(f, "IbcEvents batch from {}: ", batch.chain_id)?;
                for e in &batch.events {
                    write!(f, "{e}; ")?;
                }
                write!(f, "batch Height: {}", batch.height)
            }
            WorkerCmd::NewBlock { height, new_block } => {
                write!(f, "NewBlock({height}, {new_block})")
            }
            WorkerCmd::ClearPendingPackets => write!(f, "ClearPendingPackets"),
        }
    }
}