Dent

Struct Dent 

Source
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

Source

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.

Source

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.
Source

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)));
Source

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)));
Source

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()
));

Trait Implementations§

Source§

impl Default for Dent

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Dent

§

impl RefUnwindSafe for Dent

§

impl Send for Dent

§

impl Sync for Dent

§

impl Unpin for Dent

§

impl UnwindSafe for Dent

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.