Skip to content

Commit

Permalink
add phlow ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
syrel committed Jan 25, 2023
1 parent 524fc1e commit aaa1295
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"phlow-derive",
"phlow-examples",
"phlow-extensions",
"phlow-ffi"
]

exclude = [ "target" ]
Expand Down
15 changes: 15 additions & 0 deletions phlow-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "phlow-ffi"
version = "1.0.0"
authors = ["feenk gmbh <contact@feenk.com>"]
repository = "https://github.com/feenkcom/phlow-rs/tree/main/phlow-ffi"
edition = "2021"
license = "MIT"
keywords = ["phlow","meta", "ffi"]
description = "Provides C-like api to the phlow crates"

[dependencies]
phlow = { version = "1" }
value-box = { version = "2", features = [ "phlow" ] }
string-box = "1"
env_logger = "0.10"
24 changes: 24 additions & 0 deletions phlow-ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![allow(non_snake_case)]
#![allow(incomplete_features)]
#![feature(specialization)]

extern crate phlow;

pub use phlow_columned_list_view::*;
pub use phlow_list_view::*;
pub use phlow_object::*;
pub use phlow_text_view::*;
pub use phlow_view::*;
pub use phlow_view_method::*;

mod phlow_columned_list_view;
mod phlow_list_view;
mod phlow_object;
mod phlow_text_view;
mod phlow_view;
mod phlow_view_method;

#[no_mangle]
pub fn phlow_init_env_logger() {
env_logger::init();
}
109 changes: 109 additions & 0 deletions phlow-ffi/src/phlow_columned_list_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use phlow::{PhlowColumnedListView, PhlowObject, PhlowView};
use string_box::StringBox;
use value_box::{BoxerError, ReturnBoxerResult, ValueBox, ValueBoxPointer};

use crate::with_view;

#[no_mangle]
pub extern "C" fn phlow_columned_list_view_compute_items(
phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
) -> *mut ValueBox<Vec<PhlowObject>> {
with_view(phlow_view, |phlow_view: &PhlowColumnedListView| {
Ok(phlow_view.compute_items())
})
.into_raw()
}

#[no_mangle]
pub extern "C" fn phlow_columned_list_get_columns_len(
phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
) -> usize {
with_view(phlow_view, |phlow_view: &PhlowColumnedListView| {
Ok(phlow_view.get_columns().len())
})
.or_log(0)
}

#[no_mangle]
pub extern "C" fn phlow_columned_list_get_column_title(
phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
column_index: usize,
column_title: *mut ValueBox<StringBox>,
) {
with_view(phlow_view, |phlow_view: &PhlowColumnedListView| {
column_title.with_mut_ok(|column_title| {
if let Some(column) = phlow_view.get_columns().get(column_index) {
column_title.set_string(column.get_title().to_string());
}
})
})
.log();
}

#[no_mangle]
pub extern "C" fn phlow_columned_list_view_compute_item_text_at(
phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
items: *mut ValueBox<Vec<PhlowObject>>,
row_index: usize,
column_index: usize,
item_text: *mut ValueBox<StringBox>,
) {
with_view(phlow_view, |phlow_view: &PhlowColumnedListView| {
items.with_ref(|items| {
items
.get(row_index)
.ok_or_else(|| {
BoxerError::AnyError(format!("Item at {} does not exist", row_index).into())
})
.and_then(|item| {
item_text.with_mut(|item_text| {
phlow_view
.get_columns()
.get(column_index)
.ok_or_else(|| {
BoxerError::AnyError(
format!("Column at {} does not exist", column_index).into(),
)
})
.and_then(|column| {
column
.compute_cell_item(item)
.ok_or_else(|| {
BoxerError::AnyError(
format!(
"Could not compute cell item for row: {}, column: {}",
row_index,
column.get_title()
)
.into(),
)
})
.map(|cell_item| {
item_text.set_string(column.compute_cell_text(&cell_item))
})
})
})
})
})
})
.log();
}

#[no_mangle]
pub extern "C" fn phlow_columned_list_view_compute_item_send_at(
phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
items: *mut ValueBox<Vec<PhlowObject>>,
index: usize,
) -> *mut ValueBox<PhlowObject> {
with_view(phlow_view, |phlow_view: &PhlowColumnedListView| {
items.with_ref(|items| {
items
.get(index)
.ok_or_else(|| {
BoxerError::AnyError(format!("Item at {} does not exist", index).into())
})
.map(|item| phlow_view.compute_item_send(item))
})
})
.into_raw()
}
58 changes: 58 additions & 0 deletions phlow-ffi/src/phlow_list_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use phlow::{PhlowListView, PhlowObject, PhlowView};
use string_box::StringBox;
use value_box::{BoxerError, ReturnBoxerResult, ValueBox, ValueBoxPointer};

use crate::with_view;

