refactor: fix type error

This commit is contained in:
Zixuan Chen 2022-10-31 12:33:44 +08:00
parent e0a472fd1a
commit 9d48e5df88
9 changed files with 26 additions and 40 deletions

View file

@ -1,5 +1,5 @@
{
"cSpell.words": ["smstring", "yspan"],
"cSpell.words": ["smstring", "thiserror", "yspan"],
"rust-analyzer.runnableEnv": {
"RUST_BACKTRACE": "full"
},

View file

@ -136,15 +136,7 @@ impl ContainerManager {
}
let container = self.get_mut(id).unwrap();
if container.type_() != id.container_type() {
Err(LoroError::ContainerTypeError {
id: id.clone(),
actual_type: container.type_(),
expected_type: id.container_type(),
})
} else {
Ok(container)
}
Ok(container)
}
}

View file

@ -6,9 +6,9 @@ use fxhash::FxHashMap;
use proptest::prelude::*;
use proptest::proptest;
use crate::container::Container;
use crate::isomorph::Irc;
use crate::value::proptest::gen_insert_value;
use crate::Container;
use crate::{fx_map, value::InsertValue, LoroCore, LoroValue};
@ -16,9 +16,8 @@ use crate::{fx_map, value::InsertValue, LoroCore, LoroValue};
fn basic() {
let mut loro = LoroCore::default();
let weak = Irc::downgrade(&loro.log_store);
let mut a = loro.get_map_container("map".into());
let container = a.as_mut();
container.insert("haha".into(), InsertValue::Int32(1), weak);
let mut container = loro.get_or_create_root_map("map".into()).unwrap();
container.insert("haha".into(), InsertValue::Int32(1));
let ans = fx_map!(
"haha".into() => LoroValue::Integer(1)
);
@ -39,12 +38,11 @@ mod map_proptest {
) {
let mut loro = LoroCore::default();
let weak = Irc::downgrade(&loro.log_store);
let mut a = loro.get_map_container("map".into());
let container = a.as_mut();
let mut container = loro.get_or_create_root_map("map".into()).unwrap();
let mut map: HashMap<String, InsertValue> = HashMap::new();
for (k, v) in key.iter().zip(value.iter()) {
map.insert(k.clone(), v.clone());
container.insert(k.clone().into(), v.clone(), weak.clone());
container.insert(k.clone().into(), v.clone());
let snapshot = container.get_value();
for (key, value) in snapshot.as_map().unwrap().iter() {
assert_eq!(map.get(&key.to_string()).map(|x|x.clone().into()), Some(value.clone()));

View file

@ -1,15 +1,7 @@
use thiserror::Error;
use crate::{container::ContainerID, ContainerType};
#[derive(Error, Debug)]
pub enum LoroError {
#[error("Expect container with the id of {id:?} has type {expected_type:?} but the actual type is {actual_type:?}.")]
ContainerTypeError {
id: ContainerID,
actual_type: ContainerType,
expected_type: ContainerType,
},
// #[error("the data for key `{0}` is not available")]
// Redaction(String),
// #[error("invalid header (expected {expected:?}, found {found:?})")]

View file

@ -177,13 +177,13 @@ impl Actionable for Vec<LoroCore> {
match action {
Action::Ins { content, pos, site } => {
self[*site as usize]
.get_or_create_text_container("text".into())
.get_or_create_root_text("text".into())
.unwrap()
.insert(*pos, content);
}
Action::Del { pos, len, site } => {
self[*site as usize]
.get_or_create_text_container("text".into())
.get_or_create_root_text("text".into())
.unwrap()
.delete(*pos, *len);
}
@ -201,7 +201,7 @@ impl Actionable for Vec<LoroCore> {
Action::Ins { pos, site, .. } => {
*site %= self.len() as u8;
let mut text = self[*site as usize]
.get_or_create_text_container("text".into())
.get_or_create_root_text("text".into())
.unwrap();
let value = text.get_value().as_string().unwrap();
*pos %= value.len() + 1;
@ -212,7 +212,7 @@ impl Actionable for Vec<LoroCore> {
Action::Del { pos, len, site } => {
*site %= self.len() as u8;
let mut text = self[*site as usize]
.get_or_create_text_container("text".into())
.get_or_create_root_text("text".into())
.unwrap();
if text.text_len() == 0 {
*len = 0;
@ -241,8 +241,8 @@ impl Actionable for Vec<LoroCore> {
}
fn check_eq(site_a: &mut LoroCore, site_b: &mut LoroCore) {
let mut a = site_a.get_or_create_text_container("text".into()).unwrap();
let mut b = site_b.get_or_create_text_container("text".into()).unwrap();
let mut a = site_a.get_or_create_root_text("text".into()).unwrap();
let mut b = site_b.get_or_create_root_text("text".into()).unwrap();
let value_a = a.get_value();
let value_b = b.get_value();
assert_eq!(value_a.as_string().unwrap(), value_b.as_string().unwrap());
@ -265,7 +265,7 @@ fn check_synced(sites: &mut [LoroCore]) {
pub fn test_single_client(mut actions: Vec<Action>) {
let mut store = LoroCore::new(Default::default(), Some(1));
let mut text_container = store.get_or_create_text_container("haha".into()).unwrap();
let mut text_container = store.get_or_create_root_text("haha".into()).unwrap();
let mut ground_truth = String::new();
let mut applied = Vec::new();
for action in actions

View file

@ -45,7 +45,7 @@ impl LoroCore {
}
#[inline(always)]
pub fn get_or_create_map_container(
pub fn get_or_create_root_map(
&mut self,
name: &str,
) -> Result<ContainerRefMut<MapContainer>, LoroError> {
@ -60,7 +60,7 @@ impl LoroCore {
}
#[inline(always)]
pub fn get_or_create_text_container(
pub fn get_or_create_root_text(
&mut self,
name: &str,
) -> Result<ContainerRefMut<TextContainer>, LoroError> {

View file

@ -5,7 +5,7 @@ use loro_core::LoroCore;
#[test]
fn test() {
let mut store = LoroCore::new(Default::default(), Some(10));
let mut text_container = store.get_or_create_text_container("haha".into());
let mut text_container = store.get_or_create_root_text("haha").unwrap();
text_container.insert(0, "012");
text_container.insert(1, "34");
text_container.insert(1, "56");
@ -17,7 +17,7 @@ fn test() {
let mut store_b = LoroCore::new(Default::default(), Some(11));
let exported = store.export(Default::default());
store_b.import(exported);
let mut text_container = store_b.get_or_create_text_container("haha".into());
let mut text_container = store_b.get_or_create_root_text("haha").unwrap();
text_container.check();
let value = text_container.get_value();
let value = value.as_string().unwrap();
@ -31,7 +31,7 @@ fn test() {
drop(text_container);
store.import(store_b.export(store.vv()));
let mut text_container = store.get_or_create_text_container("haha".into());
let mut text_container = store.get_or_create_root_text("haha".into()).unwrap();
let value = text_container.get_value();
let value = value.as_string().unwrap();
assert_eq!(value.as_str(), "63417892");
@ -43,7 +43,7 @@ fn test() {
drop(text_container);
store_b.import(store.export(Default::default()));
let mut text_container = store_b.get_or_create_text_container("haha".into());
let mut text_container = store_b.get_or_create_root_text("haha".into()).unwrap();
text_container.check();
let value = text_container.get_value();
let value = value.as_string().unwrap();

View file

@ -8,3 +8,7 @@ a.insert(0, "hello world");
a.delete(6, 5);
a.insert(6, "everyone");
console.log(a.get_value());
const b = loro.get_map_container("ha");
b.set("ab", 123);
console.log(b.get_value());
console.log(a.get_value());

View file

@ -38,7 +38,7 @@ impl Loro {
pub fn get_text_container(&mut self, name: &str) -> Result<Text, JsValue> {
let mut loro = self.loro.borrow_mut();
let text_container = loro.get_or_create_text_container(name)?;
let text_container = loro.get_or_create_root_text(name)?;
Ok(Text {
id: text_container.id().clone(),
loro: Rc::downgrade(&self.loro),
@ -47,7 +47,7 @@ impl Loro {
pub fn get_map_container(&mut self, name: &str) -> Result<Map, JsValue> {
let mut loro = self.loro.borrow_mut();
let map = loro.get_or_create_map_container(name)?;
let map = loro.get_or_create_root_map(name)?;
Ok(Map {
id: map.id().clone(),
loro: Rc::downgrade(&self.loro),