This repository has been archived on 2024-07-25. You can view files and clone it, but cannot push or open issues or pull requests.
estring/README.md

2.3 KiB
Raw Blame History

EString

Crates.io docs.rs GitHub Workflow Status The MSRV

[dependencies]
estring = "0.2"

A simple way to parse a string using type annotations.

This package was originally designed for enve.

Documentation

For more details, see examples.

Usage

Basic

use estring::EString;

fn main() -> estring::Result<()> {
    let res: i32 = EString::from("10").parse()?;
    assert_eq!(res, 10);
    Ok(())
}

You can use predefined structs like SepVec if you enable the structs feature.

Note: You can use custom types as annotations! Just implement ParseFragment!

use estring::{SepVec, EString};

type PlusVec<T> = SepVec<T, '+'>;
type MulVec<T> = SepVec<T, '*'>;

fn main() -> estring::Result<()> {
    let res = EString::from("10+5*2+3")
        .parse::<PlusVec<MulVec<f32>>>()?
        .iter()
        .map(|m| m.iter().product::<f32>())
        .sum::<f32>();

    assert_eq!(res, 23.0);
    Ok(())
}

You can also use predefined aggregators if you enable the aggs feature.

use estring::{Aggregate, EString, Product, SepVec, Sum};

type PlusVec<T> = SepVec<T, '+'>;
type MulVec<T> = SepVec<T, '*'>;

fn main() -> estring::Result<()> {
    let res = EString::from("10+5*2+3")
        .parse::<Sum<PlusVec<Product<MulVec<f32>>>>>()?
        .agg();

    assert_eq!(res, 23.0);
    Ok(())
}

Contact Us

Join us in:

Matrix

License

MIT. See LICENSE to see the full text.

Contributors

pleshevskiy (Dmitriy Pleshevskiy) creator, maintainer.