Skip to main content

htmltoadf/
lib.rs

1mod adf_builder;
2mod adf_structure;
3mod extractor;
4mod tests;
5mod types;
6
7extern crate wasm_bindgen;
8
9pub use adf_builder::convert_html_str_to_adf_str;
10use std::{
11    ffi::{CStr, CString},
12    os::raw::c_char,
13};
14
15#[macro_use]
16extern crate lazy_static;
17
18/**
19 * If we are compiling as a C Lib (not a WASM lib)
20 * we should make sure the function adheres to the C calling convention and its name is not mangled.
21 */
22#[cfg(not(target_family = "wasm"))]
23#[allow(clippy::not_unsafe_ptr_arg_deref)]
24#[no_mangle]
25pub extern "C" fn convert(html: *const c_char) -> *mut c_char {
26    let c_str = unsafe {
27        assert!(!html.is_null());
28        CStr::from_ptr(html)
29    };
30    let html = c_str.to_str().unwrap();
31    CString::new(convert_html_str_to_adf_str(html.to_string()))
32        .unwrap()
33        .into_raw()
34}
35
36#[cfg(not(target_family = "wasm"))]
37#[allow(clippy::not_unsafe_ptr_arg_deref)]
38#[no_mangle]
39pub extern "C" fn free_str(c_str: *mut c_char) {
40    unsafe {
41        let _ = CString::from_raw(c_str);
42    }
43}
44
45/**
46 * If we are compiling for WASM we do not worry about C FFI compatibility
47*/
48#[cfg(target_family = "wasm")]
49use wasm_bindgen::prelude::*;
50
51#[wasm_bindgen]
52#[cfg(target_family = "wasm")]
53pub fn convert(html: String) -> String {
54    convert_html_str_to_adf_str(html)
55}