Rust API
添加依赖
cargo add --git https://github.com/kcl-lang/lib
快速开始
use kcl_lang::*;
use anyhow::Result;
fn main() -> Result<()> {
let api = API::default();
let args = &ExecProgramArgs {
k_filename_list: vec!["main.k".to_string()],
k_code_list: vec!["a = 1".to_string()],
..Default::default()
};
let exec_result = api.exec_program(args)?;
println!("{}", exec_result.yaml_result);
Ok(())
}
更多 Rust API 可以在这里找到. 如果您想使用KCL Rust API 的子 crate,可以运行以下命令 (以 kclvm-runtime 为例)。
# Take the kclvm-runtime as an example.
cargo add --git https://github.com/kcl-lang/kcl kclvm-runtime
API 参考
exec_program
Execute KCL file with args.
Example
format_code
Service for formatting a code source and returns the formatted source and whether the source is changed.
Example
format_path
Service for formatting kcl file or directory path contains kcl files and returns the changed file paths.
Example
lint_path
Service for KCL Lint API, check a set of files, skips execute, returns error message including errors and warnings.
Example
validate_code
Service for validating the data string using the schema code string, when the parameter schema is omitted, use the first schema appeared in the kcl code.
Example
load_settings_files
Service for building setting file config from args.
Example
rename
Service for renaming all the occurrences of the target symbol in the files. This API will rewrite files if they contain symbols to be renamed. return the file paths got changed.
Example
rename_code
Service for renaming all the occurrences of the target symbol and rename them. This API won’t rewrite files but return the modified code if any code has been changed. return the changed code.
Example
test
Service for the testing tool.
Example
update_dependencies
update_dependencies provides users with the ability to update kcl module dependencies.