Skip to content

Commit 2f01c33

Browse files
committed
docs: add rust kcl plugin guide documents
Signed-off-by: peefy <[email protected]>
1 parent e97e72b commit 2f01c33

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

docs/reference/plugin/overview.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,81 @@ import kcl_plugin.my_plugin
174174
175175
result = my_plugin.add(1, 1)
176176
```
177+
178+
## Use Rust to Write Plugins
179+
180+
### 0. Prerequisites
181+
182+
Using the KCL Rust plugin requires the presence of `Rust 1.79+` in your `PATH` and install the KCL Rust SDK.
183+
184+
```shell
185+
cargo add anyhow
186+
cargo add kclvm-parser --git https://github.com/kcl-lang/kcl
187+
cargo add kclvm-loader --git https://github.com/kcl-lang/kcl
188+
cargo add kclvm-evaluator --git https://github.com/kcl-lang/kcl
189+
cargo add kclvm-runtime --git https://github.com/kcl-lang/kcl
190+
```
191+
192+
### 1. Hello Plugin
193+
194+
Write the following Rust code and add the the plugin named `my_plugin`.
195+
196+
```rust
197+
use std::{cell::RefCell, rc::Rc, sync::Arc};
198+
use anyhow::{Result, anyhow};
199+
use kclvm_evaluator::Evaluator;
200+
use kclvm_loader::{load_packages, LoadPackageOptions};
201+
use kclvm_parser::LoadProgramOptions;
202+
use kclvm_runtime::{Context, ValueRef, IndexMap, PluginFunction};
203+
204+
fn my_plugin_sum(_: &Context, args: &ValueRef, _: &ValueRef) -> Result<ValueRef> {
205+
let a = args
206+
.arg_i_int(0, Some(0))
207+
.ok_or(anyhow!("expect int value for the first param"))?;
208+
let b = args
209+
.arg_i_int(1, Some(0))
210+
.ok_or(anyhow!("expect int value for the second param"))?;
211+
Ok((a + b).into())
212+
}
213+
214+
fn context_with_plugin() -> Rc<RefCell<Context>> {
215+
let mut plugin_functions: IndexMap<String, PluginFunction> =
216+
Default::default();
217+
let func = Arc::new(my_plugin_sum);
218+
plugin_functions.insert("my_plugin.add".to_string(), func);
219+
let mut ctx = Context::new();
220+
ctx.plugin_functions = plugin_functions;
221+
Rc::new(RefCell::new(ctx))
222+
}
223+
224+
fn main() -> Result<()> {
225+
let src = r#"
226+
import kcl_plugin.my_plugin
227+
228+
sum = my_plugin.add(1, 1)
229+
"#;
230+
let p = load_packages(&LoadPackageOptions {
231+
paths: vec!["test.k".to_string()],
232+
load_opts: Some(LoadProgramOptions {
233+
load_plugins: true,
234+
k_code_list: vec![src.to_string()],
235+
..Default::default()
236+
}),
237+
load_builtin: false,
238+
..Default::default()
239+
})
240+
.unwrap();
241+
let evaluator = Evaluator::new_with_runtime_ctx(&p.program, context_with_plugin());
242+
let result = evaluator.run()?;
243+
println!("yaml result {}", result.1);
244+
Ok(())
245+
}
246+
```
247+
248+
The content of `test.k` are:
249+
250+
```python
251+
import kcl_plugin.my_plugin
252+
253+
result = my_plugin.add(1, 1)
254+
```

i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/plugin/overview.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,81 @@ import kcl_plugin.my_plugin
173173
174174
result = my_plugin.add(1, 1)
175175
```
176+
177+
## 使用 Rust 编写插件
178+
179+
### 0. 先决条件
180+
181+
使用 KCL Rust 插件需要在您的 PATH 中具有 Rust 1.79+,并安装 KCL Rust SDK。
182+
183+
```shell
184+
cargo add anyhow
185+
cargo add kclvm-parser --git https://github.com/kcl-lang/kcl
186+
cargo add kclvm-loader --git https://github.com/kcl-lang/kcl
187+
cargo add kclvm-evaluator --git https://github.com/kcl-lang/kcl
188+
cargo add kclvm-runtime --git https://github.com/kcl-lang/kcl
189+
```
190+
191+
### 1. 你好插件
192+
193+
编写以下 Rust 代码并添加名为 `my_plugin` 的插件。
194+
195+
```rust
196+
use std::{cell::RefCell, rc::Rc, sync::Arc};
197+
use anyhow::{Result, anyhow};
198+
use kclvm_evaluator::Evaluator;
199+
use kclvm_loader::{load_packages, LoadPackageOptions};
200+
use kclvm_parser::LoadProgramOptions;
201+
use kclvm_runtime::{Context, ValueRef, IndexMap, PluginFunction};
202+
203+
fn my_plugin_sum(_: &Context, args: &ValueRef, _: &ValueRef) -> Result<ValueRef> {
204+
let a = args
205+
.arg_i_int(0, Some(0))
206+
.ok_or(anyhow!("expect int value for the first param"))?;
207+
let b = args
208+
.arg_i_int(1, Some(0))
209+
.ok_or(anyhow!("expect int value for the second param"))?;
210+
Ok((a + b).into())
211+
}
212+
213+
fn context_with_plugin() -> Rc<RefCell<Context>> {
214+
let mut plugin_functions: IndexMap<String, PluginFunction> =
215+
Default::default();
216+
let func = Arc::new(my_plugin_sum);
217+
plugin_functions.insert("my_plugin.add".to_string(), func);
218+
let mut ctx = Context::new();
219+
ctx.plugin_functions = plugin_functions;
220+
Rc::new(RefCell::new(ctx))
221+
}
222+
223+
fn main() -> Result<()> {
224+
let src = r#"
225+
import kcl_plugin.my_plugin
226+
227+
sum = my_plugin.add(1, 1)
228+
"#;
229+
let p = load_packages(&LoadPackageOptions {
230+
paths: vec!["test.k".to_string()],
231+
load_opts: Some(LoadProgramOptions {
232+
load_plugins: true,
233+
k_code_list: vec![src.to_string()],
234+
..Default::default()
235+
}),
236+
load_builtin: false,
237+
..Default::default()
238+
})
239+
.unwrap();
240+
let evaluator = Evaluator::new_with_runtime_ctx(&p.program, context_with_plugin());
241+
let result = evaluator.run()?;
242+
println!("yaml result {}", result.1);
243+
Ok(())
244+
}
245+
```
246+
247+
test.k 的内容为:
248+
249+
```python
250+
import kcl_plugin.my_plugin
251+
252+
result = my_plugin.add(1, 1)
253+
```

0 commit comments

Comments
 (0)