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
169 | 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 RedisClientArguments {
pub id: String,
pub uri: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct RedisArguments {
pub id: String,
pub key: String,
pub value: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct StringResult {
pub success: String,
pub failure: String,
}
#[derive(Error, Debug)]
pub enum RedisError {
#[error("Redis Client issue")]
ClientFailure,
#[error("Store issue")]
StoreFailure,
#[error("Not found")]
NotFound,
}
extern "C" {
fn hostInitRedisClient(offset: u64) -> u64;
}
pub fn init_redis_client(redis_client_id: String, redis_uri: String) -> Result<String, Error> {
// Prepare the arguments for the host function
// with a JSON string:
// {
// "id": "id of the redis client",
// "uri": "redis uri"
// }
let args = RedisClientArguments {
id: redis_client_id,
uri: redis_uri,
};
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 { hostInitRedisClient(memory_json_str.offset) };
// Get result from the shared memory
// The host function (hostInitRedisClient) returns a JSON buffer:
// {
// "success": "the value associated to the key",
// "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(RedisError::ClientFailure.into());
}
}
extern "C" {
fn hostRedisSet(offset: u64) -> u64;
}
pub fn redis_set(redis_client_id: String, key: String, value: String) -> Result<String, Error> {
// Prepare the arguments for the host function
// with a JSON string:
// {
// "id": "id of the redis client",
// "key": "name",
// "value": "Bob Morane"
// }
let args = RedisArguments {
id: redis_client_id,
key: key,
value: value,
};
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 { hostRedisSet(memory_json_str.offset) };
// Get result from the shared memory
// The host function (hostRedisSet) returns a JSON buffer:
// {
// "success": "the value associated to the key",
// "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(RedisError::StoreFailure.into());
}
}
#[plugin_fn]
pub fn hello(_: String) -> FnResult<u64> {
let redis_uri : String = get_env("REDIS_URI".to_string());
let redis_client : Result<String, Error> = init_redis_client("redisDb".to_string(), redis_uri);
match redis_set("redisDb".to_string(), "100".to_string(), "Huey".to_string()) {
Ok(value) => println("π¦ saved value: ".to_string() + &value),
Err(error) => println("π‘ error: ".to_string() + &error.to_string()),
}
match redis_set("redisDb".to_string(), "200".to_string(), "Dewey".to_string()) {
Ok(value) => println("π¦ saved value: ".to_string() + &value),
Err(error) => println("π‘ error: ".to_string() + &error.to_string()),
}
match redis_set("redisDb".to_string(), "300".to_string(), "Louie".to_string()) {
Ok(value) => println("π¦ saved value: ".to_string() + &value),
Err(error) => println("π‘ error: ".to_string() + &error.to_string()),
}
Ok(0)
}
|