Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: from value or handler #605

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 14 additions & 44 deletions crates/loro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use loro_internal::cursor::PosQueryResult;
use loro_internal::cursor::Side;
pub use loro_internal::encoding::ImportStatus;
use loro_internal::handler::HandlerTrait;
use loro_internal::handler::ValueOrHandler;
pub use loro_internal::loro::ChangeTravelError;
use loro_internal::undo::{OnPop, OnPush};
use loro_internal::version::shrink_frontiers;
Expand Down Expand Up @@ -883,14 +882,9 @@ impl LoroDoc {
#[inline]
#[cfg(feature = "jsonpath")]
pub fn jsonpath(&self, path: &str) -> Result<Vec<ValueOrContainer>, JsonPathError> {
self.doc.jsonpath(path).map(|vec| {
vec.into_iter()
.map(|v| match v {
ValueOrHandler::Value(v) => ValueOrContainer::Value(v),
ValueOrHandler::Handler(h) => ValueOrContainer::Container(h.into()),
})
.collect()
})
self.doc
.jsonpath(path)
.map(|vec| vec.into_iter().map(ValueOrContainer::from).collect())
}

/// Get the number of operations in the pending transaction.
Expand Down Expand Up @@ -1057,13 +1051,7 @@ impl LoroList {
/// Get the value at the given position.
#[inline]
pub fn get(&self, index: usize) -> Option<ValueOrContainer> {
match self.handler.get_(index) {
Some(ValueOrHandler::Handler(c)) => {
Some(ValueOrContainer::Container(Container::from_handler(c)))
}
Some(ValueOrHandler::Value(v)) => Some(ValueOrContainer::Value(v)),
None => None,
}
self.handler.get_(index).map(ValueOrContainer::from)
}

/// Get the deep value of the container.
Expand Down Expand Up @@ -1320,11 +1308,13 @@ impl LoroMap {
}

/// 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
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.
Expand All @@ -1349,13 +1339,7 @@ impl LoroMap {

/// Get the value of the map with the given key.
pub fn get(&self, key: &str) -> Option<ValueOrContainer> {
match self.handler.get_(key) {
None => None,
Some(ValueOrHandler::Handler(c)) => {
Some(ValueOrContainer::Container(Container::from_handler(c)))
}
Some(ValueOrHandler::Value(v)) => Some(ValueOrContainer::Value(v)),
}
self.handler.get_(key).map(ValueOrContainer::from)
}

/// Insert a container with the given type at the given key.
Expand Down Expand Up @@ -1412,10 +1396,7 @@ impl LoroMap {

/// Get the values of the map.
pub fn values(&self) -> impl Iterator<Item = ValueOrContainer> + '_ {
self.handler.values().map(|v| match v {
ValueOrHandler::Value(v) => ValueOrContainer::Value(v),
ValueOrHandler::Handler(c) => ValueOrContainer::Container(Container::from_handler(c)),
})
self.handler.values().map(ValueOrContainer::from)
}

/// Get the peer id of the last editor on the given entry
Expand Down Expand Up @@ -2223,13 +2204,7 @@ impl LoroMovableList {

/// Get the value at the given position.
pub fn get(&self, index: usize) -> Option<ValueOrContainer> {
match self.handler.get_(index) {
Some(ValueOrHandler::Handler(c)) => {
Some(ValueOrContainer::Container(Container::from_handler(c)))
}
Some(ValueOrHandler::Value(v)) => Some(ValueOrContainer::Value(v)),
None => None,
}
self.handler.get_(index).map(ValueOrContainer::from)
}

/// Get the length of the list.
Expand Down Expand Up @@ -2259,13 +2234,8 @@ impl LoroMovableList {

/// Pop the last element of the list.
pub fn pop(&self) -> LoroResult<Option<ValueOrContainer>> {
Ok(match self.handler.pop_()? {
Some(ValueOrHandler::Handler(c)) => {
Some(ValueOrContainer::Container(Container::from_handler(c)))
}
Some(ValueOrHandler::Value(v)) => Some(ValueOrContainer::Value(v)),
None => None,
})
let ans = self.handler.pop_()?.map(ValueOrContainer::from);
Ok(ans)
}

/// Push a value to the end of the list.
Expand Down