phlow_ffi/
phlow_object.rs

1use phlow::{PhlowObject, PhlowView, PhlowViewMethod};
2use string_box::StringBox;
3use value_box::{BoxerError, ReturnBoxerResult, ValueBox, ValueBoxIntoRaw, ValueBoxPointer};
4
5#[no_mangle]
6pub extern "C" fn phlow_object_get_view_methods(
7    phlow_object: *mut ValueBox<PhlowObject>,
8) -> *mut ValueBox<Vec<PhlowViewMethod>> {
9    phlow_object
10        .with_ref_ok(|phlow_object| ValueBox::new(phlow_object.phlow_view_methods()))
11        .into_raw()
12}
13
14#[no_mangle]
15pub extern "C" fn phlow_object_get_views(
16    phlow_object: *mut ValueBox<PhlowObject>,
17) -> *mut ValueBox<Vec<Box<dyn PhlowView>>> {
18    phlow_object
19        .with_ref_ok(|phlow_object| ValueBox::new(phlow_object.phlow_views()))
20        .into_raw()
21}
22
23#[no_mangle]
24pub extern "C" fn phlow_object_get_view_named(
25    phlow_object: *mut ValueBox<PhlowObject>,
26    view_name: *mut ValueBox<StringBox>,
27) -> *mut ValueBox<Box<dyn PhlowView>> {
28    phlow_object
29        .with_ref(|phlow_object| {
30            view_name.with_ref(|view_name| {
31                phlow_object
32                    .phlow_view_named(view_name.as_str())
33                    .map(|view| ValueBox::new(view))
34                    .ok_or_else(|| {
35                        BoxerError::AnyError(
36                            format!("View named {} does not exist", view_name.as_str()).into(),
37                        )
38                    })
39            })
40        })
41        .into_raw()
42}
43
44#[no_mangle]
45pub extern "C" fn phlow_object_to_string(
46    phlow_object: *mut ValueBox<PhlowObject>,
47    string: *mut ValueBox<StringBox>,
48) {
49    phlow_object
50        .with_ref(|phlow_object| {
51            string.with_mut_ok(|string| string.set_string(phlow_object.to_string()))
52        })
53        .log();
54}
55
56#[no_mangle]
57pub extern "C" fn phlow_object_get_value_type(
58    phlow_object: *mut ValueBox<PhlowObject>,
59    value_type: *mut ValueBox<StringBox>,
60) {
61    phlow_object
62        .with_ref(|phlow_object| {
63            value_type.with_mut_ok(|value_type| {
64                value_type.set_string(phlow_object.value_type_name().to_string())
65            })
66        })
67        .log();
68}
69
70#[no_mangle]
71pub extern "C" fn phlow_object_drop(phlow_object: *mut ValueBox<PhlowObject>) {
72    phlow_object.release();
73}
74
75#[no_mangle]
76pub extern "C" fn phlow_object_vec_len(any_vec: *mut ValueBox<Vec<PhlowObject>>) -> usize {
77    any_vec.with_ref_ok(|any_vec| any_vec.len()).or_log(0)
78}
79
80#[no_mangle]
81pub extern "C" fn phlow_object_vec_drop(any_vec: *mut ValueBox<Vec<PhlowObject>>) {
82    any_vec.release();
83}