Rust API
Installation
cargo add --git https://github.com/kcl-lang/lib
Quick Start
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(())
}
More Rust APIs can be found here. If you want to use the sub crate of KCL Rust core, you can run the following command.
# Take the kclvm-runtime as an example.
cargo add --git https://github.com/kcl-lang/kcl kclvm-runtime
API Reference
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.