1
//! Example of WebAssembly compatibility for JetStream RPC
2
//! 
3
//! This example demonstrates how to use JetStream RPC in a WebAssembly context.
4

            
5
use jetstream_macros::JetStreamWireFormat;
6
use jetstream_wireformat::WireFormat;
7
use std::io::Cursor;
8

            
9
#[derive(Debug, JetStreamWireFormat)]
10
struct Message {
11
    id: u32,
12
    content: String,
13
}
14

            
15
// This function would be exported to JavaScript in a real WebAssembly binary
16
fn encode_message(id: u32, content: String) -> Vec<u8> {
17
    let msg = Message { id, content };
18
    let mut buffer = Vec::new();
19
    msg.encode(&mut buffer).expect("Failed to encode message");
20
    buffer
21
}
22

            
23
// This function would be exported to JavaScript in a real WebAssembly binary
24
fn decode_message(buffer: &[u8]) -> (u32, String) {
25
    let mut cursor = Cursor::new(buffer);
26
    let msg: Message = WireFormat::decode(&mut cursor).expect("Failed to decode message");
27
    (msg.id, msg.content)
28
}
29

            
30
// Simple example function
31
fn main() {
32
    // Create a message
33
    let encoded = encode_message(42, "Hello from WebAssembly!".to_string());
34
    println!("Encoded message: {:?}", encoded);
35
    
36
    // Decode the message
37
    let (id, content) = decode_message(&encoded);
38
    println!("Decoded message: id={}, content=\"{}\"", id, content);
39
}