Compare commits

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

1 commit

Author SHA1 Message Date
2090bfe965
agg: add product struct 2022-07-28 13:09:09 +03:00
5 changed files with 121 additions and 26 deletions

View file

@ -28,7 +28,7 @@ maintenance = { status = "actively-developed" }
[[example]]
name = "calc"
required-features = ["structs"]
required-features = ["structs", "aggs"]
[[example]]
name = "dotenv"

View file

@ -1,14 +1,12 @@
use estring::{EString, SepVec};
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::<PlusVec<MulVec<f32>>>()?
.iter()
.map(|m| m.iter().product::<f32>())
.sum::<f32>();
.parse::<Sum<PlusVec<Product<MulVec<f32>>>>>()?
.agg();
assert_eq!(res, 23.0);
Ok(())

View file

@ -1,6 +1,8 @@
//! This module will contain aggregate functions (Sum, Product, etc)
//!
mod product;
mod sum;
pub use product::*;
pub use sum::*;

92
src/agg/product.rs Normal file
View file

@ -0,0 +1,92 @@
use crate::{Aggregatable, Aggregate, EString, ParseFragment};
/// Aggregate struct, that can multiply inner aggregatable [items](Aggregatable::items) if
/// [``Aggregatable::Item``] implements [``std::iter::Product``](std::iter::Product)
///
/// # Examples
///
/// ```rust
/// use estring::{Aggregate, EString, SepVec, Product};
/// let res = EString::from("1*2*3*4")
/// .parse::<Product<SepVec<i32, '*'>>>()
/// .unwrap()
/// .agg();
/// assert_eq!(res, 24);
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct Product<T>(pub T);
impl<T> ParseFragment for Product<T>
where
T: ParseFragment,
{
fn parse_frag(es: EString) -> crate::Result<Self> {
T::parse_frag(es).map(Self)
}
}
impl<R, T> Aggregate for Product<T>
where
R: std::iter::Product,
T: Aggregatable<Item = R>,
{
type Target = R;
fn agg(self) -> Self::Target {
self.0.items().into_iter().product()
}
}
impl<R, T> Aggregatable for Product<T>
where
R: std::iter::Product,
T: Aggregatable<Item = R>,
{
type Item = R;
fn items(self) -> Vec<Self::Item> {
vec![self.agg()]
}
}
#[cfg(test)]
mod tests {
use crate::SepVec;
use super::*;
type CommaVec<T> = SepVec<T, ','>;
type MulVec<T> = SepVec<T, '*'>;
#[test]
fn should_parse_vec() {
let es = EString::from("1,2,3");
match es.parse::<Product<CommaVec<i32>>>() {
Ok(res) => assert_eq!(res, Product(CommaVec::from(vec![1, 2, 3]))),
_ => unreachable!(),
}
}
#[test]
fn should_aggregate_vector() {
let es = EString::from("1,2,3");
let expr = es.parse::<Product<CommaVec<i32>>>().unwrap();
assert_eq!(expr.agg(), 6);
}
#[test]
fn should_aggregate_vector_with_inner_vector() {
let es = EString::from("1*2,2,3");
let expr = es.parse::<Product<CommaVec<MulVec<i32>>>>().unwrap();
assert_eq!(expr.agg(), 12);
}
#[test]
fn should_aggregate_vector_with_inner_aggregation() {
let es = EString::from("1*2,2,3");
let expr = es
.parse::<Product<CommaVec<Product<MulVec<i32>>>>>()
.unwrap();
assert_eq!(expr.agg(), 12);
}
}

View file

@ -1,26 +1,31 @@
use std::marker::PhantomData;
use crate::{Aggregatable, Aggregate, EString, ParseFragment};
/// Aggregate struct, that can sum inner aggregatable [items](Aggregatable::items) if
/// [``Aggregatable::Item``] implements [``std::iter::Sum``](std::iter::Sum)
///
/// # Examples
///
/// ```rust
/// use estring::{Aggregate, EString, SepVec, Sum};
/// let res = EString::from("1+2+3+4")
/// .parse::<Sum<SepVec<i32, '+'>>>()
/// .unwrap()
/// .agg();
/// assert_eq!(res, 10);
/// ```
#[derive(Debug, PartialEq, Eq)]
struct Sum<R, T>(T, PhantomData<R>);
pub struct Sum<T>(pub T);
impl<R, T> Sum<R, T> {
fn new(inner: T) -> Self {
Self(inner, PhantomData::default())
}
}
impl<R, T> ParseFragment for Sum<R, T>
impl<T> ParseFragment for Sum<T>
where
T: ParseFragment,
{
fn parse_frag(es: EString) -> crate::Result<Self> {
T::parse_frag(es).map(Self::new)
T::parse_frag(es).map(Self)
}
}
impl<R, T> Aggregate for Sum<R, T>
impl<R, T> Aggregate for Sum<T>
where
R: std::iter::Sum,
T: Aggregatable<Item = R>,
@ -32,7 +37,7 @@ where
}
}
impl<R, T> Aggregatable for Sum<R, T>
impl<R, T> Aggregatable for Sum<T>
where
R: std::iter::Sum,
T: Aggregatable<Item = R>,
@ -56,8 +61,8 @@ mod tests {
#[test]
fn should_parse_vec() {
let es = EString::from("1,2,3");
match es.parse::<Sum<i32, CommaVec<i32>>>() {
Ok(res) => assert_eq!(res, Sum::new(CommaVec::from(vec![1, 2, 3]))),
match es.parse::<Sum<CommaVec<i32>>>() {
Ok(res) => assert_eq!(res, Sum(CommaVec::from(vec![1, 2, 3]))),
_ => unreachable!(),
}
}
@ -65,23 +70,21 @@ mod tests {
#[test]
fn should_aggregate_vector() {
let es = EString::from("1,2,3");
let expr = es.parse::<Sum<i32, CommaVec<i32>>>().unwrap();
let expr = es.parse::<Sum<CommaVec<i32>>>().unwrap();
assert_eq!(expr.agg(), 6);
}
#[test]
fn should_aggregate_vector_with_inner_vector() {
let es = EString::from("1+2,2,3");
let expr = es.parse::<Sum<i32, CommaVec<PlusVec<i32>>>>().unwrap();
let expr = es.parse::<Sum<CommaVec<PlusVec<i32>>>>().unwrap();
assert_eq!(expr.agg(), 8);
}
#[test]
fn should_aggregate_vector_with_inner_aggregation() {
let es = EString::from("1+2,2,3");
let expr = es
.parse::<Sum<_, CommaVec<Sum<_, PlusVec<i32>>>>>()
.unwrap();
let expr = es.parse::<Sum<CommaVec<Sum<PlusVec<i32>>>>>().unwrap();
assert_eq!(expr.agg(), 8);
}
}