Skip to content

Commit af14d01

Browse files
authored
Merge pull request #466 from Peefy/docs-rust-plugin-docs
docs: add rust kcl plugin guide documents
2 parents e97e72b + b13b274 commit af14d01

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

docs/reference/plugin/overview.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,79 @@ 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 anyhow::{anyhow, Result};
198+
use kclvm_evaluator::Evaluator;
199+
use kclvm_loader::{load_packages, LoadPackageOptions};
200+
use kclvm_parser::LoadProgramOptions;
201+
use kclvm_runtime::{Context, IndexMap, PluginFunction, ValueRef};
202+
use std::{cell::RefCell, rc::Rc, sync::Arc};
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> = 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+
let evaluator = Evaluator::new_with_runtime_ctx(&p.program, context_with_plugin());
240+
let result = evaluator.run()?;
241+
println!("yaml result {}", result.1);
242+
Ok(())
243+
}
244+
```
245+
246+
The content of `test.k` are:
247+
248+
```python
249+
import kcl_plugin.my_plugin
250+
251+
result = my_plugin.add(1, 1)
252+
```

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,79 @@ 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 anyhow::{anyhow, Result};
197+
use kclvm_evaluator::Evaluator;
198+
use kclvm_loader::{load_packages, LoadPackageOptions};
199+
use kclvm_parser::LoadProgramOptions;
200+
use kclvm_runtime::{Context, IndexMap, PluginFunction, ValueRef};
201+
use std::{cell::RefCell, rc::Rc, sync::Arc};
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> = Default::default();
215+
let func = Arc::new(my_plugin_sum);
216+
plugin_functions.insert("my_plugin.add".to_string(), func);
217+
let mut ctx = Context::new();
218+
ctx.plugin_functions = plugin_functions;
219+
Rc::new(RefCell::new(ctx))
220+
}
221+
222+
fn main() -> Result<()> {
223+
let src = r#"
224+
import kcl_plugin.my_plugin
225+
226+
sum = my_plugin.add(1, 1)
227+
"#;
228+
let p = load_packages(&LoadPackageOptions {
229+
paths: vec!["test.k".to_string()],
230+
load_opts: Some(LoadProgramOptions {
231+
load_plugins: true,
232+
k_code_list: vec![src.to_string()],
233+
..Default::default()
234+
}),
235+
load_builtin: false,
236+
..Default::default()
237+
})?;
238+
let evaluator = Evaluator::new_with_runtime_ctx(&p.program, context_with_plugin());
239+
let result = evaluator.run()?;
240+
println!("yaml result {}", result.1);
241+
Ok(())
242+
}
243+
```
244+
245+
test.k 的内容为:
246+
247+
```python
248+
import kcl_plugin.my_plugin
249+
250+
result = my_plugin.add(1, 1)
251+
```

0 commit comments

Comments
 (0)