From bf34eba3388d94589f7d043782fa4925358d98d8 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 01:22:57 +0800 Subject: [PATCH] update: single ffi rs file --- src/to-json/src/ffi.rs | 18 ++++++++++++++++++ src/to-json/src/lib.rs | 16 +--------------- 2 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 src/to-json/src/ffi.rs diff --git a/src/to-json/src/ffi.rs b/src/to-json/src/ffi.rs new file mode 100644 index 0000000..fef3e3a --- /dev/null +++ b/src/to-json/src/ffi.rs @@ -0,0 +1,18 @@ +use std::ffi::{c_char, CStr, CString}; + +#[no_mangle] +pub unsafe extern "C" fn free_rust_string(string: *const c_char) { + let _ = CString::from_raw(string as *mut _); +} + +#[no_mangle] +pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char { + let content: &str = CStr::from_ptr(content).to_str().unwrap(); + + // TODO: remove test code + println!("Raw content -> {}", content); + + // TODO: convert to JSON format + + CString::new(content).unwrap().into_raw() +} diff --git a/src/to-json/src/lib.rs b/src/to-json/src/lib.rs index 1ece23a..57ae9b9 100644 --- a/src/to-json/src/lib.rs +++ b/src/to-json/src/lib.rs @@ -1,15 +1 @@ -use std::ffi::{c_char, CStr, CString}; - -#[no_mangle] -pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char { - let content: &str = CStr::from_ptr(content).to_str().unwrap(); - - println!("Raw content -> {}", content); - - CString::new(content).unwrap().into_raw() -} - -#[no_mangle] -pub unsafe extern "C" fn free_rust_string(string: *const c_char) { - let _ = CString::from_raw(string as *mut _); -} +mod ffi;