Add macos/ios app using rust lib

This commit is contained in:
Piv
2023-02-03 21:29:23 +10:30
parent 1168ca46db
commit 734a19f711
25 changed files with 1265 additions and 8 deletions

View File

@@ -1,4 +1,8 @@
mod move_money;
use std::ffi::c_char;
use std::ffi::CStr;
use std::ffi::CString;
pub use self::move_money::*;
mod smush_rules;
@@ -6,3 +10,47 @@ pub use self::smush_rules::*;
mod overhead_allocation;
pub use self::overhead_allocation::*;
#[no_mangle]
pub extern "C" fn move_money_from_text(
rules: *const c_char,
lines: *const c_char,
use_numeric_accounts: bool,
) -> *mut c_char {
let mut output_writer = csv::Writer::from_writer(vec![]);
let safe_rules = unsafe {
assert!(!rules.is_null());
CStr::from_ptr(rules)
};
let safe_lines = unsafe {
assert!(!lines.is_null());
CStr::from_ptr(lines)
};
move_money(
csv::Reader::from_reader(safe_rules.to_bytes()),
csv::Reader::from_reader(safe_lines.to_bytes()),
&mut output_writer,
use_numeric_accounts,
);
// TODO: Replace all these unwraps with something more elegant
let inner = output_writer.into_inner().unwrap();
CString::new(String::from_utf8(inner).unwrap())
.unwrap()
.into_raw()
// Also some resources I looked at, in case things aren't going right:
// https://notes.huy.rocks/en/string-ffi-rust.html
// http://jakegoulding.com/rust-ffi-omnibus/string_return/
// https://rust-unofficial.github.io/patterns/idioms/ffi/passing-strings.html
// This looks like exactly what I'm doing too: https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-06-rust-on-ios.htmlcar
}
#[no_mangle]
pub extern "C" fn move_money_from_text_free(s: *mut c_char) {
unsafe {
if s.is_null() {
return;
}
CString::from_raw(s)
};
}