use core::fmt::{Display, Formatter};
use std::fmt;
use crate::pg_sys;
use crate::trigger_support::{PgTriggerError, TriggerEvent};
pub enum PgTriggerOperation {
Insert,
Update,
Delete,
Truncate,
}
impl TryFrom<TriggerEvent> for PgTriggerOperation {
type Error = PgTriggerError;
fn try_from(event: TriggerEvent) -> Result<Self, Self::Error> {
match event.0 & pg_sys::TRIGGER_EVENT_OPMASK {
pg_sys::TRIGGER_EVENT_INSERT => Ok(Self::Insert),
pg_sys::TRIGGER_EVENT_DELETE => Ok(Self::Delete),
pg_sys::TRIGGER_EVENT_UPDATE => Ok(Self::Update),
pg_sys::TRIGGER_EVENT_TRUNCATE => Ok(Self::Truncate),
v => Err(PgTriggerError::InvalidPgTriggerOperation(v)),
}
}
}
impl Display for PgTriggerOperation {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self {
PgTriggerOperation::Insert => "INSERT",
PgTriggerOperation::Update => "UPDATE",
PgTriggerOperation::Delete => "DELETE",
PgTriggerOperation::Truncate => "TRUNCATE",
})
}
}