first version
This commit is contained in:
+27
-32
@@ -5,7 +5,9 @@ extern crate napi_derive;
|
||||
|
||||
use std::convert::TryInto;
|
||||
|
||||
use napi::{CallContext, Env, JsNumber, JsObject, Module, Result, Task};
|
||||
use napi::{CallContext, Env, JsNumber, JsObject, JsString, Module, Result, Task};
|
||||
|
||||
mod mdiff;
|
||||
|
||||
#[cfg(all(unix, not(target_env = "musl")))]
|
||||
#[global_allocator]
|
||||
@@ -13,41 +15,34 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
||||
|
||||
register_module!(example, init);
|
||||
|
||||
struct AsyncTask(u32);
|
||||
|
||||
impl Task for AsyncTask {
|
||||
type Output = u32;
|
||||
type JsValue = JsNumber;
|
||||
|
||||
fn compute(&mut self) -> Result<Self::Output> {
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
sleep(Duration::from_millis(self.0 as u64));
|
||||
Ok(self.0 * 2)
|
||||
}
|
||||
|
||||
fn resolve(&self, env: &mut Env, output: Self::Output) -> Result<Self::JsValue> {
|
||||
env.create_uint32(output)
|
||||
}
|
||||
}
|
||||
|
||||
fn init(module: &mut Module) -> Result<()> {
|
||||
module.create_named_method("sync", sync_fn)?;
|
||||
|
||||
module.create_named_method("sleep", sleep)?;
|
||||
module.create_named_method("diff", diff_char)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[js_function(1)]
|
||||
fn sync_fn(ctx: CallContext) -> Result<JsNumber> {
|
||||
let argument: u32 = ctx.get::<JsNumber>(0)?.try_into()?;
|
||||
#[js_function(2)]
|
||||
fn diff_char(ctx: CallContext) -> Result<JsObject> {
|
||||
let str1: String = ctx.get::<JsString>(0)?.try_into()?;
|
||||
let str2: String = ctx.get::<JsString>(1)?.try_into()?;
|
||||
|
||||
ctx.env.create_uint32(argument + 100)
|
||||
}
|
||||
let res = mdiff::diff_chars(str1.as_str(), str2.as_str());
|
||||
|
||||
#[js_function(1)]
|
||||
fn sleep(ctx: CallContext) -> Result<JsObject> {
|
||||
let argument: u32 = ctx.get::<JsNumber>(0)?.try_into()?;
|
||||
let task = AsyncTask(argument);
|
||||
ctx.env.spawn(task)
|
||||
let mut obj = ctx.env.create_array_with_length(res.len()).unwrap();
|
||||
|
||||
let mut idx = 0;
|
||||
for it in res.iter() {
|
||||
let mut item = ctx.env.create_object().unwrap();
|
||||
item.set_named_property(
|
||||
"count",
|
||||
ctx.env.create_int64(it.count.try_into().unwrap()).unwrap(),
|
||||
);
|
||||
item.set_named_property("value", ctx.env.create_string(&it.value).unwrap());
|
||||
item.set_named_property("added", ctx.env.get_boolean(it.added).unwrap());
|
||||
item.set_named_property("removed", ctx.env.get_boolean(it.removed).unwrap());
|
||||
|
||||
obj.set_index(idx, item);
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
Ok(obj)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
pub struct Res {
|
||||
pub count: usize,
|
||||
pub added: bool,
|
||||
pub removed: bool,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum Mode {
|
||||
INIT,
|
||||
BOTH,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
}
|
||||
|
||||
fn get_res(s: &String, mode: Mode) -> Res {
|
||||
Res {
|
||||
count: s.chars().count(),
|
||||
added: if mode == Mode::RIGHT { true } else { false },
|
||||
removed: if mode == Mode::LEFT { true } else { false },
|
||||
value: s.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn diff_chars(s1: &str, s2: &str) -> Vec<Res> {
|
||||
let mut mode = Mode::INIT;
|
||||
let mut s: String = String::new();
|
||||
|
||||
let mut result: Vec<Res> = Vec::new();
|
||||
|
||||
for d in diff::chars(s1, s2) {
|
||||
match d {
|
||||
diff::Result::Left(l) => {
|
||||
if mode != Mode::LEFT && mode != Mode::INIT {
|
||||
result.push(get_res(&s, mode));
|
||||
s.clear();
|
||||
}
|
||||
mode = Mode::LEFT;
|
||||
s.push(l);
|
||||
}
|
||||
diff::Result::Right(r) => {
|
||||
if mode != Mode::RIGHT && mode != Mode::INIT {
|
||||
result.push(get_res(&s, mode));
|
||||
s.clear();
|
||||
}
|
||||
mode = Mode::RIGHT;
|
||||
s.push(r);
|
||||
}
|
||||
diff::Result::Both(l, _) => {
|
||||
if mode != Mode::BOTH && mode != Mode::INIT {
|
||||
result.push(get_res(&s, mode));
|
||||
s.clear();
|
||||
}
|
||||
mode = Mode::BOTH;
|
||||
s.push(l);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if s.len() > 0 {
|
||||
result.push(get_res(&s, mode));
|
||||
s.clear();
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
Reference in New Issue
Block a user