Skip to content

Commit 31f34a9

Browse files
committed
Add C FFI for Document::parent
1 parent ca48f8c commit 31f34a9

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

crates/core/c_src/include/LiveViewNativeCore.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ typedef struct __Node {
4545
__NodeData data;
4646
} __Node;
4747

48+
typedef struct __OptionNodeRef {
49+
bool is_some;
50+
NodeRef some_value;
51+
} __OptionNodeRef;
52+
4853
extern void __liveview_native_core$AttributeVec$drop(_AttributeVec vec);
4954

5055
extern _RustString __liveview_native_core$Document$to_string(__Document doc);
@@ -75,3 +80,5 @@ extern _RustSlice __liveview_native_core$Document$children(__Document doc,
7580

7681
extern _AttributeVec __liveview_native_core$Document$attributes(__Document doc,
7782
NodeRef node);
83+
84+
extern __OptionNodeRef __liveview_native_core$Document$get_parent(__Document doc, NodeRef node);

crates/core/src/ffi/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ pub union NodeData<'a> {
6262
pub leaf: RustStr<'a>,
6363
}
6464

65+
#[repr(C)]
66+
pub struct OptionNodeRef {
67+
pub is_some: bool,
68+
pub some_value: NodeRef,
69+
}
70+
6571
#[repr(C)]
6672
#[derive(Copy, Clone)]
6773
pub struct Element<'a> {
@@ -208,3 +214,18 @@ pub extern "C" fn document_get_attributes(
208214
}
209215
AttributeVec::from_vec(result)
210216
}
217+
218+
#[export_name = "__liveview_native_core$Document$get_parent"]
219+
pub extern "C" fn document_get_parent(doc: *const dom::Document, node: NodeRef) -> OptionNodeRef {
220+
let doc = unsafe { &*doc };
221+
match doc.parent(node) {
222+
Some(parent) => OptionNodeRef {
223+
is_some: true,
224+
some_value: parent,
225+
},
226+
None => OptionNodeRef {
227+
is_some: false,
228+
some_value: Default::default(),
229+
},
230+
}
231+
}

0 commit comments

Comments
 (0)