use crate::correlation::CorrelatableFrame;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TerminatorCorrelation {
StampRequest,
PreserveFrame,
}
impl TerminatorCorrelation {
pub(crate) const ACTOR: Self = Self::StampRequest;
pub(crate) const PREPARED: Self = Self::PreserveFrame;
pub(crate) fn apply<F: CorrelatableFrame>(self, frame: &mut F, correlation: Option<u64>) {
if self == Self::PREPARED {
return;
}
if let Some(identifier) = correlation {
frame.set_correlation_id(Some(identifier));
}
}
pub(crate) fn apply_codec_frame<T>(self, _frame: &mut T) {
debug_assert_eq!(
self,
Self::PREPARED,
"a codec-frame terminator cannot be restamped"
);
}
}
#[cfg(test)]
mod tests {
use super::TerminatorCorrelation;
use crate::app::Envelope;
#[test]
fn stamp_request_applies_the_response_correlation() {
let mut terminator = Envelope::new(9, None, Vec::new());
TerminatorCorrelation::ACTOR.apply(&mut terminator, Some(7));
assert_eq!(terminator.correlation_id, Some(7));
}
#[test]
fn stamp_request_keeps_the_frame_correlation_when_the_request_has_none() {
let mut terminator = Envelope::new(9, Some(3), Vec::new());
TerminatorCorrelation::ACTOR.apply(&mut terminator, None);
assert_eq!(terminator.correlation_id, Some(3));
}
#[test]
fn preserve_frame_leaves_an_envelope_terminator_unchanged() {
let mut terminator = Envelope::new(9, None, Vec::new());
TerminatorCorrelation::PREPARED.apply(&mut terminator, Some(7));
assert_eq!(terminator.correlation_id, None);
}
#[test]
fn preserve_frame_accepts_a_codec_frame() {
let mut frame = vec![1_u8, 2, 3];
TerminatorCorrelation::PREPARED.apply_codec_frame(&mut frame);
assert_eq!(frame, vec![1, 2, 3]);
}
}