Commit 670e3d33 authored by nanocryk's avatar nanocryk
Browse files

feature : many WebOfTrust methods

parent 6503d2fe
......@@ -4,12 +4,12 @@
extern crate neon;
extern crate duniter_rs_wotb;
use neon::js::{Value, JsInteger, JsString, JsBoolean, JsFunction, Object};
use neon::js::{Value, JsInteger, JsString, JsBoolean, JsFunction, JsArray, Object};
use neon::js::class::{Class, JsClass};
use neon::mem::Handle;
use neon::vm::{Throw, Lock};
use duniter_rs_wotb::WebOfTrust;
use duniter_rs_wotb::{WebOfTrust, NodeId};
declare_types! {
......@@ -26,8 +26,6 @@ declare_types! {
true => Ok(WebOfTrust::new(max_cert as usize)),
false => Err(Throw),
}
} else if let Ok(path) = arg0.check::<JsString>() {
let path = path.value();
......@@ -37,7 +35,7 @@ declare_types! {
}
} else {
Err(Throw)
}
}
}
method to_file(call) {
......@@ -50,6 +48,131 @@ declare_types! {
Ok(JsBoolean::new(scope, result).upcast())
}
method add_node(call) {
let scope = call.scope;
let id = call.arguments.this(scope).grab(|wot| {
wot.add_node()
});
Ok(JsInteger::new(scope, *id as i32).upcast())
}
method remove_node(call) {
let scope = call.scope;
let id = call.arguments.this(scope).grab(|wot| {
match wot.remove_node() {
Some(id) => *id as i32,
None => -1,
}
});
Ok(JsInteger::new(scope, id).upcast())
}
method get_sentries(call) {
let scope = call.scope;
let arg0 = try!(call.arguments.require(scope, 0));
let d_min = try!(arg0.check::<JsInteger>()).value() as usize;
let array = call.arguments.this(scope).grab(|wot| {
wot.get_sentries(d_min)
});
let jsarray: Handle<JsArray> = JsArray::new(scope, array.len() as u32);
for (index, &item) in array.iter().enumerate() {
try!(jsarray.set(index as u32, JsInteger::new(scope, *item as i32)));
}
Ok(jsarray.upcast())
}
method get_non_sentries(call) {
let scope = call.scope;
let arg0 = try!(call.arguments.require(scope, 0));
let d_min = try!(arg0.check::<JsInteger>()).value() as usize;
let array = call.arguments.this(scope).grab(|wot| {
wot.get_non_sentries(d_min)
});
let jsarray: Handle<JsArray> = JsArray::new(scope, array.len() as u32);
for (index, &item) in array.iter().enumerate() {
try!(jsarray.set(index as u32, JsInteger::new(scope, *item as i32)));
}
Ok(jsarray.upcast())
}
method disabled(call) {
let scope = call.scope;
let array = call.arguments.this(scope).grab(|wot| {
wot.get_disabled()
});
let jsarray: Handle<JsArray> = JsArray::new(scope, array.len() as u32);
for (index, &item) in array.iter().enumerate() {
try!(jsarray.set(index as u32, JsInteger::new(scope, *item as i32)));
}
Ok(jsarray.upcast())
}
method get_paths(call) {
let scope = call.scope;
let from = try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value() as usize;
let to = try!(try!(call.arguments.require(scope, 1)).check::<JsInteger>()).value() as usize;
let k_max = try!(try!(call.arguments.require(scope, 2)).check::<JsInteger>()).value() as u32;
let paths = call.arguments.this(scope).grab(|wot| {
wot.get_paths(NodeId(from), NodeId(to), k_max)
});
let jsarray: Handle<JsArray> = JsArray::new(scope, paths.len() as u32);
for(index, ref inner_array) in paths.iter().enumerate() {
let inner_jsarray: Handle<JsArray> = JsArray::new(scope, inner_array.len() as u32);
for(inner_index, &item) in inner_array.iter().enumerate() {
try!(inner_jsarray.set(inner_index as u32, JsInteger::new(scope, *item as i32)));
}
try!(jsarray.set(index as u32, inner_jsarray));
}
Ok(jsarray.upcast())
}
method size(call) {
let scope = call.scope;
let size = call.arguments.this(scope).grab(|wot| {
wot.size()
});
Ok(JsInteger::new(scope, size as i32).upcast())
}
method is_enabled(call) {
let scope = call.scope;
let node = try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value() as usize;
let state = call.arguments.this(scope).grab(|wot| {
wot.is_enabled(NodeId(node)).unwrap_or(false)
});
Ok(JsBoolean::new(scope, state).upcast())
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment