Skip to content

Commit

Permalink
wip - from functions
Browse files Browse the repository at this point in the history
  • Loading branch information
heckj committed Jun 28, 2024
1 parent 3a405db commit acc1388
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
4 changes: 2 additions & 2 deletions rust/src/automerge.udl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ dictionary MarkSet {
interface AMValue {
Map ( record<string,AMValue> value );
Scalar ( ScalarValue value );
List ( sequence<HydratedList> value );
List ( sequence<HydratedListItem> value );
Text ( HydratedText value );
};

Expand All @@ -121,7 +121,7 @@ dictionary HydratedText {
record<string,ScalarValue> marks;
};

dictionary HydratedList {
dictionary HydratedListItem {
AMValue value;
record<string,ScalarValue> marks;
boolean conflict;
Expand Down
10 changes: 10 additions & 0 deletions rust/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use automerge::{transaction::Transactable, ReadDoc};
use crate::actor_id::ActorId;
use crate::cursor::Position;
use crate::mark::{ExpandMark, KeyValue, Mark};
use crate::span::{Span};
use crate::patches::Patch;
use crate::{
Change, ChangeHash, Cursor, ObjId, ObjType, PathElement, ScalarValue, SyncState, Value,
Expand Down Expand Up @@ -514,6 +515,15 @@ impl Doc {
Ok(())
}

pub fn spans(&self, obj: ObjId) -> Result<Vec<Span>, DocError> {
let mut doc = self.0.write().unwrap();
let obj = am::ObjId::from(obj);
let x = doc.spans(obj).unwrap();
let y = x
.into_iter()
.map(am::iter::spans::from);
}

pub fn merge(&self, other: Arc<Self>) -> Result<(), DocError> {
let mut doc = self.0.write().unwrap();
let mut other = other.0.write().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ use sync_state::{DecodeSyncStateError, SyncState};
mod value;
use value::Value;
mod span;
use span::{AMValue, HydratedList, HydratedText, MarkSet, Span};
use span::{AMValue, HydratedListItem, HydratedText, MarkSet, Span};
41 changes: 38 additions & 3 deletions rust/src/span.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ScalarValue;
use std::collections::HashMap;
// use automerge as am;
use automerge as am;

// maps to am::iter::Span
// need to create From<> in to convert over
Expand All @@ -14,22 +14,57 @@ pub enum Span {
Block { value: HashMap<String, AMValue> },
}

// impl From<Span> for am::iter::Spans<'_> {
// fn from(value: Span) -> Self {
// let inner: [u8; 32] = value.0.try_into().unwrap();
// am::ChangeHash(inner)
// }
// }

impl<'a> From<&'a am::iter::Span> for Span {
fn from(value: &'a am::iter::Span) -> Self {
match value {
am::iter::Span::Text( t, m) => Self::Text { text: t.to_string(), marks: Option<Arc<am::marks::MarkSet>>::from() },
am::iter::Span::Block( value ) => Self::Block { value: HashMap<String, AMValue>::from(value) }
}
}
}

impl<'a> From<&'a am::hydrate::Map> for HashMap<String, AMValue> {
fn from(value: &'a am::hydrate::Map) -> Self {
let mut new_hash_map:HashMap<String, AMValue> = HashMap::new();
// fill in the middle bits...
return new_hash_map;
}
}

// loosely maps to am::marks:MarkSet
// need to create From<> in to convert over
pub struct MarkSet {
pub marks: HashMap<String, ScalarValue>,
}

impl<'a> From<&'a am::marks::MarkSet> for MarkSet {
fn from(value: &'a am::marks::MarkSet) -> Self {
let mut new_hash:HashMap<String, ScalarValue> = HashMap::new();
// iterate through MarkSet, building a hashmap for this MarkSet
for (k, v) in value.iter() {
new_hash.insert(k.to_string(), v.into());
}
Self { marks: new_hash }
}
}

pub enum AMValue {
Map { value: HashMap<String, AMValue> },
Scalar { value: ScalarValue },
List { value: Vec<HydratedList> },
List { value: Vec<HydratedListItem> },
Text { value: HydratedText },
}

// loosely maps to am::hydrate::ListValue
// need to create From<> in to convert over
pub struct HydratedList {
pub struct HydratedListItem {
pub value: AMValue,
pub marks: HashMap<String, ScalarValue>,
pub conflict: bool,
Expand Down

0 comments on commit acc1388

Please sign in to comment.