feat: add prelim struct

This commit is contained in:
leeeon233 2023-01-19 12:19:08 +08:00 committed by Leonzhao
parent 83587c4d81
commit 43cb9cc6db
2 changed files with 64 additions and 1 deletions

View file

@ -90,7 +90,7 @@ impl ListContainer {
let store = m.read().unwrap();
let container = store.get_container(&container_id).unwrap();
drop(store);
prelim.integrate(ctx, container);
prelim.integrate(ctx, container)?;
Ok((event, Some(container_id)))
} else {
let value = value.into_value().unwrap();

View file

@ -1,6 +1,7 @@
use std::sync::{Mutex, Weak};
use enum_as_inner::EnumAsInner;
use fxhash::FxHashMap;
use crate::{
container::registry::ContainerInstance, context::Context, ContainerType, LoroError, LoroValue,
@ -87,3 +88,65 @@ impl From<f64> for PrelimValue {
PrelimValue::Value(v.into())
}
}
pub struct PrelimText(pub String);
impl Prelim for PrelimText {
fn convert_value(self) -> Result<(PrelimValue, Option<Self>), LoroError> {
Ok((PrelimValue::Container(ContainerType::Text), Some(self)))
}
fn integrate<C: Context>(
self,
ctx: &C,
container: Weak<Mutex<ContainerInstance>>,
) -> Result<(), LoroError> {
let text = container.upgrade().unwrap();
let mut text = text.try_lock().unwrap();
let text = text.as_text_mut().unwrap();
text.insert(ctx, 0, &self.0);
Ok(())
}
}
pub struct PrelimList(pub Vec<LoroValue>);
impl Prelim for PrelimList {
fn convert_value(self) -> Result<(PrelimValue, Option<Self>), LoroError> {
Ok((PrelimValue::Container(ContainerType::List), Some(self)))
}
fn integrate<C: Context>(
self,
ctx: &C,
container: Weak<Mutex<ContainerInstance>>,
) -> Result<(), LoroError> {
let list = container.upgrade().unwrap();
let mut list = list.try_lock().unwrap();
let list = list.as_list_mut().unwrap();
list.insert_batch(ctx, 0, self.0);
Ok(())
}
}
pub struct PrelimMap(pub FxHashMap<String, LoroValue>);
impl Prelim for PrelimMap {
fn convert_value(self) -> Result<(PrelimValue, Option<Self>), LoroError> {
Ok((PrelimValue::Container(ContainerType::Map), Some(self)))
}
fn integrate<C: Context>(
self,
ctx: &C,
container: Weak<Mutex<ContainerInstance>>,
) -> Result<(), LoroError> {
let map = container.upgrade().unwrap();
let mut map = map.try_lock().unwrap();
let map = map.as_map_mut().unwrap();
for (key, value) in self.0.into_iter() {
map.insert(ctx, key.into(), value)?;
}
Ok(())
}
}