Skip to main content

DocumentWriter

Struct DocumentWriter 

Source
pub struct DocumentWriter<'a> { /* private fields */ }
Expand description

Closure-free mutation handle for FFI/WASM consumers.

Binds an agent to a document so mutations don’t need closures. Each method executes a single operation via transact() internally. For batching multiple operations in Rust, prefer Document::transact().

Path-based methods return bool (true = success) or Option<CrdtId> so FFI consumers can detect invalid paths without panicking.

§Example

use diamond_types_extended::{Document, Uuid};

let mut doc = Document::new();
let alice = doc.create_agent(Uuid::from_u128(0xA11CE));

{
    let mut w = doc.writer(alice);
    w.root_set("title", "My Document");
    w.root_set("count", 42);
    let text_id = w.root_create_text("body");
    assert!(w.text_push(&["body"], "Hello, world!"));
    assert!(!w.text_push(&["nonexistent"], "oops"));
}

assert_eq!(doc.root().get("title").unwrap().as_str(), Some("My Document"));
assert_eq!(doc.root().get_text("body").unwrap().content(), "Hello, world!");

Implementations§

Source§

impl<'a> DocumentWriter<'a>

Source

pub fn agent(&self) -> AgentId

Get the agent ID bound to this writer.

Source

pub fn set( &mut self, path: &[&str], key: &str, value: impl Into<PrimitiveValue>, ) -> bool

Set a key to a primitive value in a map at path.

Path points to the containing map. Empty path = root (always succeeds). Returns false if the path doesn’t resolve to a map.

Source

pub fn set_nil(&mut self, path: &[&str], key: &str) -> bool

Set a key to nil in a map at path.

Returns false if the path doesn’t resolve to a map.

Source

pub fn create_map(&mut self, path: &[&str], key: &str) -> Option<CrdtId>

Create a nested Map at key in a map at path.

Returns None if the path doesn’t resolve to a map.

Source

pub fn create_text(&mut self, path: &[&str], key: &str) -> Option<CrdtId>

Create a nested Text CRDT at key in a map at path.

Returns None if the path doesn’t resolve to a map.

Source

pub fn create_set(&mut self, path: &[&str], key: &str) -> Option<CrdtId>

Create a nested Set CRDT at key in a map at path.

Returns None if the path doesn’t resolve to a map.

Source

pub fn create_register(&mut self, path: &[&str], key: &str) -> Option<CrdtId>

Create a nested Register CRDT at key in a map at path.

Returns None if the path doesn’t resolve to a map.

Source

pub fn root_set(&mut self, key: &str, value: impl Into<PrimitiveValue>)

Set a key to a primitive value in the root map.

Source

pub fn root_create_map(&mut self, key: &str) -> CrdtId

Create a nested Map in the root map.

Source

pub fn root_create_text(&mut self, key: &str) -> CrdtId

Create a nested Text CRDT in the root map.

Source

pub fn root_create_set(&mut self, key: &str) -> CrdtId

Create a nested Set CRDT in the root map.

Source

pub fn text_insert(&mut self, path: &[&str], pos: usize, content: &str) -> bool

Insert text at a position in a Text CRDT at path.

Returns false if the path doesn’t resolve to a Text CRDT.

Source

pub fn text_delete(&mut self, path: &[&str], range: Range<usize>) -> bool

Delete a range of text in a Text CRDT at path.

Returns false if the path doesn’t resolve to a Text CRDT.

Source

pub fn text_push(&mut self, path: &[&str], content: &str) -> bool

Append text to the end of a Text CRDT at path.

Returns false if the path doesn’t resolve to a Text CRDT.

Source

pub fn set_add( &mut self, path: &[&str], value: impl Into<PrimitiveValue>, ) -> bool

Add a value to a Set CRDT at path.

Returns false if the path doesn’t resolve to a Set CRDT.

Source

pub fn set_remove( &mut self, path: &[&str], value: impl Into<PrimitiveValue>, ) -> bool

Remove a value from a Set CRDT at path.

Returns false if the path doesn’t resolve to a Set CRDT.

Auto Trait Implementations§

§

impl<'a> Freeze for DocumentWriter<'a>

§

impl<'a> RefUnwindSafe for DocumentWriter<'a>

§

impl<'a> Send for DocumentWriter<'a>

§

impl<'a> Sync for DocumentWriter<'a>

§

impl<'a> Unpin for DocumentWriter<'a>

§

impl<'a> UnsafeUnpin for DocumentWriter<'a>

§

impl<'a> !UnwindSafe for DocumentWriter<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V