libmotor-sys 0.1.0

cgo binding for packages
Documentation
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use std::ffi::{CString, CStr};

include!("./bindings.rs");

pub fn aes_encrypt_with_key(key: String, plaintext: String) -> String {
    let keyStr = string_to_i8_ptr(key);
    let textStr = string_to_i8_ptr(plaintext);
    let result = unsafe { aesEncryptWithKey(keyStr, textStr) };
    return i8_ptr_to_string(result);
}

fn string_to_i8_ptr(s: String) -> *mut i8 {
    let s = String::from(s);
    let cs = CString::new(s).unwrap();
    let cv: Vec<u8> = cs.into_bytes_with_nul();
    let mut tmp: Vec<i8> = cv.into_iter().map(|c| c as i8).collect::<_>(); // line 7
    let _cptr: *mut i8 = tmp.as_mut_ptr();
    return _cptr;
}

fn i8_ptr_to_string(p: *mut i8) -> String {
  let c_str: &CStr = unsafe { CStr::from_ptr(p) };
  let str_slice: &str = c_str.to_str().unwrap();
  return str_slice.to_owned();
}