use std::fmt;
use crate::types::MsgType;
use stratum_apps::stratum_core::parsers_sv2::AnyMessage;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageDirection {
ToDownstream,
ToUpstream,
}
impl fmt::Display for MessageDirection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MessageDirection::ToDownstream => write!(f, "downstream"),
MessageDirection::ToUpstream => write!(f, "upstream"),
}
}
}
#[derive(Debug, Clone)]
pub enum InterceptAction {
IgnoreMessage(IgnoreMessage),
ReplaceMessage(Box<ReplaceMessage>),
}
impl InterceptAction {
pub fn find_matching_action(
&self,
msg_type: MsgType,
direction: MessageDirection,
) -> Option<&Self> {
match self {
InterceptAction::IgnoreMessage(bm)
if bm.direction == direction && bm.expected_message_type == msg_type =>
{
Some(self)
}
InterceptAction::ReplaceMessage(im)
if im.direction == direction && im.expected_message_type == msg_type =>
{
Some(self)
}
_ => None,
}
}
}
#[derive(Debug, Clone)]
pub struct IgnoreMessage {
direction: MessageDirection,
expected_message_type: MsgType,
}
impl IgnoreMessage {
pub fn new(direction: MessageDirection, expected_message_type: MsgType) -> Self {
IgnoreMessage {
direction,
expected_message_type,
}
}
}
impl From<IgnoreMessage> for InterceptAction {
fn from(value: IgnoreMessage) -> Self {
InterceptAction::IgnoreMessage(value)
}
}
#[derive(Debug, Clone)]
pub struct ReplaceMessage {
direction: MessageDirection,
expected_message_type: MsgType,
pub(crate) replacement_message: AnyMessage<'static>,
}
impl ReplaceMessage {
pub fn new(
direction: MessageDirection,
expected_message_type: MsgType,
replacement_message: AnyMessage<'static>,
) -> Self {
Self {
direction,
expected_message_type,
replacement_message,
}
}
}
impl From<ReplaceMessage> for InterceptAction {
fn from(value: ReplaceMessage) -> Self {
InterceptAction::ReplaceMessage(Box::new(value))
}
}