mod serde_dif;
use crate::{
prelude::*,
shared_values::base_shared_value_container::observers::TransceiverId,
values::value_container::ValueContainer,
};
use strum::{AsRefStr, EnumString};
mod append_entry;
mod decrement;
mod delete_entry;
mod increment;
mod list_splice;
mod replace;
mod set_entry;
pub use append_entry::*;
pub use decrement::*;
pub use delete_entry::*;
pub use increment::*;
pub use list_splice::*;
pub use replace::*;
pub use set_entry::*;
mod to_instructions;
use strum::VariantNames;
#[derive(Clone, Debug, PartialEq, AsRefStr, EnumString, VariantNames)]
#[strum(serialize_all = "snake_case")]
pub enum UpdateOperationKind {
Replace,
SetEntry,
DeleteEntry,
Clear,
AppendEntry,
ListSplice,
Increment,
Decrement,
}
#[derive(Clone, Debug, PartialEq, AsRefStr, Hash)]
#[strum(serialize_all = "snake_case")]
pub enum UpdateOperation {
Replace(Box<ReplaceUpdateData>),
SetEntry(Box<SetEntryUpdateData>),
DeleteEntry(Box<DeleteEntryUpdateData>),
Clear,
AppendEntry(Box<AppendEntryUpdateData>),
ListSplice(Box<ListSpliceUpdateData>),
Increment(Box<IncrementUpdateData>),
Decrement(Box<DecrementUpdateData>),
}
impl UpdateOperation {
pub fn replace(value: ValueContainer) -> Self {
UpdateOperation::Replace(Box::new(ReplaceUpdateData::new(value)))
}
pub fn set_entry(key: ValueContainer, value: ValueContainer) -> Self {
UpdateOperation::SetEntry(Box::new(SetEntryUpdateData::new(key, value)))
}
pub fn delete_entry(key: ValueContainer) -> Self {
UpdateOperation::DeleteEntry(Box::new(DeleteEntryUpdateData::new(key)))
}
pub fn clear() -> Self {
UpdateOperation::Clear
}
pub fn append_entry(value: ValueContainer) -> Self {
UpdateOperation::AppendEntry(Box::new(AppendEntryUpdateData::new(
value,
)))
}
pub fn list_splice(
start_index: u32,
delete_count: u32,
items_to_insert: Vec<ValueContainer>,
) -> Self {
UpdateOperation::ListSplice(Box::new(ListSpliceUpdateData::new(
start_index,
delete_count,
items_to_insert,
)))
}
pub fn increment(amount: ValueContainer) -> Self {
UpdateOperation::Increment(Box::new(IncrementUpdateData::new(amount)))
}
pub fn decrement(amount: ValueContainer) -> Self {
UpdateOperation::Decrement(Box::new(DecrementUpdateData::new(amount)))
}
}
#[derive(Clone, Debug, PartialEq, AsRefStr, Hash)]
pub enum UpdateOperator {
AppendEntry, DeleteEntry, Increment, Decrement, }
impl UpdateOperation {
pub fn with_source(
self,
source_id: TransceiverId,
path: Vec<ValueContainer>,
) -> Update {
Update {
source_id,
data: UpdateData::new_with_path(self, path),
}
}
pub fn with_source_root(self, source_id: TransceiverId) -> Update {
Self::with_source(self, source_id, vec![])
}
}
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct Update {
source_id: TransceiverId,
data: UpdateData,
}
impl Update {
pub fn new(source_id: TransceiverId, data: UpdateData) -> Self {
Update { source_id, data }
}
pub fn source_id(&self) -> &TransceiverId {
&self.source_id
}
pub fn data(&self) -> &UpdateData {
&self.data
}
pub fn path(&self) -> &[ValueContainer] {
self.data.path()
}
pub fn operation(&self) -> &UpdateOperation {
self.data.operation()
}
pub fn into_operation(self) -> UpdateOperation {
self.data.into_operation()
}
pub fn into_parts(
self,
) -> (TransceiverId, UpdateOperation, Vec<ValueContainer>) {
let (operation, path) = self.data.into_parts();
(self.source_id, operation, path)
}
}
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct UpdateData {
operation: UpdateOperation,
path: Vec<ValueContainer>,
}
impl UpdateData {
pub fn new(operation: UpdateOperation) -> Self {
UpdateData {
operation,
path: vec![],
}
}
pub fn new_with_path(
operation: UpdateOperation,
path: Vec<ValueContainer>,
) -> Self {
UpdateData { operation, path }
}
pub fn path(&self) -> &[ValueContainer] {
&self.path
}
pub fn into_parts(self) -> (UpdateOperation, Vec<ValueContainer>) {
(self.operation, self.path)
}
pub fn operation(&self) -> &UpdateOperation {
&self.operation
}
pub fn into_operation(self) -> UpdateOperation {
self.operation
}
}