Crates.io | rline_macro |
lib.rs | rline_macro |
version | 1.0.0 |
source | src |
created_at | 2023-12-12 16:47:43.5846 |
updated_at | 2023-12-12 16:47:43.5846 |
description | A Rust procedural macro for generating WebAssembly stubs with customizable serialization formats. |
homepage | https://punchplatform.com/ |
repository | |
max_upload_size | |
id | 1066609 |
size | 24,660 |
A Rust procedural macro for generating WebAssembly stubs with customizable serialization formats.
rline_bindgen
is a procedural macro attribute designed to simplify the process of generating WebAssembly (Wasm)
stubs for Rust functions.
It allows you to choose the serialization format for input and output, supporting both "bincode" and "json".
Add the following line to your Cargo.toml
file:
[dependencies]
rline_macro = "1.0"
Row
is defined in the rline_api
. It is the base item in data processing.
The function encapsulated by the macro takes a Row as parameter.
It returns a Result<Row, String>
with the produced Row in case of success or an error message in case of failure.
use rline_api::row::Row;
use rline_api::value::Value;
use rline_macro::rline_bindgen;
/// Transform each string value to uppercase using
/// the default serialization format.
#[rline_bindgen]
pub fn uppercase(row: Row) -> Result<Row, String> {
let mut result = Row::with_capacity(row.len());
for (column_name, value) in row {
match value {
Value::String(s) => result.insert(
column_name,
Value::from(s.to_uppercase())
),
_ => result.insert(column_name, value),
};
}
Ok(result)
}
use rline_api::row::Row;
use rline_api::value::Value;
use rline_macro::rline_bindgen;
/// Repeat one time each string value using
/// the json serialization format.
#[rline_bindgen(json)]
pub fn repeat_json(row: Row) -> Result<Row, String> {
// You are able to write to stderr.
eprintln!("Printing to stderr from the Wasm");
// However, writing to stdout will be ignored.
println!("Printing to stdout will be ignored");
let mut result = Row::with_capacity(row.len());
for (column_name, value) in row {
match value {
Value::String(s) => result.insert(
column_name,
Value::from(s.repeat(2))
),
_ => result.insert(column_name, value),
};
}
Ok(result)
}
use rline_api::row::Row;
use rline_api::value::Value;
use rline_macro::rline_bindgen;
/// Transform each string value to to_lowercase using
/// the bincode serialization format.
#[rline_bindgen(bincode)]
pub fn lowercase_bincode(row: Row) -> Result<Row, String> {
let mut result = Row::with_capacity(row.len());
for (column_name, value) in row {
match value {
Value::String(s) => result.insert(
column_name,
Value::from(s.to_lowercase())
),
_ => result.insert(column_name, value),
};
}
Ok(result)
}
This crate is inspired from wasmedge-bindgen
, https://github.com/second-state/wasmedge-bindgen,
under the MIT/Apache v2 license.