Stranger6667 / css-inline

High-performance library for inlining CSS into HTML 'style' attributes
https://css-inline.org/
MIT License
242 stars 29 forks source link

C bindings #209

Closed Stranger6667 closed 1 year ago

Stranger6667 commented 1 year ago

We need C bindings to extend css-inline accessibility to other languages that can work via C.

TODO:

Notes:

It can roughly look like this:

use libc::{c_char, c_int, size_t};
use std::ffi::CStr;
use std::ptr;

#[no_mangle]
pub extern "C" fn inline_to(input: *const c_char, output: *mut c_char, output_size: size_t) -> c_int {
    let cstr = unsafe { CStr::from_ptr(input) }.to_str().expect("TODO: handle properly");
    // ... Inline impl via `CSSInliner::inline_to`
}

// `Write` impl for a buffer coming from the C side?
use std::io::{self, Write};

struct CBuffer {
    buffer: *mut c_char,
    buffer_size: size_t,
    pos: usize,
}

impl CBuffer {
    fn new(buffer: *mut c_char, buffer_size: size_t) -> Self {
        Self {
            buffer,
            buffer_size,
            pos: 0,
        }
    }
}

impl Write for CBuffer {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        ...
    }

    fn flush(&mut self) -> io::Result<()> {
        ...
    }
}

cc @lobziik