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

chore(rust): cargo fmt and refactor into two workspace with two crates #164

Merged
merged 9 commits into from
Nov 1, 2021
Next Next commit
chore(rust): cargo fmt
willemneal committed Oct 28, 2021
commit 6d5e14f3b01782f42004d2b1c6dc464ee847664c
36 changes: 25 additions & 11 deletions api/rust/suborbital/src/cache.rs
Original file line number Diff line number Diff line change
@@ -2,25 +2,39 @@ use crate::ffi;
use crate::runnable::HostErr;
use crate::STATE;

extern {
fn cache_set(key_pointer: *const u8, key_size: i32, value_pointer: *const u8, value_size: i32, ttl: i32, ident: i32) -> i32;
fn cache_get(key_pointer: *const u8, key_size: i32, ident: i32) -> i32;
extern "C" {
fn cache_set(
key_pointer: *const u8,
key_size: i32,
value_pointer: *const u8,
value_size: i32,
ttl: i32,
ident: i32,
) -> i32;
fn cache_get(key_pointer: *const u8, key_size: i32, ident: i32) -> i32;
}

pub fn set(key: &str, val: Vec<u8>, ttl: i32) {
let val_slice = val.as_slice();
let val_ptr = val_slice.as_ptr();
let val_slice = val.as_slice();
let val_ptr = val_slice.as_ptr();

unsafe {
cache_set(key.as_ptr(), key.len() as i32, val_ptr, val.len() as i32, ttl, super::STATE.ident);
}
unsafe {
cache_set(
key.as_ptr(),
key.len() as i32,
val_ptr,
val.len() as i32,
ttl,
super::STATE.ident,
);
}
}

/// Executes the request via FFI
///
/// Then retreives the result from the host and returns it
pub fn get(key: &str) -> Result<Vec<u8>, HostErr> {
let result_size = unsafe { cache_get(key.as_ptr(), key.len() as i32, STATE.ident) };
ffi::result(result_size)
let result_size = unsafe { cache_get(key.as_ptr(), key.len() as i32, STATE.ident) };

ffi::result(result_size)
}
50 changes: 23 additions & 27 deletions api/rust/suborbital/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -9,41 +9,37 @@ extern {
fn add_ffi_var(name_ptr: *const u8, name_len: i32, val_ptr: *const u8, val_len: i32, ident: i32) -> i32;
}

pub (crate) fn result(size: i32) -> Result<Vec<u8>, HostErr> {
let mut alloc_size = size;
pub(crate) fn result(size: i32) -> Result<Vec<u8>, HostErr> {
let mut alloc_size = size;

// FFI functions return negative values when an error occurs
if size < 0 {
if size == -1 {
return Err(HostErr::new("unknown error returned from host"))
}
// FFI functions return negative values when an error occurs
if size < 0 {
if size == -1 {
return Err(HostErr::new("unknown error returned from host"));
}

alloc_size = -size
}
alloc_size = -size
}

// create some memory for the host to write into
let mut result_mem = Vec::with_capacity(alloc_size as usize);
let result_ptr = result_mem.as_mut_slice().as_mut_ptr() as *const u8;
// create some memory for the host to write into
let mut result_mem = Vec::with_capacity(alloc_size as usize);
let result_ptr = result_mem.as_mut_slice().as_mut_ptr() as *const u8;

let code = unsafe {
get_ffi_result(result_ptr, STATE.ident)
};
let code = unsafe { get_ffi_result(result_ptr, STATE.ident) };

// check if it was successful, and then re-build the memory
if code != 0 {
return Err(HostErr::new("unknown error returned from host"));
}
// check if it was successful, and then re-build the memory
if code != 0 {
return Err(HostErr::new("unknown error returned from host"));
}

let data: &[u8] = unsafe {
slice::from_raw_parts(result_ptr, alloc_size as usize)
};
let data: &[u8] = unsafe { slice::from_raw_parts(result_ptr, alloc_size as usize) };

if size < 0 {
let msg = Vec::from(data);
return Err(HostErr::new(util::to_string(msg).as_str()))
}
if size < 0 {
let msg = Vec::from(data);
return Err(HostErr::new(util::to_string(msg).as_str()));
}

Ok(Vec::from(data))
Ok(Vec::from(data))
}

pub(crate) fn add_var(name: &str, value: &str) {
18 changes: 8 additions & 10 deletions api/rust/suborbital/src/file.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use crate::STATE;
use crate::ffi;
use crate::STATE;

extern {
fn get_static_file(name_ptr: *const u8, name_size: i32, ident: i32) -> i32;
extern "C" {
fn get_static_file(name_ptr: *const u8, name_size: i32, ident: i32) -> i32;
}

/// Executes the request via FFI
///
/// Then retreives the result from the host and returns it
pub fn get_static(name: &str) -> Option<Vec<u8>> {
let result_size = unsafe { get_static_file(name.as_ptr(), name.len() as i32, STATE.ident) };
let result_size = unsafe { get_static_file(name.as_ptr(), name.len() as i32, STATE.ident) };

match ffi::result(result_size) {
Ok(res) => Some(res),
Err(_) => {
None
}
}
match ffi::result(result_size) {
Ok(res) => Some(res),
Err(_) => None,
}
}
30 changes: 21 additions & 9 deletions api/rust/suborbital/src/graphql.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
use crate::ffi;
use crate::runnable::HostErr;
use crate::STATE;
use crate::ffi;

extern {
fn graphql_query(endpoint_pointer: *const u8, endpoint_size: i32, query_pointer: *const u8, query_size: i32, ident: i32) -> i32;
extern "C" {
fn graphql_query(
endpoint_pointer: *const u8,
endpoint_size: i32,
query_pointer: *const u8,
query_size: i32,
ident: i32,
) -> i32;
}

/// Retreives the result from the host and returns it
pub fn query(endpoint: &str, query: &str) -> Result<Vec<u8>, HostErr> {
let endpoint_size = endpoint.len() as i32;
let query_size = query.len() as i32;
let endpoint_size = endpoint.len() as i32;
let query_size = query.len() as i32;

let result_size = unsafe {
graphql_query(endpoint.as_ptr(), endpoint_size, query.as_ptr(), query_size, STATE.ident)
};
let result_size = unsafe {
graphql_query(
endpoint.as_ptr(),
endpoint_size,
query.as_ptr(),
query_size,
STATE.ident,
)
};

ffi::result(result_size)
ffi::result(result_size)
}
123 changes: 75 additions & 48 deletions api/rust/suborbital/src/http.rs
Original file line number Diff line number Diff line change
@@ -2,29 +2,44 @@ pub mod method;

use std::collections::BTreeMap;

use crate::runnable::HostErr;
use crate::ffi;
use crate::runnable::HostErr;
use crate::STATE;
use method::Method;

extern {
fn fetch_url(method: i32, url_pointer: *const u8, url_size: i32, body_pointer: *const u8, body_size: i32, ident: i32) -> i32;
extern "C" {
fn fetch_url(
method: i32,
url_pointer: *const u8,
url_size: i32,
body_pointer: *const u8,
body_size: i32,
ident: i32,
) -> i32;
}

pub fn get(url: &str, headers: Option<BTreeMap<&str, &str>>) -> Result<Vec<u8>, HostErr> {
do_request(Method::GET.into(), url, None, headers)
do_request(Method::GET.into(), url, None, headers)
}

pub fn post(url: &str, body: Option<Vec<u8>>, headers: Option<BTreeMap<&str, &str>>) -> Result<Vec<u8>, HostErr> {
do_request(Method::POST.into(), url, body, headers)
pub fn post(
url: &str,
body: Option<Vec<u8>>,
headers: Option<BTreeMap<&str, &str>>,
) -> Result<Vec<u8>, HostErr> {
do_request(Method::POST.into(), url, body, headers)
}

pub fn patch(url: &str, body: Option<Vec<u8>>, headers: Option<BTreeMap<&str, &str>>) -> Result<Vec<u8>, HostErr> {
do_request(Method::PATCH.into(), url, body, headers)
pub fn patch(
url: &str,
body: Option<Vec<u8>>,
headers: Option<BTreeMap<&str, &str>>,
) -> Result<Vec<u8>, HostErr> {
do_request(Method::PATCH.into(), url, body, headers)
}

pub fn delete(url: &str, headers: Option<BTreeMap<&str, &str>>) -> Result<Vec<u8>, HostErr> {
do_request(Method::DELETE.into(), url, None, headers)
do_request(Method::DELETE.into(), url, None, headers)
}

/// Executes the request via FFI
@@ -33,50 +48,62 @@ pub fn delete(url: &str, headers: Option<BTreeMap<&str, &str>>) -> Result<Vec<u8
///
/// > Remark: The URL gets encoded with headers added on the end, seperated by ::
/// > eg. https://google.com/somepage::authorization:bearer qdouwrnvgoquwnrg::anotherheader:nicetomeetyou
fn do_request(method: i32, url: &str, body: Option<Vec<u8>>, headers: Option<BTreeMap<&str, &str>>) -> Result<Vec<u8>, HostErr> {
let header_string = render_header_string(headers);

let url_string = match header_string {
Some(h) => format!("{}::{}", url, h),
None => String::from(url)
};

let body_pointer: *const u8;
let mut body_size: i32 = 0;

match body {
Some(b) => {
let body_slice = b.as_slice();
body_pointer = body_slice.as_ptr();
body_size = b.len() as i32;
},
None => body_pointer = std::ptr::null::<u8>()
}

let result_size = unsafe {
fetch_url(method, url_string.as_str().as_ptr(), url_string.len() as i32, body_pointer, body_size, STATE.ident)
};

ffi::result(result_size)
fn do_request(
method: i32,
url: &str,
body: Option<Vec<u8>>,
headers: Option<BTreeMap<&str, &str>>,
) -> Result<Vec<u8>, HostErr> {
let header_string = render_header_string(headers);

let url_string = match header_string {
Some(h) => format!("{}::{}", url, h),
None => String::from(url),
};

let body_pointer: *const u8;
let mut body_size: i32 = 0;

match body {
Some(b) => {
let body_slice = b.as_slice();
body_pointer = body_slice.as_ptr();
body_size = b.len() as i32;
}
None => body_pointer = std::ptr::null::<u8>(),
}

let result_size = unsafe {
fetch_url(
method,
url_string.as_str().as_ptr(),
url_string.len() as i32,
body_pointer,
body_size,
STATE.ident,
)
};

ffi::result(result_size)
}

fn render_header_string(headers: Option<BTreeMap<&str, &str>>) -> Option<String> {
let mut rendered: String = String::from("");
let header_map = headers?;
let mut rendered: String = String::from("");

let header_map = headers?;

for key in header_map.keys() {
rendered.push_str(key);
rendered.push(':');
for key in header_map.keys() {
rendered.push_str(key);
rendered.push(':');

let val: &str = match header_map.get(key) {
Some(v) => v,
None => "",
};
let val: &str = match header_map.get(key) {
Some(v) => v,
None => "",
};

rendered.push_str(val);
rendered.push_str("::")
}
rendered.push_str(val);
rendered.push_str("::")
}

Some(String::from(rendered.trim_end_matches("::")))
Some(String::from(rendered.trim_end_matches("::")))
}
22 changes: 11 additions & 11 deletions api/rust/suborbital/src/http/method.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
pub enum Method {
GET,
POST,
PATCH,
DELETE
GET,
POST,
PATCH,
DELETE,
}

impl From<Method> for i32 {
fn from(field_type: Method) -> Self {
match field_type {
Method::GET => 0,
Method::POST => 1,
Method::PATCH => 2,
Method::DELETE => 3,
}
match field_type {
Method::GET => 0,
Method::POST => 1,
Method::PATCH => 2,
Method::DELETE => 3,
}
}
}
}
Loading