1use std::any::Any;
2use string_box::StringBox;
3use value_box::{Result, ReturnBoxerResult, ValueBox, ValueBoxPointer};
4
5use phlow::{downcast_view_ref, PhlowView};
6
7pub fn with_view<T: PhlowView, R: Any>(
8 phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
9 op: impl FnOnce(&T) -> Result<R>,
10) -> Result<R> {
11 phlow_view.with_ref(|phlow_view| {
12 downcast_view_ref::<T>(&phlow_view)
13 .map_err(|error| error.into())
14 .and_then(|view| op(view))
15 })
16}
17
18#[no_mangle]
19pub extern "C" fn phlow_view_get_type(
20 phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
21 view_type: *mut ValueBox<StringBox>,
22) {
23 phlow_view
24 .with_ref(|phlow_view| {
25 view_type.with_mut_ok(|view_type| {
26 view_type.set_string(phlow_view.get_view_type().to_string())
27 })
28 })
29 .log();
30}
31
32#[no_mangle]
33pub extern "C" fn phlow_view_get_title(
34 phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
35 view_title: *mut ValueBox<StringBox>,
36) {
37 phlow_view
38 .with_ref(|phlow_view| {
39 view_title
40 .with_mut_ok(|view_title| view_title.set_string(phlow_view.get_title().to_string()))
41 })
42 .log();
43}
44
45#[no_mangle]
46pub extern "C" fn phlow_view_get_priority(phlow_view: *mut ValueBox<Box<dyn PhlowView>>) -> usize {
47 phlow_view
48 .with_ref_ok(|phlow_view| phlow_view.get_priority())
49 .or_log(0)
50}
51
52#[no_mangle]
53pub extern "C" fn phlow_view_get_source_code(
54 phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
55 source_code: *mut ValueBox<StringBox>,
56) {
57 phlow_view
58 .with_ref(|phlow_view| {
59 source_code.with_mut_ok(|source_code| {
60 source_code.set_string(phlow_view.get_defining_method().source_code().to_string())
61 })
62 })
63 .log();
64}
65
66#[no_mangle]
67pub extern "C" fn phlow_view_drop(phlow_view: *mut ValueBox<Box<dyn PhlowView>>) {
68 phlow_view.release();
69}
70
71#[no_mangle]
72pub extern "C" fn phlow_views_pop(
73 phlow_views: *mut ValueBox<Vec<Box<dyn PhlowView>>>,
74) -> *mut ValueBox<Box<dyn PhlowView>> {
75 phlow_views
76 .with_mut_ok(|phlow_views| phlow_views.pop())
77 .map(|view| view.map_or(std::ptr::null_mut(), |view| ValueBox::new(view).into_raw()))
78 .or_log(std::ptr::null_mut())
79}
80
81#[no_mangle]
82pub extern "C" fn phlow_views_drop(phlow_views: *mut ValueBox<Vec<Box<dyn PhlowView>>>) {
83 phlow_views.release();
84}