Skip to content

Commit

Permalink
Add working render_roles call
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Jun 27, 2018
1 parent 04cd2a7 commit 70b8929
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions example-configs/wasm-with-query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ description = """
version = "0.1.0"

[dependencies]
serde = "1.0.0"
serde_json = "1.0.0"
28 changes: 24 additions & 4 deletions example-configs/wasm-with-query/src/bin/query.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
extern crate serde;
#[macro_use] extern crate serde_json;

use std::mem;
use std::slice;
use std::panic::set_hook;
use std::os::raw::{c_void};

use serde::{Serialize, Deserialize};
use serde_json::{Value, from_slice, to_vec};

extern {
Expand Down Expand Up @@ -38,13 +40,31 @@ fn main() {
#[no_mangle]
pub extern "C" fn init(ptr: *const u8, len: usize) -> *mut c_void {
let input = unsafe { slice::from_raw_parts(ptr, len) };
let mut out = _init_wrapper(input);
let mut out = _wrapper(input, _init);
let out_ptr = out.as_mut_ptr();
mem::forget(out);
return out_ptr as *mut c_void;
}

fn _init_wrapper(data: &[u8]) -> Vec<u8> {
#[no_mangle]
pub extern "C" fn render_roles(ptr: *const u8, len: usize) -> *mut c_void {
let input = unsafe { slice::from_raw_parts(ptr, len) };
let mut out = _wrapper(input, _render_roles);
let out_ptr = out.as_mut_ptr();
mem::forget(out);
return out_ptr as *mut c_void;
}

fn _render_roles(_input: Value) -> Result<Value, String> {
return Ok(json!({"imaginary_role": {}}))
}

fn _wrapper<'x, F, S, D>(data: &'x [u8], f: F) -> Vec<u8>
where
F: Fn(D) -> Result<S, String>,
D: Deserialize<'x>,
S: Serialize
{
let input = match from_slice(data) {
Ok(inp) => inp,
Err(e) => {
Expand All @@ -53,7 +73,7 @@ fn _init_wrapper(data: &[u8]) -> Vec<u8> {
})).expect("should serialize standard json");
}
};
let result = _init_inner(input);
let result = f(input);
match to_vec(&result) {
Ok(result) => result,
Err(e) => {
Expand All @@ -65,7 +85,7 @@ fn _init_wrapper(data: &[u8]) -> Vec<u8> {
}
}

fn _init_inner(_input: Value) -> Result<Value, String> {
fn _init(_input: Value) -> Result<Value, String> {
return Ok(json!(null))
}

Expand Down
4 changes: 3 additions & 1 deletion src/daemon/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ pub fn run(init: ResponderInit) {
}

impl Impl {
fn render_roles(&self, id: &str) -> Result<BTreeMap<String, Json>, Error> {
fn render_roles(&mut self, id: &str)
-> Result<BTreeMap<String, Json>, Error>
{
use self::Impl::*;
match self {
Empty => Err(err_msg("no schedule yet")),
Expand Down
13 changes: 11 additions & 2 deletions src/daemon/query/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ pub struct QueryInit<'a> {
hostname: &'a str,
}

#[derive(Debug, Serialize)]
pub struct RolesQuery<'a> {
deployment_id: &'a str,
}

impl Responder {
pub fn new(schedule: &Arc<Schedule>, settings: &Settings,
file: &Path)
Expand All @@ -38,10 +43,14 @@ impl Responder {
})
}

pub fn render_roles(&self, id: &str)
pub fn render_roles(&mut self, id: &str)
-> Result<BTreeMap<String, Json>, Error>
{
unimplemented!();
let result: Result<_, String>;
result = self.wasm.json_call("render_roles", &RolesQuery {
deployment_id: id,
})?;
return result.map_err(|e| err_msg(e));
}

pub fn schedule(&self) -> Arc<Schedule> {
Expand Down

0 comments on commit 70b8929

Please sign in to comment.