feat: extism works

This commit is contained in:
2022-12-04 15:00:28 +08:00
parent 78561aec5b
commit 1098220058
6 changed files with 185 additions and 3 deletions

Binary file not shown.

View File

@@ -1,9 +1,12 @@
use extism::{Context, Plugin}; use extism::{Context, Plugin};
fn main() { fn main() {
let wasm = include_bytes!("../code.wasm"); let wasm = include_bytes!("../extism_plugin.wasm");
let context = Context::new(); let context = Context::new();
let mut plugin = Plugin::new(&context, wasm, false).unwrap(); let mut plugin = Plugin::new(&context, wasm, false).unwrap();
let data = plugin.call("count_vowels", "this is a test").unwrap(); let result = plugin.call("count_vowels", "this is a test");
assert_eq!(data, b"{\"count\": 4}"); println!("{:?}", result);
// println!("{}", result.unwrap_err());
let data = result.unwrap();
println!("{}", String::from_utf8((&data).to_vec()).unwrap());
} }

136
__wasm/extism/extism_plugin/Cargo.lock generated Normal file
View File

@@ -0,0 +1,136 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6"
[[package]]
name = "base64"
version = "0.20.0-alpha.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "149ea5dc24cb11513350770afebba32b68e3d2e356f9221351a2a1ee89112a82"
[[package]]
name = "extism-manifest"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e943e9de8151a8e9e45bc0703e636ba1ce1e229b4c42706c3fcc8cb0a2894b51"
dependencies = [
"base64",
"serde",
]
[[package]]
name = "extism-pdk"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25a3e593deeba116fbbeed4b73c33ffbdcd4223a341bd2b472242a703a52a319"
dependencies = [
"anyhow",
"base64",
"extism-manifest",
"extism-pdk-derive",
"serde",
"serde_json",
]
[[package]]
name = "extism-pdk-derive"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "659ef7e08bc205c1080a04f36f779fc3309af9e7dffc47b9372ce777bcc66d43"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "extism_plugin"
version = "0.1.0"
dependencies = [
"extism-pdk",
"serde",
]
[[package]]
name = "itoa"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc"
[[package]]
name = "proc-macro2"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "serde"
version = "1.0.148"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e53f64bb4ba0191d6d0676e1b141ca55047d83b74f5607e6d8eb88126c52c2dc"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.148"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a55492425aa53521babf6137309e7d34c20bbfbbfcfe2c7f3a047fd1f6b92c0c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"

View File

@@ -0,0 +1,13 @@
[package]
name = "extism_plugin"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate_type = ["cdylib"]
[dependencies]
serde = "1.0.148"
extism-pdk = "0.1.0"

View File

@@ -0,0 +1,7 @@
_:
@just --list
build:
cargo build --release --target wasm32-unknown-unknown

View File

@@ -0,0 +1,23 @@
use extism_pdk::*;
use serde::Serialize;
const VOWELS: &[char] = &['a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'];
#[derive(Serialize)]
struct TestOutput {
pub count: i32,
}
#[plugin_fn]
pub fn count_vowels(input: String) -> FnResult<Json<TestOutput>> {
let mut count = 0;
for ch in input.chars() {
if VOWELS.contains(&ch) {
count += 1;
}
}
let output = TestOutput { count };
Ok(Json(output))
}