diff --git a/automerge/src/autocommit.rs b/automerge/src/autocommit.rs index 3d833266..e84185e5 100644 --- a/automerge/src/autocommit.rs +++ b/automerge/src/autocommit.rs @@ -5,7 +5,7 @@ use crate::{ change::export_change, transaction::TransactionInner, ActorId, Automerge, AutomergeError, Change, ChangeHash, Prop, Value, }; -use crate::{Keys, KeysAt, SyncMessage, SyncState}; +use crate::{Keys, KeysAt, ObjType, ScalarValue, SyncMessage, SyncState}; /// An automerge document that automatically manages transactions. #[derive(Debug, Clone)] @@ -325,28 +325,50 @@ impl Transactable for AutoCommit { /// - The object does not exist /// - The key is the wrong type for the object /// - The key does not exist in the object - fn set, V: Into>( + fn set, V: Into>( &mut self, obj: &ExId, prop: P, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { self.ensure_transaction_open(); let tx = self.transaction.as_mut().unwrap(); tx.set(&mut self.doc, obj, prop, value) } - fn insert>( + fn make, V: Into>( + &mut self, + obj: &ExId, + prop: P, + value: V, + ) -> Result { + self.ensure_transaction_open(); + let tx = self.transaction.as_mut().unwrap(); + tx.make(&mut self.doc, obj, prop, value) + } + + fn insert>( &mut self, obj: &ExId, index: usize, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { self.ensure_transaction_open(); let tx = self.transaction.as_mut().unwrap(); tx.insert(&mut self.doc, obj, index, value) } + fn make_insert>( + &mut self, + obj: &ExId, + index: usize, + value: V, + ) -> Result { + self.ensure_transaction_open(); + let tx = self.transaction.as_mut().unwrap(); + tx.make_insert(&mut self.doc, obj, index, value) + } + fn inc>( &mut self, obj: &ExId, diff --git a/automerge/src/transaction/inner.rs b/automerge/src/transaction/inner.rs index 4aec42de..453fc362 100644 --- a/automerge/src/transaction/inner.rs +++ b/automerge/src/transaction/inner.rs @@ -2,7 +2,7 @@ use crate::exid::ExId; use crate::query::{self, OpIdSearch}; use crate::types::{Key, ObjId, OpId}; use crate::{change::export_change, types::Op, Automerge, ChangeHash, Prop, Value}; -use crate::{AutomergeError, OpType}; +use crate::{AutomergeError, ObjType, OpType, ScalarValue}; #[derive(Debug, Clone)] pub struct TransactionInner { @@ -74,20 +74,43 @@ impl TransactionInner { /// - The object does not exist /// - The key is the wrong type for the object /// - The key does not exist in the object - pub fn set, V: Into>( + pub fn set, V: Into>( &mut self, doc: &mut Automerge, obj: &ExId, prop: P, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { let obj = doc.exid_to_obj(obj)?; - let value = value.into(); - if let Some(id) = self.local_op(doc, obj, prop.into(), value.into())? { - Ok(Some(doc.id_to_exid(id))) - } else { - Ok(None) - } + let value = Value::Scalar(value.into()); + self.local_op(doc, obj, prop.into(), value.into())?; + Ok(()) + } + + /// Set the value of property `P` to value `V` in object `obj`. + /// + /// # Returns + /// + /// The opid of the operation which was created, or None if this operation doesn't change the + /// document + /// + /// # Errors + /// + /// This will return an error if + /// - The object does not exist + /// - The key is the wrong type for the object + /// - The key does not exist in the object + pub fn make, V: Into>( + &mut self, + doc: &mut Automerge, + obj: &ExId, + prop: P, + value: V, + ) -> Result { + let obj = doc.exid_to_obj(obj)?; + let value = Value::Object(value.into()); + let id = self.local_op(doc, obj, prop.into(), value.into())?.unwrap(); + Ok(doc.id_to_exid(id)) } fn next_id(&mut self) -> OpId { @@ -108,19 +131,30 @@ impl TransactionInner { self.operations.push(op); } - pub fn insert>( + pub fn insert>( &mut self, doc: &mut Automerge, obj: &ExId, index: usize, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { let obj = doc.exid_to_obj(obj)?; - if let Some(id) = self.do_insert(doc, obj, index, value)? { - Ok(Some(doc.id_to_exid(id))) - } else { - Ok(None) - } + self.do_insert(doc, obj, index, Value::Scalar(value.into()))?; + Ok(()) + } + + pub fn make_insert>( + &mut self, + doc: &mut Automerge, + obj: &ExId, + index: usize, + value: V, + ) -> Result { + let obj = doc.exid_to_obj(obj)?; + let id = self + .do_insert(doc, obj, index, Value::Object(value.into()))? + .unwrap(); + Ok(doc.id_to_exid(id)) } fn do_insert>( diff --git a/automerge/src/transaction/manual_transaction.rs b/automerge/src/transaction/manual_transaction.rs index 93dcf615..e06e1552 100644 --- a/automerge/src/transaction/manual_transaction.rs +++ b/automerge/src/transaction/manual_transaction.rs @@ -1,5 +1,5 @@ use crate::exid::ExId; -use crate::{Automerge, ChangeHash, KeysAt, Prop, Value}; +use crate::{Automerge, ChangeHash, KeysAt, ObjType, Prop, ScalarValue, Value}; use crate::{AutomergeError, Keys}; use super::{CommitOptions, Transactable, TransactionInner}; @@ -85,27 +85,51 @@ impl<'a> Transactable for Transaction<'a> { /// - The object does not exist /// - The key is the wrong type for the object /// - The key does not exist in the object - fn set, V: Into>( + fn set, V: Into>( &mut self, obj: &ExId, prop: P, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { self.inner.as_mut().unwrap().set(self.doc, obj, prop, value) } - fn insert>( + fn make, V: Into>( + &mut self, + obj: &ExId, + prop: P, + value: V, + ) -> Result { + self.inner + .as_mut() + .unwrap() + .make(self.doc, obj, prop, value) + } + + fn insert>( &mut self, obj: &ExId, index: usize, value: V, - ) -> Result, AutomergeError> { + ) -> Result<(), AutomergeError> { self.inner .as_mut() .unwrap() .insert(self.doc, obj, index, value) } + fn make_insert>( + &mut self, + obj: &ExId, + index: usize, + value: V, + ) -> Result { + self.inner + .as_mut() + .unwrap() + .make_insert(self.doc, obj, index, value) + } + fn inc>( &mut self, obj: &ExId, diff --git a/automerge/src/transaction/transactable.rs b/automerge/src/transaction/transactable.rs index 93d65cd1..ea863af0 100644 --- a/automerge/src/transaction/transactable.rs +++ b/automerge/src/transaction/transactable.rs @@ -1,5 +1,5 @@ use crate::exid::ExId; -use crate::{AutomergeError, ChangeHash, Keys, KeysAt, Prop, Value}; +use crate::{AutomergeError, ChangeHash, Keys, KeysAt, ObjType, Prop, ScalarValue, Value}; use unicode_segmentation::UnicodeSegmentation; /// A way of mutating a document within a single change. @@ -20,20 +20,48 @@ pub trait Transactable { /// - The object does not exist /// - The key is the wrong type for the object /// - The key does not exist in the object - fn set, V: Into>( + fn set, V: Into>( &mut self, obj: &ExId, prop: P, value: V, - ) -> Result, AutomergeError>; + ) -> Result<(), AutomergeError>; + + /// Set the value of property `P` to value `V` in object `obj`. + /// + /// # Returns + /// + /// The opid of the operation which was created, or None if this operation doesn't change the + /// document + /// + /// # Errors + /// + /// This will return an error if + /// - The object does not exist + /// - The key is the wrong type for the object + /// - The key does not exist in the object + fn make, V: Into>( + &mut self, + obj: &ExId, + prop: P, + value: V, + ) -> Result; /// Insert a value into a list at the given index. - fn insert>( + fn insert>( &mut self, obj: &ExId, index: usize, value: V, - ) -> Result, AutomergeError>; + ) -> Result<(), AutomergeError>; + + /// Insert a value into a list at the given index. + fn make_insert>( + &mut self, + obj: &ExId, + index: usize, + value: V, + ) -> Result; /// Increment the counter at the prop in the object by `value`. fn inc>(&mut self, obj: &ExId, prop: P, value: i64)