#[no_mangle]
pub extern "C" fn phlow_list_view_compute_items(
phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
) -> *mut ValueBox<Vec<PhlowObject>> {
with_view(phlow_view, |phlow_view: &PhlowListView| {
Ok(phlow_view.compute_items())
})
.into_raw()
}

#[no_mangle]
pub extern "C" fn phlow_list_view_compute_item_text_at(
phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
items: *mut ValueBox<Vec<PhlowObject>>,
index: usize,
item_text: *mut ValueBox<StringBox>,
) {
with_view(phlow_view, |phlow_view: &PhlowListView| {
items.with_ref(|items| {
items
.get(index)
.ok_or_else(|| {
BoxerError::AnyError(format!("Item at {} does not exist", index).into())
})
.map(|item| {
item_text.with_mut_ok(|item_text| {
item_text.set_string(phlow_view.compute_item_text(item))
})
})
})
})
.log();
}

#[no_mangle]
pub extern "C" fn phlow_list_view_compute_item_send_at(
phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
items: *mut ValueBox<Vec<PhlowObject>>,
index: usize,
) -> *mut ValueBox<PhlowObject> {
with_view(phlow_view, |phlow_view: &PhlowListView| {
items.with_ref(|items| {
items
.get(index)
.ok_or_else(|| {
BoxerError::AnyError(format!("Item at {} does not exist", index).into())
})
.map(|item| phlow_view.compute_item_send(item))
})
})
.into_raw()
}
82 changes: 82 additions & 0 deletions phlow-ffi/src/phlow_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use phlow::{PhlowObject, PhlowView, PhlowViewMethod};
use string_box::StringBox;
use value_box::{BoxerError, ReturnBoxerResult, ValueBox, ValueBoxPointer};

#[no_mangle]
pub extern "C" fn phlow_object_get_view_methods(
phlow_object: *mut ValueBox<PhlowObject>,
) -> *mut ValueBox<Vec<PhlowViewMethod>> {
phlow_object
.with_ref_ok(|phlow_object| phlow_object.phlow_view_methods())
.into_raw()
}

#[no_mangle]
pub extern "C" fn phlow_object_get_views(
phlow_object: *mut ValueBox<PhlowObject>,
) -> *mut ValueBox<Vec<Box<dyn PhlowView>>> {
phlow_object
.with_ref_ok(|phlow_object| phlow_object.phlow_views())
.into_raw()
}

#[no_mangle]
pub extern "C" fn phlow_object_get_view_named(
phlow_object: *mut ValueBox<PhlowObject>,
view_name: *mut ValueBox<StringBox>,
) -> *mut ValueBox<Box<dyn PhlowView>> {
phlow_object
.with_ref(|phlow_object| {
view_name.with_ref(|view_name| {
phlow_object
.phlow_view_named(view_name.as_str())
.ok_or_else(|| {
BoxerError::AnyError(
format!("View named {} does not exist", view_name.as_str()).into(),
)
})
})
})
.into_raw()
}

#[no_mangle]
pub extern "C" fn phlow_object_to_string(
phlow_object: *mut ValueBox<PhlowObject>,
string: *mut ValueBox<StringBox>,
) {
phlow_object
.with_ref(|phlow_object| {
string.with_mut_ok(|string| string.set_string(phlow_object.to_string()))
})
.log();
}

#[no_mangle]
pub extern "C" fn phlow_object_get_value_type(
phlow_object: *mut ValueBox<PhlowObject>,
value_type: *mut ValueBox<StringBox>,
) {
phlow_object
.with_ref(|phlow_object| {
value_type.with_mut_ok(|value_type| {
value_type.set_string(phlow_object.value_type_name().to_string())
})
})
.log();
}

#[no_mangle]
pub extern "C" fn phlow_object_drop(phlow_object: *mut ValueBox<PhlowObject>) {
phlow_object.release();
}

#[no_mangle]
pub extern "C" fn phlow_object_vec_len(any_vec: *mut ValueBox<Vec<PhlowObject>>) -> usize {
any_vec.with_ref_ok(|any_vec| any_vec.len()).or_log(0)
}

#[no_mangle]
pub extern "C" fn phlow_object_vec_drop(any_vec: *mut ValueBox<Vec<PhlowObject>>) {
any_vec.release();
}
16 changes: 16 additions & 0 deletions phlow-ffi/src/phlow_text_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use phlow::{PhlowTextView, PhlowView};
use string_box::StringBox;
use value_box::{ReturnBoxerResult, ValueBox, ValueBoxPointer};

use crate::with_view;

#[no_mangle]
pub extern "C" fn phlow_text_view_compute_text(
phlow_view: *mut ValueBox<Box<dyn PhlowView>>,
text: *mut ValueBox<StringBox>,
) {
with_view(phlow_view, |phlow_view: &PhlowTextView| {
text.with_mut_ok(|text| text.set_string(phlow_view.compute_text()))
})
.log();
}
Loading

0 comments on commit aaa1295

Please sign in to comment.