2 releases
0.1.1 | Jun 24, 2024 |
---|---|
0.1.0 | Jun 24, 2024 |
#1034 in Parser implementations
24 downloads per month
41KB
808 lines
new-pkl
Fastest pkl-parsing crate out there!
Features
- Parse Pkl string into a structured representation (hashmap) in rust
- Parse Pkl string into an AST
- Support for strings, integers (decimal, octal, hex, binary), floats, boolean and multiline strings
- Support for nested objects, amends declaration, amends expression and chained amends declaration
- Support for classical identifiers, $identifiers, _identifiers and illegal identifiers
- Support for class instance
Installation
When in your rust project, simply run: cargo add new-pkl
Usage
Here's an example of how to parse a PKL string and retrieve values from the context:
use new_pkl::{Pkl, PklResult, PklValue};
fn main() -> PklResult<()> {
let source = r#"
bool_var = true
int_var = 42
float_var = 3.14
string_var = "hello"
object_var {
key1 = "value1"
key2 = 2
}
"#;
let mut pkl = Pkl::new();
pkl.parse(source)?;
println!("{:?}", pkl.get("int_var")); // Ok(PklValue::Int(100))
// Get values
println!("{:?}", pkl.get_bool("bool_var")); // Ok(true)
println!("{:?}", pkl.get_int("int_var")); // Ok(42)
println!("{:?}", pkl.get_float("float_var")); // Ok(3.14)
println!("{:?}", pkl.get_string("string_var")); // Ok("hello")
println!("{:?}", pkl.get_object("object_var")); // Ok(HashMap with key1 and key2)
// Modify values
pkl.set("int_var", PklValue::Int(100));
// Remove values
pkl.remove("float_var");
println!("{:?}", pkl.get_float("float_var")); // Err("Variable `float_var` not found")
// Or just generate an ast
let mut pkl = Pkl::new();
// the ast contains the start and end indexes of each value and statement
let ast = pkl.generate_ast(source)?;
Ok(())
}
LICENSE
This project is licensed under the MIT License. See the LICENSE file for details.
Dependencies
~2MB