1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! This crate provides WebAssembly host functions and other utilities for Space Operator.
//! 
//! ## Macro
//! 
//! ```rust
//! use space_lib::space;
//! use serde::{Serialize, Deserialize};
//! 
//! #[derive(Deserialize)]
//! struct Input {
//!     value: usize,
//!     name: String,
//! }
//! 
//! #[derive(Serialize)]
//! struct Output {
//!     value: usize,
//!     name: String,
//! }
//! 
//! #[space]
//! fn main(input: Input) -> Output {
//!     Output {
//!         value: input.value * 2,
//!         name: input.name.chars().rev().collect(),
//!     }
//! }
//! ```
//!
//! ## Result
//! 
//! ```rust
//! use space_lib::{space, Result};
//! 
//! #[space]
//! fn main() -> Result<u64> {
//!     Ok("123".parse()?)
//! }
//! ```
//!
//! ## HTTP client
//! 
//! ```rust
//! use space_lib::Request;
//! 
//! let body = Request::get("https://www.spaceoperator.com")
//!     .call()?
//!     .into_string()?;
//! ```
//! 
//! ## Supabase
//! 
//! ```rust
//! use space_lib::Supabase;
//! 
//! let client = Supabase::new("https://hyjbiblkjrrvkzaqsyxe.supabase.co")
//!     .insert_header("apikey", "anon_api_key");
//! 
//! let rows = client
//!     .from("dogs")
//!     .select("name")
//!     .execute()?
//!     .into_string()?;
//! ```
//! 
//! ## Solana
//! 
//! ```rust
//! use space_lib::Solana;
//! 
//! let client = Solana::new("https://api.devnet.solana.com");
//! let balance = client.get_balance("base58_encoded_pubkey")?;
//! ```

pub mod common;
mod error;
mod ffi;
mod http;
mod solana;
mod supabase;

pub use solana::Solana;
pub use serde_json::json;
pub use http::{Request, Response};
pub use supabase::{Supabase, Builder};
pub use error::{SpaceError, HostError};

// Macro
pub use space_macro::space;

#[repr(C)]
pub struct SpaceSlice {
    pub len: usize,
    pub ptr: *mut u8,
}

// Error handling compatible with the space runtime
#[allow(dead_code)]
#[repr(transparent)]
pub struct Error(pub String);

impl Error {
    pub fn new<T: std::fmt::Display>(message: T) -> Self {
        Self(message.to_string())
    }
}

impl<T: std::fmt::Display> From<T> for Error {
    fn from(message: T) -> Self {
        Self(message.to_string())
    }
}

pub type Result<T, E = Error> = std::result::Result<T, E>;