minify-html/nodejs/native/src/lib.rs

70 lines
1.8 KiB
Rust
Raw Normal View History

2021-08-07 05:48:07 -04:00
use minify_html::{minify, Cfg};
2021-08-07 10:33:17 -04:00
use std::{mem, slice};
2019-12-30 02:16:33 -05:00
#[no_mangle]
2021-08-07 05:48:07 -04:00
pub extern "C" fn ffi_create_cfg(
do_not_minify_doctype: bool,
ensure_spec_compliant_unquoted_attribute_values: bool,
2021-08-07 05:48:07 -04:00
keep_closing_tags: bool,
keep_comments: bool,
keep_html_and_head_opening_tags: bool,
keep_spaces_between_attributes: bool,
minify_css: bool,
minify_js: bool,
remove_bangs: bool,
remove_processing_instructions: bool,
) -> *const Cfg {
Box::into_raw(Box::new(Cfg {
do_not_minify_doctype,
ensure_spec_compliant_unquoted_attribute_values,
2021-08-07 05:48:07 -04:00
keep_closing_tags,
keep_comments,
keep_html_and_head_opening_tags,
keep_spaces_between_attributes,
2021-01-07 08:26:02 -05:00
minify_css,
2021-08-07 05:48:07 -04:00
minify_js,
remove_bangs,
remove_processing_instructions,
}))
}
#[no_mangle]
2020-07-24 02:03:47 -04:00
pub extern "C" fn ffi_drop_cfg(cfg: *const Cfg) -> () {
unsafe {
2020-07-24 02:03:47 -04:00
Box::from_raw(cfg as *mut Cfg);
};
2019-12-30 02:16:33 -05:00
}
2021-08-07 10:33:17 -04:00
#[repr(C)]
pub struct ffi_output {
data: *mut u8,
len: usize,
cap: usize,
}
#[no_mangle]
pub extern "C" fn ffi_drop_output(ptr: *const ffi_output) -> () {
unsafe {
let out = Box::from_raw(ptr as *mut ffi_output);
Vec::from_raw_parts(out.data, out.len, out.cap);
};
}
#[no_mangle]
2021-08-07 06:57:34 -04:00
// TODO Return result memory (let Node.js manage GC) instead of overwriting source.
2021-08-07 10:33:17 -04:00
pub extern "C" fn ffi_minify(
code: *const u8,
2021-08-07 05:48:07 -04:00
code_len: usize,
cfg: *const Cfg,
2021-08-07 10:33:17 -04:00
) -> *const ffi_output {
let code_slice = unsafe { slice::from_raw_parts(code, code_len) };
let mut out_code = minify(code_slice, unsafe { &*cfg });
let res = Box::into_raw(Box::new(ffi_output {
data: out_code.as_mut_ptr(),
len: out_code.len(),
cap: out_code.capacity(),
}));
mem::forget(out_code);
res
}