Skip to content

Commit

Permalink
chore: cleanup codes
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Sep 16, 2023
1 parent 6d41147 commit deb5b4a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 37 deletions.
14 changes: 7 additions & 7 deletions y-octo-node/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl YArray {
if let Some(value) = self.array.get(char_index as u64) {
get_js_unknown_from_value(env, value)
.map(Some)
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
} else {
Ok(None)
}
Expand All @@ -42,12 +42,12 @@ impl YArray {
ValueType::Undefined | ValueType::Null => self
.array
.insert(char_index as u64, Any::Null)
.map_err(|e| anyhow::Error::from(e)),
.map_err(anyhow::Error::from),
ValueType::Boolean => {
if let Ok(boolean) = value.coerce_to_bool().and_then(|v| v.get_value()) {
self.array
.insert(char_index as u64, boolean)
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
} else {
Err(anyhow::Error::msg("Failed to coerce value to boolean"))
}
Expand All @@ -56,7 +56,7 @@ impl YArray {
if let Ok(number) = value.coerce_to_number().and_then(|v| v.get_double()) {
self.array
.insert(char_index as u64, number)
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
} else {
Err(anyhow::Error::msg("Failed to coerce value to number"))
}
Expand All @@ -69,7 +69,7 @@ impl YArray {
{
self.array
.insert(char_index as u64, string)
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
} else {
Err(anyhow::Error::msg("Failed to coerce value to string"))
}
Expand All @@ -81,7 +81,7 @@ impl YArray {
if let Ok(any) = object.get_element::<JsUnknown>(i).and_then(get_any_from_js_unknown) {
self.array
.insert(char_index as u64 + i as u64, Value::Any(any))
.map_err(|e| anyhow::Error::from(e))?;
.map_err(anyhow::Error::from)?;
}
}
Ok(())
Expand All @@ -105,7 +105,7 @@ impl YArray {
pub fn remove(&mut self, char_index: i64, len: i64) -> Result<()> {
self.array
.remove(char_index as u64, len as u64)
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
}

#[napi]
Expand Down
18 changes: 9 additions & 9 deletions y-octo-node/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,46 +43,46 @@ impl Doc {
#[napi]
pub fn get_or_create_array(&self, key: String) -> Result<YArray> {
self.doc
.get_or_create_array(&key)
.get_or_create_array(key)
.map(YArray::new)
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
}

#[napi]
pub fn get_or_create_text(&self, key: String) -> Result<YText> {
self.doc
.get_or_create_text(&key)
.get_or_create_text(key)
.map(YText::new)
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
}

#[napi]
pub fn get_or_create_map(&self, key: String) -> Result<YMap> {
self.doc
.get_or_create_map(&key)
.get_or_create_map(key)
.map(YMap::new)
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
}

#[napi]
pub fn apply_update(&mut self, update: Buffer) -> Result<Buffer> {
self.doc
.apply_update_from_binary(update.to_vec())
.and_then(|u| u.into_ybinary1().map(|v| v.into()))
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
}

#[napi]
pub fn encode_update_v1(&self) -> Result<Buffer> {
self.doc
.encode_update_v1()
.map(|v| v.into())
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
}

#[napi]
pub fn gc(&self) -> Result<()> {
self.doc.gc().map_err(|e| anyhow::Error::from(e))
self.doc.gc().map_err(anyhow::Error::from)
}

#[napi(ts_args_type = "callback: (result: Uint8Array) => void")]
Expand Down
1 change: 0 additions & 1 deletion y-octo-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ pub use array::YArray;
pub use doc::Doc;
pub use map::YMap;
pub use text::YText;

use utils::{get_any_from_js_unknown, get_js_unknown_from_any, get_js_unknown_from_value};
16 changes: 6 additions & 10 deletions y-octo-node/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl YMap {
_ => env.get_null().map(|v| v.into_unknown()),
}
.map(Some)
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
} else {
Ok(None)
}
Expand All @@ -45,19 +45,17 @@ impl YMap {
pub fn set(&mut self, key: String, value: JsUnknown) -> Result<()> {
match value.get_type() {
Ok(value_type) => match value_type {
ValueType::Undefined | ValueType::Null => {
self.map.insert(key, Any::Null).map_err(|e| anyhow::Error::from(e))
}
ValueType::Undefined | ValueType::Null => self.map.insert(key, Any::Null).map_err(anyhow::Error::from),
ValueType::Boolean => {
if let Ok(boolean) = value.coerce_to_bool().and_then(|v| v.get_value()) {
self.map.insert(key, boolean).map_err(|e| anyhow::Error::from(e))
self.map.insert(key, boolean).map_err(anyhow::Error::from)
} else {
Err(anyhow::Error::msg("Failed to coerce value to boolean"))
}
}
ValueType::Number => {
if let Ok(number) = value.coerce_to_number().and_then(|v| v.get_double()) {
self.map.insert(key, number).map_err(|e| anyhow::Error::from(e))
self.map.insert(key, number).map_err(anyhow::Error::from)
} else {
Err(anyhow::Error::msg("Failed to coerce value to number"))
}
Expand All @@ -68,16 +66,14 @@ impl YMap {
.and_then(|v| v.into_utf8())
.and_then(|s| s.as_str().map(|s| s.to_string()))
{
self.map.insert(key, string).map_err(|e| anyhow::Error::from(e))
self.map.insert(key, string).map_err(anyhow::Error::from)
} else {
Err(anyhow::Error::msg("Failed to coerce value to string"))
}
}
ValueType::Object => {
if let Ok(any) = get_any_from_js_unknown(value) {
self.map
.insert(key, Value::Any(any))
.map_err(|e| anyhow::Error::from(e))
self.map.insert(key, Value::Any(any)).map_err(anyhow::Error::from)
} else {
Err(anyhow::Error::msg("Failed to coerce value to array"))
}
Expand Down
6 changes: 2 additions & 4 deletions y-octo-node/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ impl YText {

#[napi]
pub fn insert(&mut self, char_index: i64, str: String) -> Result<()> {
self.text
.insert(char_index as u64, str)
.map_err(|e| anyhow::Error::from(e))
self.text.insert(char_index as u64, str).map_err(anyhow::Error::from)
}

#[napi]
pub fn remove(&mut self, char_index: i64, len: i64) -> Result<()> {
self.text
.remove(char_index as u64, len as u64)
.map_err(|e| anyhow::Error::from(e))
.map_err(anyhow::Error::from)
}

#[napi(getter)]
Expand Down
11 changes: 5 additions & 6 deletions y-octo-node/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::HashMap;

use super::*;
use napi::{Env, Error, JsUnknown, Result, Status, ValueType};
use y_octo::{Any, Value};

use super::*;

pub fn get_js_unknown_from_any(env: Env, any: Any) -> Result<JsUnknown> {
match any {
Any::Null | Any::Undefined => env.get_null().map(|v| v.into_unknown()),
Expand Down Expand Up @@ -52,7 +53,7 @@ pub fn get_any_from_js_unknown(js_unknown: JsUnknown) -> Result<Any> {
if let Ok(object) = js_unknown.coerce_to_object() {
if let Ok(length) = object.get_array_length() {
let mut array = Vec::with_capacity(length as usize);
for i in 0..length as u32 {
for i in 0..length {
if let Ok(value) = object.get_element::<JsUnknown>(i) {
array.push(get_any_from_js_unknown(value)?);
}
Expand All @@ -62,12 +63,10 @@ pub fn get_any_from_js_unknown(js_unknown: JsUnknown) -> Result<Any> {
let mut map = HashMap::new();
let keys = object.get_property_names()?;
if let Ok(length) = keys.get_array_length() {
for i in 0..length as u32 {
for i in 0..length {
if let Ok((obj, key)) = keys.get_element::<JsUnknown>(i).and_then(|o| {
o.coerce_to_string().and_then(|obj| {
obj.clone()
.into_utf8()
.and_then(|s| s.as_str().map(|s| (obj, s.to_string())))
obj.into_utf8().and_then(|s| s.as_str().map(|s| (obj, s.to_string())))
})
}) {
if let Ok(value) = object.get_property::<_, JsUnknown>(obj) {
Expand Down

0 comments on commit deb5b4a

Please sign in to comment.