pub struct Dent { /* private fields */ }Expand description
Main struct for parsing Dent.
This struct is used to parse Dent files and strings. It can also be used to register functions that can be called from Dent.
§Examples
use dent_parse::{Dent, Value};
use std::collections::HashMap;
let parser = Dent::default();
assert_eq!(parser.parse("foo"), Ok(Value::Str("foo")));
assert_eq!(parser.parse("[ 1 2 3 ]"), Ok(Value::List(vec![
Value::Int(1),
Value::Int(2),
Value::Int(3)
])));Implementations§
Source§impl Dent
impl Dent
Sourcepub fn new(functions: HashMap<String, Box<Function>>) -> Dent
pub fn new(functions: HashMap<String, Box<Function>>) -> Dent
Creates a new Dent parser with the given functions.
If you want to use the built-in functions, you can use Dent::default,
or call Dent::add_builtins after creating the parser.
Sourcepub fn add_builtins(&mut self)
pub fn add_builtins(&mut self)
Adds the built-in functions to the parser.
This function adds the following functions:
import: Imports a Dent file. Takes a string (file path) as an argument.merge: Merges a list of lists or a list of dicts into a single list or dict.
Sourcepub fn add_function(&mut self, name: &str, function: Box<Function>)
pub fn add_function(&mut self, name: &str, function: Box<Function>)
Adds a function to the parser.
The function can be called from Dent using the @ operator.
The function takes a reference to a value and returns a value.
The function can only take a single argument, for simplicity.
§Examples
use dent_parse::{Dent, Value};
let mut dent = Dent::default();
dent.add_function("count", Box::new(|value| {
if let Value::List(values) = value {
Value::Int(values.len() as i64)
} else {
Value::None
}
}));
assert_eq!(dent.parse("@count [ 1 2 3 ]"), Ok(Value::Int(3)));Sourcepub fn parse<'s>(&self, input: &'s str) -> Result<Value<'s>>
pub fn parse<'s>(&self, input: &'s str) -> Result<Value<'s>>
Parses a Dent string.
The returned value is a zero-copy representation of the parsed Dent string. This means that the returned value borrows from the input string.
If you want to parse a file, use Dent::parse_file instead.
§Examples
use dent_parse::{Dent, Value};
let parser = Dent::default();
assert_eq!(parser.parse("foo"), Ok(Value::Str("foo")));
assert_eq!(parser.parse("2"), Ok(Value::Int(2)));
assert_eq!(parser.parse("2.0"), Ok(Value::Float(2.0)));
assert_eq!(parser.parse("true"), Ok(Value::Bool(true)));Sourcepub fn parse_file<P: AsRef<Path>>(&self, path: P) -> Result<Value<'static>>
pub fn parse_file<P: AsRef<Path>>(&self, path: P) -> Result<Value<'static>>
Parses a Dent file.
The returned value is a zero-copy representation of the parsed Dent. All strings in the returned value borrow from the input file.
The file is read and stored in memory for the lifetime of the program.
§Examples
use dent_parse::{Dent, Value};
use std::collections::HashMap;
let parser = Dent::default();
let value = parser.parse_file("examples/dent/dict.dent").unwrap();
assert_eq!(value, Value::Dict(
vec![
("name", Value::Str("Mario")),
(
"skills",
Value::List(vec![Value::Str("jumps"), Value::Str("grows")])
),
("age", Value::Int(35)),
("alive", Value::Bool(true)),
].into_iter().collect()
));