use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum TxLogMode {
Off,
#[default]
Memory,
OnMutation,
OnCommit,
}
impl TxLogMode {
pub fn should_persist(&self) -> bool {
matches!(self, TxLogMode::OnMutation | TxLogMode::OnCommit)
}
pub fn persist_on_mutation(&self) -> bool {
matches!(self, TxLogMode::OnMutation)
}
pub fn persist_on_commit(&self) -> bool {
matches!(self, TxLogMode::OnCommit)
}
pub fn description(&self) -> &'static str {
match self {
TxLogMode::Off => "No logging",
TxLogMode::Memory => "In-memory only",
TxLogMode::OnMutation => "Save on each mutation",
TxLogMode::OnCommit => "Save on commit",
}
}
}
impl std::fmt::Display for TxLogMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TxLogMode::Off => write!(f, "off"),
TxLogMode::Memory => write!(f, "memory"),
TxLogMode::OnMutation => write!(f, "mutation"),
TxLogMode::OnCommit => write!(f, "commit"),
}
}
}
impl std::str::FromStr for TxLogMode {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"off" | "none" | "no" => Ok(TxLogMode::Off),
"memory" | "mem" => Ok(TxLogMode::Memory),
"mutation" | "onmutation" | "on-mutation" => Ok(TxLogMode::OnMutation),
"commit" | "oncommit" | "on-commit" => Ok(TxLogMode::OnCommit),
_ => Err(format!(
"Unknown TxLogMode: '{}'. Valid options: off, memory, mutation, commit",
s
)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mode_properties() {
assert!(!TxLogMode::Off.should_persist());
assert!(!TxLogMode::Memory.should_persist());
assert!(TxLogMode::OnMutation.should_persist());
assert!(TxLogMode::OnCommit.should_persist());
assert!(TxLogMode::OnMutation.persist_on_mutation());
assert!(!TxLogMode::OnCommit.persist_on_mutation());
assert!(!TxLogMode::OnMutation.persist_on_commit());
assert!(TxLogMode::OnCommit.persist_on_commit());
}
#[test]
fn test_mode_parsing() {
assert_eq!("off".parse::<TxLogMode>().unwrap(), TxLogMode::Off);
assert_eq!("memory".parse::<TxLogMode>().unwrap(), TxLogMode::Memory);
assert_eq!(
"mutation".parse::<TxLogMode>().unwrap(),
TxLogMode::OnMutation
);
assert_eq!("commit".parse::<TxLogMode>().unwrap(), TxLogMode::OnCommit);
assert_eq!("none".parse::<TxLogMode>().unwrap(), TxLogMode::Off);
assert_eq!(
"on-commit".parse::<TxLogMode>().unwrap(),
TxLogMode::OnCommit
);
}
#[test]
fn test_mode_display() {
assert_eq!(TxLogMode::Off.to_string(), "off");
assert_eq!(TxLogMode::Memory.to_string(), "memory");
assert_eq!(TxLogMode::OnMutation.to_string(), "mutation");
assert_eq!(TxLogMode::OnCommit.to_string(), "commit");
}
}