Skip to content

Commit 3153ac1

Browse files
committed
feat: better ffi error impl
1 parent 907346f commit 3153ac1

File tree

2 files changed

+91
-40
lines changed

2 files changed

+91
-40
lines changed

float-pigment-css/src/ffi.rs

Lines changed: 90 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ use sheet::borrow::de_static_ref_zero_copy_env;
3434

3535
#[macro_export]
3636
macro_rules! check_null {
37-
(($($arg:expr),+ $(,)?), $default: expr) => {
38-
if $( $arg.is_null() )||+ {
39-
return FfiResult::error(FfiErrorCode::NullPointer, $default);
37+
($arg:expr, $error:expr, $default:expr) => {
38+
if $arg.is_null() {
39+
return FfiResult::error($error, $default);
4040
}
4141
};
4242
}
@@ -55,8 +55,23 @@ pub type NullPtr = *const ();
5555
#[repr(C)]
5656
pub enum FfiErrorCode {
5757
None,
58-
NullPointer,
58+
ThisNullPointer,
59+
PathNullPointer,
60+
PrefixNullPointer,
61+
SourceNullPointer,
62+
BufferNullPointer,
63+
ExprPtrNullPointer,
64+
StrNullPointer,
65+
InlineStyleTextNullPointer,
66+
InlineRuleNullPointer,
67+
StyleTextNullPointer,
68+
SelectorTextNullPointer,
5969
InvalidPath,
70+
JsonNullPointer,
71+
ArrayNullPointer,
72+
SelectorNullPointer,
73+
StyleSheetNullPointer,
74+
MapNullPointer,
6075
Unknown,
6176
}
6277

@@ -92,7 +107,7 @@ pub unsafe extern "C" fn style_sheet_resource_new() -> FfiResult<RawMutPtr> {
92107
/// Free the resource manager.
93108
#[export_name = "FPStyleSheetResourceFree"]
94109
pub unsafe extern "C" fn style_sheet_resource_free(this: RawMutPtr) -> FfiResult<NullPtr> {
95-
check_null!((this), null());
110+
check_null!(this, FfiErrorCode::ThisNullPointer, null());
96111
drop(Box::from_raw(this as *mut group::StyleSheetResource));
97112
FfiResult::ok(null())
98113
}
@@ -105,7 +120,9 @@ pub unsafe extern "C" fn style_sheet_resource_add_tag_name_prefix(
105120
path: *const c_char,
106121
prefix: *const c_char,
107122
) -> FfiResult<NullPtr> {
108-
check_null!((this, path, prefix), null());
123+
check_null!(this, FfiErrorCode::ThisNullPointer, null());
124+
check_null!(path, FfiErrorCode::PathNullPointer, null());
125+
check_null!(prefix, FfiErrorCode::PrefixNullPointer, null());
109126
let res = raw_ptr_as_mut_ref!(this, group::StyleSheetResource);
110127
let path = CStr::from_ptr(path).to_string_lossy();
111128
let prefix = CStr::from_ptr(prefix).to_string_lossy();
@@ -122,7 +139,8 @@ pub unsafe extern "C" fn style_sheet_resource_serialize_json(
122139
path: *const c_char,
123140
ret_buffer_len: &mut usize,
124141
) -> FfiResult<*mut u8> {
125-
check_null!((this, path), null_mut());
142+
check_null!(this, FfiErrorCode::ThisNullPointer, null_mut());
143+
check_null!(path, FfiErrorCode::PathNullPointer, null_mut());
126144
let res = raw_ptr_as_mut_ref!(this, group::StyleSheetResource);
127145
let path = CStr::from_ptr(path).to_string_lossy();
128146
let serial = res.serialize_json(&path).unwrap_or_default();
@@ -140,7 +158,8 @@ pub unsafe extern "C" fn style_sheet_resource_serialize_bincode(
140158
path: *const c_char,
141159
ret_buffer_len: &mut usize,
142160
) -> FfiResult<*mut u8> {
143-
check_null!((this, path), null_mut());
161+
check_null!(this, FfiErrorCode::ThisNullPointer, null_mut());
162+
check_null!(path, FfiErrorCode::PathNullPointer, null_mut());
144163
let res = raw_ptr_as_mut_ref!(this, group::StyleSheetResource);
145164
let path = CStr::from_ptr(path).to_string_lossy();
146165
let serial = res.serialize_bincode(&path).unwrap_or_default();
@@ -157,7 +176,9 @@ pub unsafe extern "C" fn style_sheet_resource_add_source(
157176
source: *const c_char,
158177
warnings: *mut *mut Array<Warning>,
159178
) -> FfiResult<NullPtr> {
160-
check_null!((this, path, source), null());
179+
check_null!(this, FfiErrorCode::ThisNullPointer, null());
180+
check_null!(path, FfiErrorCode::PathNullPointer, null());
181+
check_null!(source, FfiErrorCode::SourceNullPointer, null());
161182
let res = raw_ptr_as_mut_ref!(this, group::StyleSheetResource);
162183
let path = CStr::from_ptr(path).to_string_lossy();
163184
let source = CStr::from_ptr(source).to_string_lossy();
@@ -178,7 +199,9 @@ pub unsafe extern "C" fn style_sheet_resource_add_source_with_hooks(
178199
source: *const c_char,
179200
warnings: *mut *mut Array<Warning>,
180201
) -> FfiResult<NullPtr> {
181-
check_null!((this, path, source), null());
202+
check_null!(this, FfiErrorCode::ThisNullPointer, null());
203+
check_null!(path, FfiErrorCode::PathNullPointer, null());
204+
check_null!(source, FfiErrorCode::SourceNullPointer, null());
182205
let res = raw_ptr_as_mut_ref!(this, group::StyleSheetResource);
183206
let path = CStr::from_ptr(path).to_string_lossy();
184207
let source = CStr::from_ptr(source).to_string_lossy();
@@ -202,7 +225,9 @@ pub unsafe extern "C" fn style_sheet_resource_add_bincode(
202225
drop_args: RawMutPtr,
203226
warnings: *mut *mut Array<Warning>,
204227
) -> FfiResult<NullPtr> {
205-
check_null!((this, path, buffer_ptr), null());
228+
check_null!(this, FfiErrorCode::ThisNullPointer, null());
229+
check_null!(path, FfiErrorCode::PathNullPointer, null());
230+
check_null!(buffer_ptr, FfiErrorCode::BufferNullPointer, null());
206231
let res = raw_ptr_as_mut_ref!(this, group::StyleSheetResource);
207232
let bincode: *mut [u8] = core::slice::from_raw_parts_mut(buffer_ptr, buffer_len);
208233
let path = CStr::from_ptr(path).to_string_lossy();
@@ -224,7 +249,8 @@ pub unsafe extern "C" fn style_sheet_resource_direct_dependencies(
224249
this: RawMutPtr,
225250
path: *const c_char,
226251
) -> FfiResult<*mut Array<StrRef>> {
227-
check_null!((this, path), null_mut());
252+
check_null!(this, FfiErrorCode::ThisNullPointer, null_mut());
253+
check_null!(path, FfiErrorCode::PathNullPointer, null_mut());
228254
let res = raw_ptr_as_mut_ref!(this, group::StyleSheetResource);
229255
let path = CStr::from_ptr(path).to_string_lossy();
230256
let deps = res.direct_dependencies(&path);
@@ -238,7 +264,7 @@ pub unsafe extern "C" fn style_sheet_resource_direct_dependencies(
238264
pub unsafe extern "C" fn style_sheet_resource_generate_import_index(
239265
this: RawMutPtr,
240266
) -> FfiResult<RawMutPtr> {
241-
check_null!((this), null_mut());
267+
check_null!(this, FfiErrorCode::ThisNullPointer, null_mut());
242268
let res = raw_ptr_as_mut_ref!(this, group::StyleSheetResource);
243269
FfiResult::ok(
244270
StyleSheetImportIndex {
@@ -279,7 +305,7 @@ pub unsafe extern "C" fn style_sheet_import_index_new() -> FfiResult<RawMutPtr>
279305
/// Free the style sheet import index.
280306
#[export_name = "FPStyleSheetImportIndexFree"]
281307
pub unsafe extern "C" fn style_sheet_import_index_free(this: RawMutPtr) -> FfiResult<NullPtr> {
282-
check_null!((this), null());
308+
check_null!(this, FfiErrorCode::ThisNullPointer, null());
283309
drop(Box::from_raw(this as *mut StyleSheetImportIndex));
284310
FfiResult::ok(null())
285311
}
@@ -291,7 +317,8 @@ pub unsafe extern "C" fn style_sheet_import_index_query_and_mark_dependencies(
291317
this: RawMutPtr,
292318
path: *const c_char,
293319
) -> FfiResult<*mut Array<StrRef>> {
294-
check_null!((this, path), null_mut());
320+
check_null!(this, FfiErrorCode::ThisNullPointer, null_mut());
321+
check_null!(path, FfiErrorCode::PathNullPointer, null_mut());
295322
let style_sheet_import_index = raw_ptr_as_mut_ref!(this, StyleSheetImportIndex);
296323
let path = CStr::from_ptr(path).to_string_lossy();
297324
let deps = style_sheet_import_index
@@ -308,7 +335,8 @@ pub unsafe extern "C" fn style_sheet_import_index_list_dependencies(
308335
this: RawMutPtr,
309336
path: *const c_char,
310337
) -> FfiResult<*mut Array<StrRef>> {
311-
check_null!((this, path), null_mut());
338+
check_null!(this, FfiErrorCode::ThisNullPointer, null_mut());
339+
check_null!(path, FfiErrorCode::PathNullPointer, null_mut());
312340
let style_sheet_import_index = raw_ptr_as_mut_ref!(this, StyleSheetImportIndex);
313341
let path = CStr::from_ptr(path).to_string_lossy();
314342
let deps = style_sheet_import_index
@@ -325,7 +353,8 @@ pub unsafe extern "C" fn style_sheet_import_index_list_dependency(
325353
this: RawMutPtr,
326354
path: *const c_char,
327355
) -> FfiResult<*mut Array<StrRef>> {
328-
check_null!((this, path), null_mut());
356+
check_null!(this, FfiErrorCode::ThisNullPointer, null_mut());
357+
check_null!(path, FfiErrorCode::PathNullPointer, null_mut());
329358
let style_sheet_import_index = raw_ptr_as_mut_ref!(this, StyleSheetImportIndex);
330359
let path = CStr::from_ptr(path).to_string_lossy();
331360
let deps = style_sheet_import_index
@@ -349,7 +378,9 @@ pub unsafe extern "C" fn style_sheet_import_index_add_bincode(
349378
) -> FfiResult<NullPtr> {
350379
use float_pigment_consistent_bincode::Options;
351380
use parser::WarningKind;
352-
check_null!((this, path, buffer_ptr), null());
381+
check_null!(this, FfiErrorCode::ThisNullPointer, null_mut());
382+
check_null!(path, FfiErrorCode::PathNullPointer, null_mut());
383+
check_null!(buffer_ptr, FfiErrorCode::BufferNullPointer, null_mut());
353384
let path = CStr::from_ptr(path).to_string_lossy();
354385
let sheet = de_static_ref_zero_copy_env(
355386
core::slice::from_raw_parts_mut(buffer_ptr, buffer_len),
@@ -398,7 +429,8 @@ pub unsafe extern "C" fn style_sheet_import_index_remove_bincode(
398429
this: RawMutPtr,
399430
path: *const c_char,
400431
) -> FfiResult<NullPtr> {
401-
check_null!((this, path), null());
432+
check_null!(this, FfiErrorCode::ThisNullPointer, null());
433+
check_null!(path, FfiErrorCode::PathNullPointer, null());
402434
let path = CStr::from_ptr(path).to_string_lossy();
403435
let path = drop_css_extension(&path);
404436
let style_sheet_import_index = raw_ptr_as_mut_ref!(this, StyleSheetImportIndex);
@@ -412,7 +444,8 @@ pub unsafe extern "C" fn style_sheet_import_index_get_style_sheet(
412444
this: RawMutPtr,
413445
path: *const c_char,
414446
) -> FfiResult<*mut StyleSheet> {
415-
check_null!((this, path), null_mut());
447+
check_null!(this, FfiErrorCode::ThisNullPointer, null_mut());
448+
check_null!(path, FfiErrorCode::PathNullPointer, null_mut());
416449
let path = CStr::from_ptr(path).to_string_lossy();
417450
let path = drop_css_extension(&path);
418451
let style_sheet_import_index = raw_ptr_as_mut_ref!(this, StyleSheetImportIndex);
@@ -429,7 +462,7 @@ pub unsafe extern "C" fn style_sheet_import_index_serialize_json(
429462
this: RawMutPtr,
430463
ret_buffer_len: &mut usize,
431464
) -> FfiResult<*mut u8> {
432-
check_null!((this), null_mut());
465+
check_null!(this, FfiErrorCode::ThisNullPointer, null_mut());
433466
let style_sheet_import_index = raw_ptr_as_mut_ref!(this, StyleSheetImportIndex);
434467
let serial = style_sheet_import_index.inner.serialize_json();
435468
*ret_buffer_len = serial.len();
@@ -444,7 +477,7 @@ pub unsafe extern "C" fn style_sheet_import_index_serialize_bincode(
444477
this: RawMutPtr,
445478
ret_buffer_len: &mut usize,
446479
) -> FfiResult<*mut u8> {
447-
check_null!((this), null_mut());
480+
check_null!(this, FfiErrorCode::ThisNullPointer, null_mut());
448481
let style_sheet_import_index = raw_ptr_as_mut_ref!(this, StyleSheetImportIndex);
449482
let serial = style_sheet_import_index.inner.serialize_bincode();
450483
*ret_buffer_len = serial.len();
@@ -458,7 +491,7 @@ pub unsafe extern "C" fn style_sheet_import_index_serialize_bincode(
458491
pub unsafe extern "C" fn style_sheet_import_index_deserialize_json(
459492
json: *const c_char,
460493
) -> FfiResult<RawMutPtr> {
461-
check_null!((json), null_mut());
494+
check_null!(json, FfiErrorCode::JsonNullPointer, null_mut());
462495
let json = CStr::from_ptr(json).to_string_lossy();
463496
FfiResult::ok(
464497
StyleSheetImportIndex {
@@ -478,7 +511,7 @@ pub unsafe extern "C" fn style_sheet_import_index_deserialize_bincode(
478511
drop_fn: Option<unsafe extern "C" fn(RawMutPtr)>,
479512
drop_args: RawMutPtr,
480513
) -> FfiResult<RawMutPtr> {
481-
check_null!((buffer_ptr), null_mut());
514+
check_null!(buffer_ptr, FfiErrorCode::BufferNullPointer, null_mut());
482515
let bincode: *mut [u8] = core::slice::from_raw_parts_mut(buffer_ptr, buffer_len);
483516
FfiResult::ok(
484517
StyleSheetImportIndex {
@@ -503,7 +536,8 @@ pub unsafe extern "C" fn style_sheet_import_index_merge_bincode(
503536
drop_fn: Option<unsafe extern "C" fn(*mut ())>,
504537
drop_args: *mut (),
505538
) -> FfiResult<NullPtr> {
506-
check_null!((this, buffer_ptr), null());
539+
check_null!(this, FfiErrorCode::ThisNullPointer, null());
540+
check_null!(buffer_ptr, FfiErrorCode::BufferNullPointer, null());
507541
let style_sheet_import_index = raw_ptr_as_mut_ref!(this, StyleSheetImportIndex);
508542
let bincode: *mut [u8] = core::slice::from_raw_parts_mut(buffer_ptr, buffer_len);
509543
style_sheet_import_index
@@ -520,7 +554,7 @@ pub unsafe extern "C" fn style_sheet_import_index_merge_bincode(
520554
/// Free the buffer.
521555
#[export_name = "FPBufferFree"]
522556
pub unsafe extern "C" fn buffer_free(buffer_ptr: *mut u8, buffer_len: usize) -> FfiResult<NullPtr> {
523-
check_null!((buffer_ptr), null());
557+
check_null!(buffer_ptr, FfiErrorCode::BufferNullPointer, null());
524558
let x: *mut [u8] = core::slice::from_raw_parts_mut(buffer_ptr, buffer_len);
525559
drop(Box::from_raw(x));
526560
FfiResult::ok(null())
@@ -530,7 +564,7 @@ pub unsafe extern "C" fn buffer_free(buffer_ptr: *mut u8, buffer_len: usize) ->
530564
/// Free the array of string references.
531565
#[export_name = "FPArrayStrRefFree"]
532566
pub unsafe extern "C" fn array_str_ref_free(x: *mut Array<StrRef>) -> FfiResult<NullPtr> {
533-
check_null!((x), null());
567+
check_null!(x, FfiErrorCode::ArrayNullPointer, null());
534568
drop(Box::from_raw(x));
535569
FfiResult::ok(null())
536570
}
@@ -541,7 +575,7 @@ pub unsafe extern "C" fn array_str_ref_free(x: *mut Array<StrRef>) -> FfiResult<
541575
pub unsafe extern "C" fn array_warning_free(
542576
warnings: *mut Array<parser::Warning>,
543577
) -> FfiResult<NullPtr> {
544-
check_null!((warnings), null());
578+
check_null!(warnings, FfiErrorCode::ArrayNullPointer, null());
545579
drop(Box::from_raw(warnings));
546580
FfiResult::ok(null())
547581
}
@@ -553,7 +587,11 @@ pub unsafe extern "C" fn parse_inline_style(
553587
inline_style_text_ptr: *const c_char,
554588
warnings: *mut *mut Array<parser::Warning>,
555589
) -> FfiResult<*mut InlineRule> {
556-
check_null!((inline_style_text_ptr), null_mut());
590+
check_null!(
591+
inline_style_text_ptr,
592+
FfiErrorCode::InlineStyleTextNullPointer,
593+
null_mut()
594+
);
557595
let inline_style_text = CStr::from_ptr(inline_style_text_ptr).to_string_lossy();
558596
let (prop, w) =
559597
parser::parse_inline_style(&inline_style_text, parser::StyleParsingDebugMode::None);
@@ -588,7 +626,7 @@ pub unsafe extern "C" fn parse_inline_style(
588626
/// Free the inline style.
589627
#[export_name = "FPInlineStyleFree"]
590628
pub unsafe extern "C" fn inline_style_free(inline_rule: *mut InlineRule) -> FfiResult<NullPtr> {
591-
check_null!((inline_rule), null());
629+
check_null!(inline_rule, FfiErrorCode::InlineRuleNullPointer, null());
592630
drop(Box::from_raw(inline_rule));
593631
FfiResult::ok(null())
594632
}
@@ -599,7 +637,11 @@ pub unsafe extern "C" fn inline_style_free(inline_rule: *mut InlineRule) -> FfiR
599637
pub unsafe extern "C" fn parse_style_sheet_from_string(
600638
style_text_ptr: *const c_char,
601639
) -> FfiResult<*mut StyleSheet> {
602-
check_null!((style_text_ptr), null_mut());
640+
check_null!(
641+
style_text_ptr,
642+
FfiErrorCode::StyleTextNullPointer,
643+
null_mut()
644+
);
603645
let style_text = CStr::from_ptr(style_text_ptr).to_string_lossy();
604646
let (compiled_style_sheet, _) = parser::parse_style_sheet("string", &style_text);
605647
let style_sheet = StyleSheet::from_sheet(&compiled_style_sheet);
@@ -612,7 +654,11 @@ pub unsafe extern "C" fn parse_style_sheet_from_string(
612654
pub unsafe extern "C" fn parse_selector_from_string(
613655
selector_text_ptr: *const c_char,
614656
) -> FfiResult<*mut Selector> {
615-
check_null!((selector_text_ptr), null_mut());
657+
check_null!(
658+
selector_text_ptr,
659+
FfiErrorCode::SelectorTextNullPointer,
660+
null_mut()
661+
);
616662
let selector_text = CStr::from_ptr(selector_text_ptr).to_string_lossy();
617663
let selector = Selector::from_string(&selector_text);
618664
FfiResult::ok(Box::into_raw(Box::new(selector)))
@@ -622,7 +668,7 @@ pub unsafe extern "C" fn parse_selector_from_string(
622668
/// Free the selector.
623669
#[export_name = "FPSelectorFree"]
624670
pub unsafe extern "C" fn selector_free(selector: *mut Selector) -> FfiResult<NullPtr> {
625-
check_null!((selector), null());
671+
check_null!(selector, FfiErrorCode::SelectorNullPointer, null());
626672
drop(Box::from_raw(selector));
627673
FfiResult::ok(null())
628674
}
@@ -631,7 +677,7 @@ pub unsafe extern "C" fn selector_free(selector: *mut Selector) -> FfiResult<Nul
631677
/// Free the style sheet.
632678
#[export_name = "FPStyleSheetFree"]
633679
pub unsafe extern "C" fn style_sheet_free(style_sheet: *mut StyleSheet) -> FfiResult<NullPtr> {
634-
check_null!((style_sheet), null());
680+
check_null!(style_sheet, FfiErrorCode::StyleSheetNullPointer, null());
635681
drop(Box::from_raw(style_sheet));
636682
FfiResult::ok(null())
637683
}
@@ -645,7 +691,7 @@ pub unsafe extern "C" fn style_sheet_bincode_version(
645691
buffer_len: usize,
646692
) -> FfiResult<*mut StrRef> {
647693
use float_pigment_consistent_bincode::Options;
648-
check_null!((buffer_ptr), null_mut());
694+
check_null!(buffer_ptr, FfiErrorCode::BufferNullPointer, null_mut());
649695
let sheet = de_static_ref_zero_copy_env(
650696
core::slice::from_raw_parts_mut(buffer_ptr, buffer_len),
651697
|s| {
@@ -699,7 +745,11 @@ pub struct ColorValue {
699745
/// Parse the color from the string.
700746
#[export_name = "FPParseColorFromString"]
701747
pub unsafe extern "C" fn parse_color_from_string(source: *const c_char) -> FfiResult<ColorValue> {
702-
check_null!((source), ColorValue::default());
748+
check_null!(
749+
source,
750+
FfiErrorCode::SourceNullPointer,
751+
ColorValue::default()
752+
);
703753
let source = CStr::from_ptr(source).to_string_lossy();
704754
let ret = parse_color_to_rgba(&source);
705755
FfiResult::ok(ColorValue {
@@ -719,7 +769,8 @@ pub unsafe extern "C" fn substitute_variable(
719769
getter: CustomPropertyGetter,
720770
setter: CustomPropertySetter,
721771
) -> FfiResult<*const c_char> {
722-
check_null!((expr_ptr, map), null());
772+
check_null!(expr_ptr, FfiErrorCode::ExprPtrNullPointer, null());
773+
check_null!(map, FfiErrorCode::MapNullPointer, null());
723774
let expr = CStr::from_ptr(expr_ptr).to_string_lossy();
724775
let context = CustomPropertyContext::create(map, getter, setter);
725776
if let Some(ret) = parser::property_value::var::substitute_variable(&expr, &context) {
@@ -734,7 +785,7 @@ pub unsafe extern "C" fn substitute_variable(
734785
/// Free the string.
735786
#[export_name = "FPStrFree"]
736787
pub unsafe extern "C" fn str_free(ptr: *const c_char) -> FfiResult<NullPtr> {
737-
check_null!((ptr), null());
738-
drop(CString::from_raw(ptr as *mut _));
788+
check_null!(ptr, FfiErrorCode::StrNullPointer, null());
789+
drop(CString::from_raw(ptr as *mut c_char));
739790
FfiResult::ok(null())
740791
}

float-pigment-css/src/parser/hooks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl CParserHooksContext {
6565
use crate::check_null;
6666
use crate::ffi::FfiErrorCode;
6767
use core::ptr::null;
68-
check_null!((message), null());
68+
check_null!(message, FfiErrorCode::StrNullPointer, null());
6969
let message = CStr::from_ptr(message).to_string_lossy();
7070
let ctx = &mut *(self.inner as *mut ParserHooksContext);
7171
ctx.generate_warning(&message);

0 commit comments

Comments
 (0)