themed_styler/
ffi.rs

1use crate::bridge_common;
2use std::ffi::{CStr, CString, c_char};
3use std::ptr;
4
5#[unsafe(no_mangle)]
6pub extern "C" fn themed_styler_version() -> *const c_char {
7    static VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");
8    VERSION.as_ptr() as *const c_char
9}
10
11#[unsafe(no_mangle)]
12pub unsafe extern "C" fn themed_styler_render_css(
13    usage_json: *const c_char,
14    themes_json: *const c_char,
15) -> *mut c_char {
16    if usage_json.is_null() || themes_json.is_null() {
17        return ptr::null_mut();
18    }
19
20    let usage_str = match unsafe { CStr::from_ptr(usage_json).to_str() } {
21        Ok(s) => s,
22        Err(_) => return ptr::null_mut(),
23    };
24
25    let themes_str = match unsafe { CStr::from_ptr(themes_json).to_str() } {
26        Ok(s) => s,
27        Err(_) => return ptr::null_mut(),
28    };
29
30    let snapshot = bridge_common::parse_usage_json(usage_str);
31    let themes_input = bridge_common::parse_themes_json(themes_str);
32    let state = bridge_common::build_state(snapshot, themes_input);
33    let css = state.css_for_web();
34    
35    match CString::new(css) {
36        Ok(c_str) => c_str.into_raw(),
37        Err(_) => ptr::null_mut(),
38    }
39}
40
41#[unsafe(no_mangle)]
42pub unsafe extern "C" fn themed_styler_free_string(s: *mut c_char) {
43    if !s.is_null() {
44        unsafe { drop(CString::from_raw(s)) };
45    }
46}