use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq)]
pub enum AttributeValue {
String(String),
Int(i64),
Uint(u64),
Float(f64),
Bool(bool),
Timestamp(u64),
}
impl AttributeValue {
pub fn as_int(&self) -> i64 {
match self {
AttributeValue::Int(v) => *v,
AttributeValue::Uint(v) => *v as i64,
_ => 0,
}
}
pub fn as_uint(&self) -> u64 {
match self {
AttributeValue::Uint(v) => *v,
AttributeValue::Timestamp(v) => *v,
AttributeValue::Int(v) => *v as u64,
_ => 0,
}
}
pub fn as_float(&self) -> f64 {
match self {
AttributeValue::Float(v) => *v,
AttributeValue::Int(v) => *v as f64,
AttributeValue::Uint(v) => *v as f64,
_ => 0.0,
}
}
pub fn as_bool(&self) -> bool {
match self {
AttributeValue::Bool(v) => *v,
_ => false,
}
}
pub fn as_string(&self) -> String {
match self {
AttributeValue::String(v) => v.clone(),
_ => String::new(),
}
}
}
#[derive(Debug, Clone)]
pub enum ConflictResult<T> {
NoConflict,
Conflict(Vec<T>),
}
impl<T> ConflictResult<T> {
pub fn is_conflict(&self) -> bool {
matches!(self, ConflictResult::Conflict(_))
}
pub fn conflicting_items(self) -> Option<Vec<T>> {
match self {
ConflictResult::Conflict(items) => Some(items),
ConflictResult::NoConflict => None,
}
}
}
pub trait Conflictable: Clone + Send + Sync + 'static {
fn id(&self) -> String;
fn conflict_keys(&self) -> Vec<String>;
fn timestamp(&self) -> Option<u64>;
fn attributes(&self) -> HashMap<String, AttributeValue>;
}