mirror of
https://github.com/loro-dev/loro.git
synced 2025-02-11 14:53:12 +00:00
fix: return Err when index out of bound
This commit is contained in:
parent
cdc084627d
commit
532eee09a4
3 changed files with 48 additions and 2 deletions
|
@ -284,6 +284,13 @@ impl TextHandler {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pos > self.len_event() {
|
||||||
|
return Err(LoroError::OutOfBound {
|
||||||
|
pos,
|
||||||
|
len: self.len_event(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let (entity_index, styles) = self
|
let (entity_index, styles) = self
|
||||||
.state
|
.state
|
||||||
.upgrade()
|
.upgrade()
|
||||||
|
@ -424,6 +431,11 @@ impl TextHandler {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let len = self.len_event();
|
||||||
|
if end > len {
|
||||||
|
return Err(LoroError::OutOfBound { pos: end, len });
|
||||||
|
}
|
||||||
|
|
||||||
let (entity_start, entity_end) = self
|
let (entity_start, entity_end) = self
|
||||||
.state
|
.state
|
||||||
.upgrade()
|
.upgrade()
|
||||||
|
@ -551,6 +563,13 @@ impl ListHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&self, txn: &mut Transaction, pos: usize, v: LoroValue) -> LoroResult<()> {
|
pub fn insert(&self, txn: &mut Transaction, pos: usize, v: LoroValue) -> LoroResult<()> {
|
||||||
|
if pos > self.len() {
|
||||||
|
return Err(LoroError::OutOfBound {
|
||||||
|
pos,
|
||||||
|
len: self.len(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(container) = v.as_container() {
|
if let Some(container) = v.as_container() {
|
||||||
self.insert_container(txn, pos, container.container_type())?;
|
self.insert_container(txn, pos, container.container_type())?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -631,6 +650,13 @@ impl ListHandler {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pos + len > self.len() {
|
||||||
|
return Err(LoroError::OutOfBound {
|
||||||
|
pos: pos + len,
|
||||||
|
len: self.len(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
txn.apply_local_op(
|
txn.apply_local_op(
|
||||||
self.container_idx,
|
self.container_idx,
|
||||||
crate::op::RawOpContent::List(ListOp::Delete(DeleteSpan {
|
crate::op::RawOpContent::List(ListOp::Delete(DeleteSpan {
|
||||||
|
|
|
@ -18,9 +18,26 @@ fn list() {
|
||||||
.into_map()
|
.into_map()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
map.insert_("Hello", LoroValue::from("u")).unwrap();
|
map.insert_("Hello", LoroValue::from("u")).unwrap();
|
||||||
|
let pos = map
|
||||||
|
.insert_container_("pos", ContainerType::Map)
|
||||||
|
.unwrap()
|
||||||
|
.into_map()
|
||||||
|
.unwrap();
|
||||||
|
pos.insert_("x", 0.into()).unwrap();
|
||||||
|
pos.insert_("y", 100.into()).unwrap();
|
||||||
|
|
||||||
let cid = map.id();
|
let cid = map.id();
|
||||||
let id = a.get_list("list").get(1);
|
let id = a.get_list("list").get(1);
|
||||||
assert_eq!(id.unwrap().as_container().unwrap(), &cid);
|
assert_eq!(id.as_ref().unwrap().as_container().unwrap(), &cid);
|
||||||
|
let map = a.get_map(id.unwrap().into_container().unwrap());
|
||||||
|
let new_pos = a.get_map(map.get("pos").unwrap().into_container().unwrap());
|
||||||
|
assert_eq!(
|
||||||
|
new_pos.get_deep_value().to_json_value(),
|
||||||
|
json!({
|
||||||
|
"x": 0,
|
||||||
|
"y": 100,
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -154,7 +154,10 @@ fn js_value_to_container_id(
|
||||||
kind: ContainerType,
|
kind: ContainerType,
|
||||||
) -> Result<ContainerID, JsValue> {
|
) -> Result<ContainerID, JsValue> {
|
||||||
if !cid.is_string() {
|
if !cid.is_string() {
|
||||||
return Err(JsValue::from_str("ContainerID must be a string"));
|
return Err(JsValue::from_str(&format!(
|
||||||
|
"ContainerID must be a string, but found {}",
|
||||||
|
cid.js_typeof().as_string().unwrap(),
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let s = cid.as_string().unwrap();
|
let s = cid.as_string().unwrap();
|
||||||
|
|
Loading…
Reference in a new issue