rhizosdk/
lib.rs

1pub mod arg;
2pub mod test;
3pub mod print;
4pub mod onchain;
5
6use std::io::{Read, Write};
7
8/// This macro sets up the scaffolding for a Rhizo route's main function, making
9/// the argument buffer accessible and returning the final Vec<u8> evaluation
10#[macro_export]
11macro_rules! fn_main {
12    ($body:expr) => {
13        fn main() {
14            use std::io::{Read, Write};
15            let mut __buffer = read_argument_bytes();
16            let mut __cursor: std::io::Cursor<&mut [u8]> = std::io::Cursor::new(__buffer.as_mut_slice()) ;
17            let mut __mut_ref: &mut std::io::Cursor<&mut [u8]> = &mut __cursor;
18            let __response_bytes: Vec<u8> = $body(__mut_ref);
19            std::io::stdout().flush().unwrap();
20            write_return(__response_bytes);
21        }
22    };
23}
24
25/// This macro is intended to be used within the fn_main! macro 
26#[macro_export]
27macro_rules! async_runtime {
28    ($async_block:block) => {
29        let __rt = tokio::runtime::Runtime::new().unwrap();
30        let mut __result = Vec::<u8>::new();
31        __rt.block_on(async {
32            __result = $async_block;
33            write_return(__result.clone())
34        })
35    }
36}
37
38pub fn read_argument_bytes() -> Vec<u8> {
39    let arg_len_bytes = std::io::stdin().take(4).bytes();
40    let mut arg_len_vec = Vec::<u8>::new();
41    for b in arg_len_bytes {
42        arg_len_vec.push(b.unwrap());
43    }
44    
45    let arg_len_array: [u8; 4] = arg_len_vec.try_into().unwrap();
46    let arg_len = i32::from_le_bytes(arg_len_array); 
47    
48    let arg_bytes: Vec<u8> = std::io::stdin().take(arg_len as u64).bytes().map(|b| b.unwrap()).collect();
49    arg_bytes
50}
51
52
53pub fn write_return(return_bytes: Vec<u8>){    
54    let return_len_bytes = (return_bytes.len() as i32).to_le_bytes();
55    std::io::stderr().write_all(return_len_bytes.as_slice()).unwrap();
56    std::io::stderr().write_all(return_bytes.as_slice()).unwrap();
57    std::io::stdout().flush().unwrap();
58}