#serialization #proc-macro #function #data #faas #json-format

macro rline_macro

A Rust procedural macro for generating WebAssembly stubs with customizable serialization formats

1 stable release

1.0.0 Dec 12, 2023

#1086 in WebAssembly

Apache-2.0

14KB
169 lines

rline_macro

A Rust procedural macro for generating WebAssembly stubs with customizable serialization formats.

Overview

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".

Installation

Add the following line to your Cargo.toml file:

[dependencies]
rline_macro = "1.0"

Data Types

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.

Examples

Default format

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)
}

Explicitly specify the json format

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)
}

Explicitly specify the bincode format

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)
}

Attribution

This crate is inspired from wasmedge-bindgen, https://github.com/second-state/wasmedge-bindgen, under the MIT/Apache v2 license.

Dependencies

~255–710KB
~17K SLoC