我使用 snips 并构建了一个 C 库。我想用 Rust 将库连接到我的 Node 环境。
JavaScript
var ffi = require('ffi');
const ArrayType = require('ref-array');
var nlu = require('./nlu');
const StringArray = ArrayType('string');
var nlulib = '../cargo/target/x86_64-apple-darwin/release/libp_public_transport_nlu.dylib'
var nlu = ffi.Library(nlulib, {
"load": ['pointer', ['string']],
"execute": ['string', ['pointer', 'string', StringArray]]
});
var ptrToEngine = nlu.load("../snips_public_transport_engine");
var responseNLU = nlu.execute(ptrToEngine, "myQuery", ['bestIntent'], ['worstIntent']);
锈
#[no_mangle]
pub extern "C" fn execute(engine_pointer: *mut SnipsNluEngine, query: *const c_char, whitelist: &[u8], blacklist: &[u8]) -> CString {
let query_c_str = unsafe { CStr::from_ptr(query) };
let query_string = match query_c_str.to_str().map(|s| s.to_owned()){
Ok(string) => string,
Err(e) => e.to_string()
};
let engine = unsafe {
assert!(!engine_pointer.is_null());
&mut *engine_pointer
};
let result = engine.parse(query_string.trim(), None, None).unwrap();
let result_json = serde_json::to_string_pretty(&result).unwrap();
CString::new(result_json).unwrap()
}
该engine.parse
函数期望Into<Option<Vec<&'a str>>>
作为参数而不是None
,因此我需要将白名单和黑名单转换为这种格式。