use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(
feature = "db-sqlx",
derive(sqlx::Type, sqlx::FromRow),
sqlx(transparent)
)]
pub struct LogId(i64);
impl LogId {
pub fn new(value: i64) -> Self {
Self(value)
}
pub fn is_user_log(&self) -> bool {
self.0 % 2 == 1
}
pub fn is_system_log(&self) -> bool {
self.0 % 2 == 0
}
pub fn as_i64(&self) -> i64 {
self.0
}
}
impl Default for LogId {
fn default() -> Self {
Self::new(1)
}
}
impl Copy for LogId {}
impl Iterator for LogId {
type Item = LogId;
fn next(&mut self) -> Option<Self::Item> {
if self.is_user_log() {
Some(Self(self.0 + 2))
} else {
None
}
}
}
impl PartialEq for LogId {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
#[cfg(test)]
mod tests {
use super::LogId;
#[test]
fn user_log_ids() {
let mut log_id = LogId::default();
assert_eq!(log_id.is_user_log(), true);
assert_eq!(log_id.is_system_log(), false);
let mut next_log_id = log_id.next().unwrap();
assert_eq!(next_log_id, LogId::new(3));
let next_log_id = next_log_id.next().unwrap();
assert_eq!(next_log_id, LogId::new(5));
}
#[test]
fn system_log_ids() {
let mut log_id = LogId::new(0);
assert_eq!(log_id.is_user_log(), false);
assert_eq!(log_id.is_system_log(), true);
assert!(log_id.next().is_none());
}
}