Skip to content

Commit

Permalink
Concrete error types
Browse files Browse the repository at this point in the history
  • Loading branch information
adzialocha committed Jan 29, 2025
1 parent 7167b84 commit 080da26
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion aardvark-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.95"
async-channel = "2.3.1"
loro = "1.3.1"
thiserror = "2.0.11"

[dev-dependencies]
tokio = { version = "1.43.0", features = ["macros", "test-util"] }
35 changes: 24 additions & 11 deletions aardvark-doc/src/crdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::cell::RefCell;
use std::fmt;
use std::sync::Arc;

use anyhow::Result;
use loro::event::{Diff, DiffEvent};
use loro::{EventTriggerKind, ExportMode, LoroDoc, Subscription};
use thiserror::Error;

/// Identifier of container where we handle the text CRDT in a Loro document.
///
Expand Down Expand Up @@ -85,11 +85,13 @@ impl TextCrdt {
/// Use this when restoring an existing, local document (for example when it was stored on your
/// file system) or when receiving a full snapshot from another peer after joining an existing
/// document.
pub fn from_bytes(peer_id: u64, bytes: &[u8]) -> Result<Self> {
pub fn from_bytes(peer_id: u64, bytes: &[u8]) -> Result<Self, TextCrdtError> {
let crdt = Self::new(peer_id);
{
let inner = crdt.doc.borrow_mut();
inner.import_with(bytes, "snapshot")?;
inner
.import_with(bytes, "snapshot")
.map_err(|err| TextCrdtError::Imported(err))?;
}
Ok(crdt)
}
Expand Down Expand Up @@ -127,10 +129,11 @@ impl TextCrdt {
/// This text change gets directly committed, causing a local "delta event" which should be
/// used to update "higher layer" state, like the text buffer. Read [`subscribe`] for receiving
/// and handling these events.
pub fn insert(&mut self, index: usize, chunk: &str) -> Result<()> {
pub fn insert(&mut self, index: usize, chunk: &str) -> Result<(), TextCrdtError> {
let doc = self.doc.get_mut();
let text = doc.get_text(TEXT_CONTAINER_ID);
text.insert(index, chunk)?;
text.insert(index, chunk)
.map_err(|err| TextCrdtError::Local(err))?;
doc.commit();
Ok(())
}
Expand All @@ -140,20 +143,22 @@ impl TextCrdt {
/// This text change gets directly committed, causing a local "delta event" which should be
/// used to update "higher layer" state, like the text buffer. Read [`subscribe`] for receiving
/// and handling these events.
pub fn remove(&mut self, index: usize, len: usize) -> Result<()> {
pub fn remove(&mut self, index: usize, len: usize) -> Result<(), TextCrdtError> {
let doc = self.doc.get_mut();
let text = doc.get_text(TEXT_CONTAINER_ID);
text.delete(index, len)?;
text.delete(index, len)
.map_err(|err| TextCrdtError::Local(err))?;
doc.commit();
Ok(())
}

/// Applies encoded text deltas received from a remote peer.
///
/// Deltas are encoded according to the Loro specification.
pub fn apply_encoded_delta(&mut self, bytes: &[u8]) -> Result<()> {
pub fn apply_encoded_delta(&mut self, bytes: &[u8]) -> Result<(), TextCrdtError> {
let doc = self.doc.get_mut();
doc.import_with(bytes, "delta")?;
doc.import_with(bytes, "delta")
.map_err(|err| TextCrdtError::Imported(err))?;
Ok(())
}

Expand All @@ -172,7 +177,7 @@ impl TextCrdt {

/// Applies local text changes.
#[cfg(test)]
fn apply_delta(&mut self, delta: TextDelta) -> Result<()> {
fn apply_delta(&mut self, delta: TextDelta) -> Result<(), TextCrdtError> {
match delta {
TextDelta::Insert { index, chunk } => {
self.insert(index, &chunk)?;
Expand All @@ -181,7 +186,6 @@ impl TextCrdt {
self.remove(index, len)?;
}
}

Ok(())
}
}
Expand Down Expand Up @@ -292,6 +296,15 @@ fn absolute_deltas(loro_deltas: Vec<loro::TextDelta>) -> Vec<TextDelta> {
deltas
}

#[derive(Debug, Error)]
pub enum TextCrdtError {
#[error("could not apply local text change: {0}")]
Local(loro::LoroError),

#[error("could not apply imported text change: {0}")]
Imported(loro::LoroError),
}

#[cfg(test)]
mod tests {
use super::{TextCrdt, TextCrdtEvent};
Expand Down

0 comments on commit 080da26

Please sign in to comment.