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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 | use extism_pdk::*;
use serde::{Serialize, Deserialize};
use thiserror::Error;
extern "C" {
fn hostPrintln(ptr: u64) -> u64;
}
pub fn println(text: String) {
let mut memory_text: Memory = extism_pdk::Memory::new(text.len());
memory_text.store(text);
unsafe { hostPrintln(memory_text.offset) };
}
extern "C" {
fn hostGetEnv(ptr: u64) -> u64;
}
pub fn get_env(name: String) -> String {
// copy the name of the environment variable to the shared memory
let mut variable_name: Memory = extism_pdk::Memory::new(name.len());
variable_name.store(name);
// call the host function
let offset: u64 = unsafe { hostGetEnv(variable_name.offset) };
// read the value of the result from the shared memory
let variable_value: Memory = extism_pdk::Memory::find(offset).unwrap();
// return the value
return variable_value.to_string().unwrap()
}
#[derive(Serialize, Deserialize, Debug)]
struct NatsConfig {
pub id: String,
pub url: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct NatsMessage {
pub id: String,
pub subject: String,
pub data: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct StringResult {
pub success: String,
pub failure: String,
}
#[derive(Error, Debug)]
pub enum NatsError {
#[error("Nats Connection issue")]
ConnectionFailure,
#[error("Store issue")]
MessageFailure,
#[error("Not found")]
NotFound,
}
extern "C" {
fn hostInitNatsConnection(offset: u64) -> u64;
}
pub fn init_nats_connection(nats_connection_id: String, nats_url: String) -> Result<String, Error> {
// Prepare the arguments for the host function
// with a JSON string:
// {
// "id": "id of the NATS connection",
// "url": "URL of the NATS server"
// }
let args = NatsConfig {
id: nats_connection_id,
url: nats_url,
};
let json_str: String = serde_json::to_string(&args).unwrap();
// Copy the string value to the shared memory
let mut memory_json_str: Memory = extism_pdk::Memory::new(json_str.len());
memory_json_str.store(json_str);
// Call host function with the offset of the arguments
let offset: u64 = unsafe { hostInitNatsConnection(memory_json_str.offset) };
// Get result from the shared memory
// The host function (hostInitNatsConnection) returns a JSON buffer:
// {
// "success": "id of the connection",
// "failure": "error message if error, else empty"
// }
let memory_result: Memory = extism_pdk::Memory::find(offset).unwrap();
let json_string:String = memory_result.to_string().unwrap();
let result: StringResult = serde_json::from_str(&json_string).unwrap();
if result.failure.is_empty() {
return Ok(result.success);
} else {
return Err(NatsError::ConnectionFailure.into());
}
}
extern "C" {
fn hostNatsPublish(offset: u64) -> u64;
}
pub fn nats_publish(nats_connection_id: String, subject: String, data: String) -> Result<String, Error> {
// Prepare the arguments for the host function
// with a JSON string:
// {
// "id": "id of the NATS client",
// "subject": "name",
// "data": "Bob Morane"
// }
let args = NatsMessage {
id: nats_connection_id,
subject: subject,
data: data,
};
let json_str: String = serde_json::to_string(&args).unwrap();
// Copy the string value to the shared memory
let mut memory_json_str: Memory = extism_pdk::Memory::new(json_str.len());
memory_json_str.store(json_str);
// Call host function with the offset of the arguments
let offset: u64 = unsafe { hostNatsPublish(memory_json_str.offset) };
// Get result from the shared memory
// The host function returns a JSON buffer:
// {
// "success": "OK",
// "failure": "error message if error, else empty"
// }
let memory_result: Memory = extism_pdk::Memory::find(offset).unwrap();
let json_string:String = memory_result.to_string().unwrap();
let result: StringResult = serde_json::from_str(&json_string).unwrap();
if result.failure.is_empty() {
return Ok(result.success);
} else {
return Err(NatsError::MessageFailure.into());
}
}
#[plugin_fn]
pub fn publish(input: String) -> FnResult<u64> {
let nats_url : String = get_env("NATS_URL".to_string());
let nats_connection : Result<String, Error> = init_nats_connection("natsconn01".to_string(), nats_url);
match nats_connection {
Ok(value) => println("π¦ nats connection: ".to_string() + &value),
Err(error) => println("π‘ error: ".to_string() + &error.to_string()),
}
match nats_publish("natsconn01".to_string(), "news".to_string(), input.to_string()) {
Ok(value) => println("π¦ π ".to_string() + &value),
Err(error) => println("π‘ error: ".to_string() + &error.to_string()),
}
Ok(0)
}
|