use async_trait::async_trait;
use super::SobreEvento;
pub const MOTIVO_PAYLOAD_INVALIDO: &str = "payload_invalido";
pub const MOTIVO_ENTREGAS_MAXIMAS_SUPERADAS: &str = "entregas_maximas_superadas";
pub const MOTIVO_ERROR_PERMANENTE: &str = "error_permanente";
#[async_trait]
pub trait ManejadorEvento: Send + Sync {
async fn manejar(&self, event: &SobreEvento) -> ResultadoManejo;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResultadoManejo {
Ack,
Reintentar,
Descartar {
motivo: &'static str,
detalle: String,
},
}
impl ResultadoManejo {
pub fn descartar(motivo: &'static str, detalle: impl Into<String>) -> Self {
Self::Descartar {
motivo,
detalle: detalle.into(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_keep_reason_codes_stable_when_stored_in_dead_letters() {
assert_eq!(MOTIVO_PAYLOAD_INVALIDO, "payload_invalido");
assert_eq!(
MOTIVO_ENTREGAS_MAXIMAS_SUPERADAS,
"entregas_maximas_superadas"
);
assert_eq!(MOTIVO_ERROR_PERMANENTE, "error_permanente");
}
#[test]
fn should_carry_reason_and_detail_when_dead_lettered() {
assert_eq!(
ResultadoManejo::descartar(MOTIVO_ERROR_PERMANENTE, "card declined"),
ResultadoManejo::Descartar {
motivo: "error_permanente",
detalle: "card declined".into()
}
);
}
}