ladybird/Libraries/LibWeb/Rust/src/lib.rs
Vanand Gasparyan f3a3488cda Rust: Set import granularity to Item
By default, `rustfmt` persists the import granularity. In practice, most
Rust code has import granularity "Module" due to LSP's actions.

"Item" gets rid of import groupings and achieves cleaner diffs and
better conflict resolution. Better greppability is a positive side
effect.

Note: it's an unstable rustfmt feature. `cargo +nightly fmt` must be
used instead of `cargo fmt`.
2026-05-28 06:52:18 +02:00

77 lines
2.2 KiB
Rust

/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#[path = "../../../RustAllocator.rs"]
mod rust_allocator;
mod css_tokenizer;
mod encoding_detection;
pub use libweb_html_tokenizer as html_tokenizer;
use std::ffi::c_void;
use std::panic::AssertUnwindSafe;
use std::panic::catch_unwind;
pub use css_tokenizer::CssHashType;
pub use css_tokenizer::CssNumberType;
pub use css_tokenizer::CssToken;
pub use css_tokenizer::CssTokenType;
fn abort_on_panic<F: FnOnce() -> R, R>(f: F) -> R {
match catch_unwind(AssertUnwindSafe(f)) {
Ok(result) => result,
Err(payload) => {
let message = if let Some(message) = payload.downcast_ref::<&str>() {
(*message).to_string()
} else if let Some(message) = payload.downcast_ref::<String>() {
message.clone()
} else {
"unknown panic".to_string()
};
eprintln!("Rust panic at FFI boundary: {message}");
std::process::abort();
}
}
}
unsafe fn bytes_from_raw<'a>(bytes: *const u8, len: usize) -> Option<&'a [u8]> {
unsafe {
if len == 0 {
return Some(&[]);
}
if bytes.is_null() {
eprintln!("bytes_from_raw: null pointer with non-zero length {len}");
return None;
}
Some(std::slice::from_raw_parts(bytes, len))
}
}
/// # Safety
/// - `input` and `input_len` must point to a valid string
/// - `ctx` must be a valid pointer to a CallbackContext
/// - Parameters provided to `callback` must be valid pointers
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_css_tokenize(
input: *const u8,
input_len: usize,
ctx: *mut c_void,
callback: unsafe extern "C" fn(ctx: *mut c_void, token: *const CssToken),
) {
unsafe {
abort_on_panic(|| {
let Some(input) = bytes_from_raw(input, input_len) else {
return;
};
css_tokenizer::tokenize(input, |token, filtered_input| {
let ffi_token = token.as_ffi(filtered_input);
callback(ctx, &raw const ffi_token);
});
});
}
}