diff --git a/rust/src/automerge.udl b/rust/src/automerge.udl index 7d9703aa..6797afbd 100644 --- a/rust/src/automerge.udl +++ b/rust/src/automerge.udl @@ -1,5 +1,8 @@ namespace automerge { ObjId root(); + + boolean valid_change(sequence bytes); + }; [Custom] diff --git a/rust/src/change.rs b/rust/src/change.rs index 982b87ca..db337ea9 100644 --- a/rust/src/change.rs +++ b/rust/src/change.rs @@ -41,3 +41,8 @@ pub fn decode_change(bytes: Vec) -> Result { .map(Change::from) .map_err(DecodeChangeError::from) } + +pub fn valid_change(bytes: Vec) -> bool { + let x = decode_change(bytes); + return x.is_ok(); +} \ No newline at end of file diff --git a/rust/src/changeset.rs b/rust/src/changeset.rs new file mode 100644 index 00000000..5ad9105c --- /dev/null +++ b/rust/src/changeset.rs @@ -0,0 +1,76 @@ +//use super::UniffiCustomTypeConverter; +use automerge as am; +use am::Change; + +pub struct ChangeSet(Vec); + + +#[derive(Debug, thiserror::Error)] +pub enum DecodeChangeSetError { + #[error(transparent)] + Internal(#[from] am::LoadChangeError), +} + +impl ChangeSet { + pub fn new() -> Self { + Self(Vec::new()) + } + + pub fn decode(bytes: Vec) -> Result { + // let ac = automerge::AutoCommit::load(bytes.as_slice())?; + // Ok(Doc(RwLock::new(ac))) + let result_vector: Vec = Vec::new(); + + // example of Parsing a change from bytes: + let _change = am::Change::try_from(bytes.as_slice()) + .map(Change::from) + .map_err(DecodeChangeSetError::from); + + let x = ChangeSet(result_vector); + return Ok(x); + + // storage is 'private' - code replicated from load_incremental_log_patches in Autocommit.rs + // let changes = match am::storage::load::load_changes(storage::parse::Input::new(data)) { + // load::LoadedChanges::Complete(c) => c, + // load::LoadedChanges::Partial { error, loaded, .. } => { + // tracing::warn!(successful_chunks=loaded.len(), err=?error, "partial load"); + // loaded + // } + // }; + + } +} + +impl Default for ChangeSet { + fn default() -> Self { + Self::new() + } +} + +// The following methods are for converting types from the Automerge module +// impl From for am::Cursor { +// fn from(value: Cursor) -> Self { +// am::Cursor::try_from(value.0).unwrap() +// } +// } + +// impl From for Cursor { +// fn from(value: am::Cursor) -> Self { +// Cursor(value.to_bytes()) +// } +// } + +// impl UniffiCustomTypeConverter for Cursor { +// type Builtin = Vec; + +// fn into_custom(val: Self::Builtin) -> uniffi::Result +// where +// Self: Sized, +// { +// Ok(Self(val)) +// } + +// fn from_custom(obj: Self) -> Self::Builtin { +// obj.0 +// } +// } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 6733f0e9..98ea3d05 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -24,3 +24,5 @@ mod sync_state; use sync_state::{DecodeSyncStateError, SyncState}; mod value; use value::Value; +mod change; +use change::valid_change;