Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
deps: bump estring to 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pleshevskiy committed Jul 29, 2022
1 parent 04d0835 commit 9ccd86c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ all-features = true
default = []
low-level = ["estring/low-level"]
structs = ["estring/structs"]
aggs = ["estring/aggs"]

# deprecated
number = []
bool = []
vec = ["structs"]

[dependencies]
estring = "0.2"
estring = "0.3"

[badges]
maintenance = { status = "actively-developed" }

[[example]]
name = "calc"
required-features = ["structs"]
required-features = ["structs", "aggs"]

2 changes: 0 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ E=2*2-1-1+5*3-10 cargo run --example calc --all-features
Limits (yet):

- Supports `*`, `+`, `-`
- You cannot start from a negative number. `E=-10`. Solution: start from `0`.
`E=0-10`.
56 changes: 44 additions & 12 deletions examples/calc.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use enve::SepVec;
use enve::{
estring::{self, Aggregatable, Aggregate},
Product, SepVec, Sum,
};

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

const HELP_MESSAGE: &str = "
USAGE:
E=10+10*2+4 cargo run --example calc --all-features
E=10+10*2-4 cargo run --example calc --all-features
";

fn main() -> Result<(), enve::Error> {
let res: f32 = enve::get::<PlusVec<MinusVec<MulVec<f32>>>>("E")
let res: f32 = enve::get::<Sum<PlusVec<MinusVec<Product<MulVec<f32>>>>>>("E")
.map_err(|err| {
match err {
enve::Error::NotPresent => eprintln!("The expression was not found"),
Expand All @@ -21,16 +23,46 @@ fn main() -> Result<(), enve::Error> {
std::process::exit(0);
})
.unwrap()
.iter()
.map(|p| {
p.iter()
.map(|m| m.iter().product::<f32>())
.reduce(|acc, v| acc - v)
.unwrap_or_default()
})
.sum::<f32>();
.agg();

println!("result: {}", res);

Ok(())
}

struct MinusVec<T>(Vec<T>);

impl<T> estring::ParseFragment for MinusVec<T>
where
T: estring::ParseFragment,
{
fn parse_frag(es: estring::EString) -> estring::Result<Self> {
let mut prev: Option<&str> = None;
es.split('-')
.map(str::trim)
.map(|val| match prev.replace(val) {
None => String::from(val),
Some(_) => {
let mut s = val.to_owned();
s.insert(0, '-');
s
}
})
.filter(|val| !val.is_empty())
.map(estring::EString::from)
.map(T::parse_frag)
.collect::<estring::Result<Vec<T>>>()
.map(Self)
}
}

impl<T> estring::Aggregatable for MinusVec<T>
where
T: Aggregatable,
{
type Item = T::Item;

fn items(self) -> Vec<Self::Item> {
self.0.into_iter().flat_map(T::items).collect()
}
}
3 changes: 3 additions & 0 deletions src/estr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ mod structs;
pub use structs::{CommaVec, SemiVec};

pub use estring::core::EString;

#[cfg(feature = "aggs")]
pub use estring::agg::*;
#[cfg(feature = "low-level")]
pub use estring::low::*;
#[cfg(feature = "structs")]
Expand Down

0 comments on commit 9ccd86c

Please sign in to comment.