fix: from value or handler
Some checks are pending
Release WASM / Release (push) Waiting to run
Test All / build (push) Waiting to run

This commit is contained in:
Leon Zhao 2025-01-05 15:21:23 +08:00
parent 8039e446a8
commit 567067b4d1

View file

@ -10,7 +10,6 @@ use loro_internal::cursor::PosQueryResult;
use loro_internal::cursor::Side; use loro_internal::cursor::Side;
pub use loro_internal::encoding::ImportStatus; pub use loro_internal::encoding::ImportStatus;
use loro_internal::handler::HandlerTrait; use loro_internal::handler::HandlerTrait;
use loro_internal::handler::ValueOrHandler;
pub use loro_internal::loro::ChangeTravelError; pub use loro_internal::loro::ChangeTravelError;
use loro_internal::undo::{OnPop, OnPush}; use loro_internal::undo::{OnPop, OnPush};
use loro_internal::version::shrink_frontiers; use loro_internal::version::shrink_frontiers;
@ -883,14 +882,9 @@ impl LoroDoc {
#[inline] #[inline]
#[cfg(feature = "jsonpath")] #[cfg(feature = "jsonpath")]
pub fn jsonpath(&self, path: &str) -> Result<Vec<ValueOrContainer>, JsonPathError> { pub fn jsonpath(&self, path: &str) -> Result<Vec<ValueOrContainer>, JsonPathError> {
self.doc.jsonpath(path).map(|vec| { self.doc
vec.into_iter() .jsonpath(path)
.map(|v| match v { .map(|vec| vec.into_iter().map(ValueOrContainer::from).collect())
ValueOrHandler::Value(v) => ValueOrContainer::Value(v),
ValueOrHandler::Handler(h) => ValueOrContainer::Container(h.into()),
})
.collect()
})
} }
/// Get the number of operations in the pending transaction. /// Get the number of operations in the pending transaction.
@ -1063,13 +1057,7 @@ impl LoroList {
/// Get the value at the given position. /// Get the value at the given position.
#[inline] #[inline]
pub fn get(&self, index: usize) -> Option<ValueOrContainer> { pub fn get(&self, index: usize) -> Option<ValueOrContainer> {
match self.handler.get_(index) { self.handler.get_(index).map(ValueOrContainer::from)
Some(ValueOrHandler::Handler(c)) => {
Some(ValueOrContainer::Container(Container::from_handler(c)))
}
Some(ValueOrHandler::Value(v)) => Some(ValueOrContainer::Value(v)),
None => None,
}
} }
/// Get the deep value of the container. /// Get the deep value of the container.
@ -1326,11 +1314,13 @@ impl LoroMap {
} }
/// Iterate over the key-value pairs of the map. /// Iterate over the key-value pairs of the map.
pub fn for_each<I>(&self, f: I) pub fn for_each<I>(&self, mut f: I)
where where
I: FnMut(&str, ValueOrHandler), I: FnMut(&str, ValueOrContainer),
{ {
self.handler.for_each(f) self.handler.for_each(|k, v| {
f(k, ValueOrContainer::from(v));
})
} }
/// Insert a key-value pair into the map. /// Insert a key-value pair into the map.
@ -1355,13 +1345,7 @@ impl LoroMap {
/// Get the value of the map with the given key. /// Get the value of the map with the given key.
pub fn get(&self, key: &str) -> Option<ValueOrContainer> { pub fn get(&self, key: &str) -> Option<ValueOrContainer> {
match self.handler.get_(key) { self.handler.get_(key).map(ValueOrContainer::from)
None => None,
Some(ValueOrHandler::Handler(c)) => {
Some(ValueOrContainer::Container(Container::from_handler(c)))
}
Some(ValueOrHandler::Value(v)) => Some(ValueOrContainer::Value(v)),
}
} }
/// Insert a container with the given type at the given key. /// Insert a container with the given type at the given key.
@ -1418,10 +1402,7 @@ impl LoroMap {
/// Get the values of the map. /// Get the values of the map.
pub fn values(&self) -> impl Iterator<Item = ValueOrContainer> + '_ { pub fn values(&self) -> impl Iterator<Item = ValueOrContainer> + '_ {
self.handler.values().map(|v| match v { self.handler.values().map(ValueOrContainer::from)
ValueOrHandler::Value(v) => ValueOrContainer::Value(v),
ValueOrHandler::Handler(c) => ValueOrContainer::Container(Container::from_handler(c)),
})
} }
/// Get the peer id of the last editor on the given entry /// Get the peer id of the last editor on the given entry
@ -2229,13 +2210,7 @@ impl LoroMovableList {
/// Get the value at the given position. /// Get the value at the given position.
pub fn get(&self, index: usize) -> Option<ValueOrContainer> { pub fn get(&self, index: usize) -> Option<ValueOrContainer> {
match self.handler.get_(index) { self.handler.get_(index).map(ValueOrContainer::from)
Some(ValueOrHandler::Handler(c)) => {
Some(ValueOrContainer::Container(Container::from_handler(c)))
}
Some(ValueOrHandler::Value(v)) => Some(ValueOrContainer::Value(v)),
None => None,
}
} }
/// Get the length of the list. /// Get the length of the list.
@ -2265,13 +2240,8 @@ impl LoroMovableList {
/// Pop the last element of the list. /// Pop the last element of the list.
pub fn pop(&self) -> LoroResult<Option<ValueOrContainer>> { pub fn pop(&self) -> LoroResult<Option<ValueOrContainer>> {
Ok(match self.handler.pop_()? { let ans = self.handler.pop_()?.map(ValueOrContainer::from);
Some(ValueOrHandler::Handler(c)) => { Ok(ans)
Some(ValueOrContainer::Container(Container::from_handler(c)))
}
Some(ValueOrHandler::Value(v)) => Some(ValueOrContainer::Value(v)),
None => None,
})
} }
/// Push a value to the end of the list. /// Push a value to the end of the list.