2020-07-24 00:20:24 -04:00
|
|
|
use std::{mem, ptr, slice};
|
2020-07-22 07:11:16 -04:00
|
|
|
use minify_html::{Cfg, Error, in_place};
|
2019-12-30 02:16:33 -05:00
|
|
|
|
2020-07-24 00:20:24 -04:00
|
|
|
#[no_mangle]
|
2020-07-24 02:03:47 -04:00
|
|
|
pub extern "C" fn ffi_create_cfg(minify_js: bool) -> *const Cfg {
|
2020-07-24 00:20:24 -04:00
|
|
|
Box::into_raw(Box::new(Cfg {
|
|
|
|
minify_js,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2020-07-24 02:03:47 -04:00
|
|
|
pub extern "C" fn ffi_drop_cfg(cfg: *const Cfg) -> () {
|
2020-07-24 00:20:24 -04:00
|
|
|
unsafe {
|
2020-07-24 02:03:47 -04:00
|
|
|
Box::from_raw(cfg as *mut Cfg);
|
2020-07-10 11:15:56 -04:00
|
|
|
};
|
2019-12-30 02:16:33 -05:00
|
|
|
}
|
|
|
|
|
2020-07-24 00:20:24 -04:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct ffi_error {
|
|
|
|
message: *mut u8,
|
|
|
|
message_len: usize,
|
|
|
|
position: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2020-07-24 02:03:47 -04:00
|
|
|
pub extern "C" fn ffi_drop_ffi_error(ffi_error_ptr: *const ffi_error) -> () {
|
2020-07-24 00:20:24 -04:00
|
|
|
unsafe {
|
2020-07-24 02:03:47 -04:00
|
|
|
let ffi_error = Box::from_raw(ffi_error_ptr as *mut ffi_error);
|
2020-07-24 00:20:24 -04:00
|
|
|
let _ = String::from_raw_parts(ffi_error.message, ffi_error.message_len, ffi_error.message_len);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn ffi_in_place(code: *mut u8, code_len: usize, cfg: *const Cfg, out_min_len: *mut usize) -> *const ffi_error {
|
|
|
|
let code_slice = unsafe { slice::from_raw_parts_mut(code, code_len) };
|
|
|
|
match in_place(code_slice, unsafe { &*cfg }) {
|
|
|
|
Ok(min_len) => unsafe {
|
|
|
|
*out_min_len = min_len;
|
|
|
|
ptr::null()
|
|
|
|
}
|
|
|
|
Err(Error { error_type, position }) => {
|
|
|
|
let mut msg = error_type.message();
|
|
|
|
msg.shrink_to_fit();
|
|
|
|
let msg_ptr = msg.as_mut_ptr();
|
|
|
|
let msg_len = msg.len();
|
|
|
|
mem::forget(msg);
|
|
|
|
Box::into_raw(Box::new(ffi_error {
|
|
|
|
message: msg_ptr,
|
|
|
|
message_len: msg_len,
|
|
|
|
position,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|