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

Commit 9ccd86c

Browse files
committed
deps: bump estring to 0.3.0
1 parent 04d0835 commit 9ccd86c

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ all-features = true
2020
default = []
2121
low-level = ["estring/low-level"]
2222
structs = ["estring/structs"]
23+
aggs = ["estring/aggs"]
2324

2425
# deprecated
2526
number = []
2627
bool = []
2728
vec = ["structs"]
2829

2930
[dependencies]
30-
estring = "0.2"
31+
estring = "0.3"
3132

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

3536
[[example]]
3637
name = "calc"
37-
required-features = ["structs"]
38+
required-features = ["structs", "aggs"]
3839

examples/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@ E=2*2-1-1+5*3-10 cargo run --example calc --all-features
1111
Limits (yet):
1212

1313
- Supports `*`, `+`, `-`
14-
- You cannot start from a negative number. `E=-10`. Solution: start from `0`.
15-
`E=0-10`.

examples/calc.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
use enve::SepVec;
1+
use enve::{
2+
estring::{self, Aggregatable, Aggregate},
3+
Product, SepVec, Sum,
4+
};
25

3-
type MinusVec<T> = SepVec<T, '-'>;
46
type PlusVec<T> = SepVec<T, '+'>;
57
type MulVec<T> = SepVec<T, '*'>;
68

79
const HELP_MESSAGE: &str = "
810
USAGE:
9-
E=10+10*2+4 cargo run --example calc --all-features
11+
E=10+10*2-4 cargo run --example calc --all-features
1012
";
1113

1214
fn main() -> Result<(), enve::Error> {
13-
let res: f32 = enve::get::<PlusVec<MinusVec<MulVec<f32>>>>("E")
15+
let res: f32 = enve::get::<Sum<PlusVec<MinusVec<Product<MulVec<f32>>>>>>("E")
1416
.map_err(|err| {
1517
match err {
1618
enve::Error::NotPresent => eprintln!("The expression was not found"),
@@ -21,16 +23,46 @@ fn main() -> Result<(), enve::Error> {
2123
std::process::exit(0);
2224
})
2325
.unwrap()
24-
.iter()
25-
.map(|p| {
26-
p.iter()
27-
.map(|m| m.iter().product::<f32>())
28-
.reduce(|acc, v| acc - v)
29-
.unwrap_or_default()
30-
})
31-
.sum::<f32>();
26+
.agg();
3227

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

3530
Ok(())
3631
}
32+
33+
struct MinusVec<T>(Vec<T>);
34+
35+
impl<T> estring::ParseFragment for MinusVec<T>
36+
where
37+
T: estring::ParseFragment,
38+
{
39+
fn parse_frag(es: estring::EString) -> estring::Result<Self> {
40+
let mut prev: Option<&str> = None;
41+
es.split('-')
42+
.map(str::trim)
43+
.map(|val| match prev.replace(val) {
44+
None => String::from(val),
45+
Some(_) => {
46+
let mut s = val.to_owned();
47+
s.insert(0, '-');
48+
s
49+
}
50+
})
51+
.filter(|val| !val.is_empty())
52+
.map(estring::EString::from)
53+
.map(T::parse_frag)
54+
.collect::<estring::Result<Vec<T>>>()
55+
.map(Self)
56+
}
57+
}
58+
59+
impl<T> estring::Aggregatable for MinusVec<T>
60+
where
61+
T: Aggregatable,
62+
{
63+
type Item = T::Item;
64+
65+
fn items(self) -> Vec<Self::Item> {
66+
self.0.into_iter().flat_map(T::items).collect()
67+
}
68+
}

src/estr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ mod structs;
44
pub use structs::{CommaVec, SemiVec};
55

66
pub use estring::core::EString;
7+
8+
#[cfg(feature = "aggs")]
9+
pub use estring::agg::*;
710
#[cfg(feature = "low-level")]
811
pub use estring::low::*;
912
#[cfg(feature = "structs")]

0 commit comments

Comments
 (0)