deps: bump estring to 0.3.0

This commit is contained in:
Dmitriy Pleshevskiy 2022-07-28 16:02:42 +03:00
parent 04d0835185
commit 9ccd86ce1e
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
5 changed files with 52 additions and 18 deletions

4
Cargo.lock generated
View file

@ -11,6 +11,6 @@ dependencies = [
[[package]] [[package]]
name = "estring" name = "estring"
version = "0.2.1" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d0069d8a2500e291e248b96dc353cf71e2693b278058fd2954b5ed90c836e77" checksum = "a7cc7b476dae2894e7fb8d37102acc505c8981f754d1cf44aaf666f20cf21e74"

View file

@ -20,6 +20,7 @@ all-features = true
default = [] default = []
low-level = ["estring/low-level"] low-level = ["estring/low-level"]
structs = ["estring/structs"] structs = ["estring/structs"]
aggs = ["estring/aggs"]
# deprecated # deprecated
number = [] number = []
@ -27,12 +28,12 @@ bool = []
vec = ["structs"] vec = ["structs"]
[dependencies] [dependencies]
estring = "0.2" estring = "0.3"
[badges] [badges]
maintenance = { status = "actively-developed" } maintenance = { status = "actively-developed" }
[[example]] [[example]]
name = "calc" name = "calc"
required-features = ["structs"] required-features = ["structs", "aggs"]

View file

@ -11,5 +11,3 @@ E=2*2-1-1+5*3-10 cargo run --example calc --all-features
Limits (yet): Limits (yet):
- Supports `*`, `+`, `-` - Supports `*`, `+`, `-`
- You cannot start from a negative number. `E=-10`. Solution: start from `0`.
`E=0-10`.

View file

@ -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 PlusVec<T> = SepVec<T, '+'>;
type MulVec<T> = SepVec<T, '*'>; type MulVec<T> = SepVec<T, '*'>;
const HELP_MESSAGE: &str = " const HELP_MESSAGE: &str = "
USAGE: 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> { 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| { .map_err(|err| {
match err { match err {
enve::Error::NotPresent => eprintln!("The expression was not found"), enve::Error::NotPresent => eprintln!("The expression was not found"),
@ -21,16 +23,46 @@ fn main() -> Result<(), enve::Error> {
std::process::exit(0); std::process::exit(0);
}) })
.unwrap() .unwrap()
.iter() .agg();
.map(|p| {
p.iter()
.map(|m| m.iter().product::<f32>())
.reduce(|acc, v| acc - v)
.unwrap_or_default()
})
.sum::<f32>();
println!("result: {}", res); println!("result: {}", res);
Ok(()) 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()
}
}

View file

@ -4,6 +4,9 @@ mod structs;
pub use structs::{CommaVec, SemiVec}; pub use structs::{CommaVec, SemiVec};
pub use estring::core::EString; pub use estring::core::EString;
#[cfg(feature = "aggs")]
pub use estring::agg::*;
#[cfg(feature = "low-level")] #[cfg(feature = "low-level")]
pub use estring::low::*; pub use estring::low::*;
#[cfg(feature = "structs")] #[cfg(feature = "structs")]