Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChangeSet - validation of a array of bytes as a valid list of changes #126

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rust/src/automerge.udl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace automerge {
ObjId root();

boolean valid_change(sequence<u8> bytes);

};

[Custom]
Expand Down
5 changes: 5 additions & 0 deletions rust/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ pub fn decode_change(bytes: Vec<u8>) -> Result<Change, DecodeChangeError> {
.map(Change::from)
.map_err(DecodeChangeError::from)
}

pub fn valid_change(bytes: Vec<u8>) -> bool {
let x = decode_change(bytes);
return x.is_ok();
}
76 changes: 76 additions & 0 deletions rust/src/changeset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//use super::UniffiCustomTypeConverter;
use automerge as am;
use am::Change;

pub struct ChangeSet(Vec<Change>);


#[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<u8>) -> Result<Self, DecodeChangeSetError> {
// let ac = automerge::AutoCommit::load(bytes.as_slice())?;
// Ok(Doc(RwLock::new(ac)))
let result_vector: Vec<Change> = 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<Cursor> for am::Cursor {
// fn from(value: Cursor) -> Self {
// am::Cursor::try_from(value.0).unwrap()
// }
// }

// impl From<am::Cursor> for Cursor {
// fn from(value: am::Cursor) -> Self {
// Cursor(value.to_bytes())
// }
// }

// impl UniffiCustomTypeConverter for Cursor {
// type Builtin = Vec<u8>;

// fn into_custom(val: Self::Builtin) -> uniffi::Result<Self>
// where
// Self: Sized,
// {
// Ok(Self(val))
// }

// fn from_custom(obj: Self) -> Self::Builtin {
// obj.0
// }
// }
2 changes: 2 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ mod sync_state;
use sync_state::{DecodeSyncStateError, SyncState};
mod value;
use value::Value;
mod change;
use change::valid_change;
Loading