use mongodb::change_stream::event::OperationType;
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
pub enum Event {
Insert,
Update,
Delete,
}
impl Event {
pub fn event_type_str(&self) -> &'static str {
match self {
Self::Insert => "insert",
Self::Update => "update",
Self::Delete => "delete",
}
}
}
impl From<OperationType> for Event {
fn from(op_type: OperationType) -> Self {
match op_type {
OperationType::Insert => Event::Insert,
OperationType::Update => Event::Update,
OperationType::Delete => Event::Delete,
_ => Event::Insert,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use mongodb::change_stream::event::OperationType;
#[test]
fn test_event_type_str() {
assert_eq!(Event::Insert.event_type_str(), "insert");
assert_eq!(Event::Update.event_type_str(), "update");
assert_eq!(Event::Delete.event_type_str(), "delete");
}
#[test]
fn test_from_operation_type() {
assert_eq!(Event::from(OperationType::Insert), Event::Insert);
assert_eq!(Event::from(OperationType::Update), Event::Update);
assert_eq!(Event::from(OperationType::Delete), Event::Delete);
}
#[test]
fn test_unsupported_operation_type() {
assert_eq!(Event::from(OperationType::Replace), Event::Insert);
assert_eq!(Event::from(OperationType::Invalidate), Event::Insert);
}
}