Skip to content

RUST Interface

Jos Denys edited this page Oct 19, 2021 · 2 revisions

Calling the C interface from RUST

use std::str::FromStr;
use std::env;
use std::os::raw::c_int;
use std::ffi::CString;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::ptr;

#[link(name = "iknowengine")]
extern {
    pub fn iknow_json(request: *const c_char, response: *mut *const c_char) -> c_int;
}

fn main() {
    let request1 = r#"{"method" : "GetLanguagesSet"}"#;
    let c_request1 = CString::new(request1).unwrap();
    let request2 = r#"{"method" : "NormalizeText", "language":"fr", "text_source" : "Risque d'exploitation"}"#;
    let c_request2 = CString::new(request2).unwrap();
    let request3 = r#"{"method" : "IdentifyLanguage", "text_source" : "Микротерминатор может развивать скорость до 30 сантиметров за секунду, пишут калининградские СМИ."}"#;
    let c_request3 = CString::new(request3).unwrap();
    let request4 = r#"{"method" : "index", "language" : "en", "text_source" : "This is a test of the Python interface to the iKnow engine. Be the change you want to see in life. Now, I have been on many walking holidays, but never on one where I have my bags ferried\nfrom hotel to hotel while I simply get on with the job of walkingand enjoying myself.", "b_trace" : true}"#;
    let c_request4 = CString::new(request4).unwrap();

    unsafe {
        let mut response: *const c_char = ptr::null_mut();

        eprintln!("request {:?}", c_request1);
        let iknow_ret = iknow_json(c_request1.as_ptr(),&mut response);
        eprintln!("return {}", iknow_ret);
        let response_string = CStr::from_ptr(response).to_string_lossy();
        eprintln!("response {:?}", response_string);
        let json: serde_json::Value =
            serde_json::from_str(&response_string).expect("JSON was not well-formatted");

        eprintln!("request {:?}", c_request2);
        let iknow_ret = iknow_json(c_request2.as_ptr(),&mut response);
        eprintln!("return {}", iknow_ret);
        let response_string = CStr::from_ptr(response).to_string_lossy();
        eprintln!("response {:?}", response_string);
        let json: serde_json::Value =
            serde_json::from_str(&response_string).expect("JSON was not well-formatted");

        eprintln!("request {:?}", c_request3);
        let iknow_ret = iknow_json(c_request3.as_ptr(),&mut response);
        eprintln!("return {}", iknow_ret);
        let response_string = CStr::from_ptr(response).to_string_lossy();
        eprintln!("response {:?}", response_string);
        let json: serde_json::Value =
            serde_json::from_str(&response_string).expect("JSON was not well-formatted");
    
        eprintln!("request {:?}", c_request4);
        let iknow_ret = iknow_json(c_request4.as_ptr(),&mut response);
        eprintln!("return {}", iknow_ret);
        let response_string = CStr::from_ptr(response).to_string_lossy();
        eprintln!("response {:?}", response_string);
        let json: serde_json::Value =
            serde_json::from_str(&response_string).expect("JSON was not well-formatted");
    
    }

Add these dependencies to the Cargo.toml file

[dependencies]
serde = { version = "1.0.104", features = ["derive"] }
serde_json = "1.0.48"

And the reference to the library in a separate "build.rs" file:

fn main() {
    println!(r"cargo:rustc-link-search=native=/home/jdenys/iknow/kit/lnxubuntux64/release/bin");
}

For building and running, set the correct LD_LIBRARY_PATH parameter:

export LD_LIBRARY_PATH=$HOME/iknow/kit/lnxubuntux64/release/bin:$HOME/iknow/thirdparty/icu/lib