1#![doc = include_str!("../README.md")]
2#![deny(missing_docs)]
3
4use crate::abi::Buffer;
5pub use abi::init_logger;
6use anyhow::Result;
7use service::{
8 api::{AccumulateArgs, Accumulated, AuthorizeArgs, RefineArgs},
9 service::result::{Executed, Refined},
10};
11
12pub fn authorize(args: AuthorizeArgs) -> Result<Executed> {
14 let encoded = codec::encode(&args)?;
15 let input = Buffer {
16 ptr: encoded.as_ptr(),
17 len: encoded.len(),
18 };
19
20 let output: Buffer = unsafe { abi::authorize(input) };
21 codec::decode(&output.to_vec()).map_err(Into::into)
22}
23
24pub fn refine(args: RefineArgs) -> Result<Refined> {
26 let encoded = codec::encode(&args)?;
27 let input = Buffer {
28 ptr: encoded.as_ptr(),
29 len: encoded.len(),
30 };
31
32 let output = unsafe { abi::refine(input) };
33 codec::decode(&output.to_vec()).map_err(Into::into)
34}
35
36pub fn accumulate(args: AccumulateArgs) -> Result<Accumulated> {
38 let encoded = codec::encode(&args)?;
39 let input = Buffer {
40 ptr: encoded.as_ptr(),
41 len: encoded.len(),
42 };
43
44 let output = unsafe { abi::accumulate(input) };
45 codec::decode(&output.to_vec()).map_err(Into::into)
46}
47
48mod abi {
49 #[cfg(feature = "interp")]
50 pub use {
51 interp_accumulate as accumulate, interp_authorize as authorize, interp_refine as refine,
52 };
53
54 #[cfg(not(feature = "interp"))]
55 pub use {comp_accumulate as accumulate, comp_authorize as authorize, comp_refine as refine};
56
57 #[repr(C)]
58 #[derive(Copy, Clone, Debug)]
59 pub struct Buffer {
60 pub ptr: *const u8,
61 pub len: usize,
62 }
63
64 impl Buffer {
65 pub fn to_vec(self) -> Vec<u8> {
67 let result = unsafe { core::slice::from_raw_parts(self.ptr, self.len).to_vec() };
68 unsafe {
69 let layout = std::alloc::Layout::from_size_align(self.len, 1).unwrap();
70 std::alloc::dealloc(self.ptr as *mut _, layout);
71 }
72 result
73 }
74 }
75
76 unsafe extern "C" {
77 pub fn init_logger(ansi: bool, timer: bool);
79
80 #[cfg(not(feature = "interp"))]
82 pub fn comp_authorize(args: Buffer) -> Buffer;
83
84 #[cfg(not(feature = "interp"))]
86 pub fn comp_refine(args: Buffer) -> Buffer;
87
88 #[cfg(not(feature = "interp"))]
90 pub fn comp_accumulate(args: Buffer) -> Buffer;
91
92 #[cfg(feature = "interp")]
94 pub fn interp_authorize(args: Buffer) -> Buffer;
95
96 #[cfg(feature = "interp")]
98 pub fn interp_refine(args: Buffer) -> Buffer;
99
100 #[cfg(feature = "interp")]
102 pub fn interp_accumulate(args: Buffer) -> Buffer;
103 }
104}