agg: add draft logic for aggregation
This commit is contained in:
parent
7d215c876f
commit
b13a510fb7
4 changed files with 93 additions and 2 deletions
5
Cargo.lock
generated
5
Cargo.lock
generated
|
@ -1,6 +1,7 @@
|
||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "estring"
|
name = "estring"
|
||||||
version = "0.1.2"
|
version = "0.2.1"
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,6 @@
|
||||||
//! This module will contain aggregate functions (Sum, Product, etc)
|
//! This module will contain aggregate functions (Sum, Product, etc)
|
||||||
//!
|
//!
|
||||||
|
|
||||||
|
mod sum;
|
||||||
|
|
||||||
|
pub use sum::*;
|
||||||
|
|
73
src/agg/sum.rs
Normal file
73
src/agg/sum.rs
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use crate::{Aggregate, EString, ParseFragment};
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
struct Sum<R, T>(T, PhantomData<R>)
|
||||||
|
where
|
||||||
|
R: Copy + std::iter::Sum,
|
||||||
|
T: ParseFragment + std::ops::Deref<Target = Vec<R>>;
|
||||||
|
|
||||||
|
impl<R, T> Sum<R, T>
|
||||||
|
where
|
||||||
|
R: Copy + std::iter::Sum,
|
||||||
|
T: ParseFragment + std::ops::Deref<Target = Vec<R>>,
|
||||||
|
{
|
||||||
|
fn new(inner: T) -> Self {
|
||||||
|
Self(inner, PhantomData::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, T> ParseFragment for Sum<R, T>
|
||||||
|
where
|
||||||
|
R: Copy + std::iter::Sum,
|
||||||
|
T: ParseFragment + std::ops::Deref<Target = Vec<R>>,
|
||||||
|
{
|
||||||
|
fn parse_frag(es: EString) -> crate::Result<Self> {
|
||||||
|
T::parse_frag(es).map(Self::new)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, T> Aggregate for Sum<R, T>
|
||||||
|
where
|
||||||
|
R: Copy + std::iter::Sum,
|
||||||
|
T: ParseFragment + std::ops::Deref<Target = Vec<R>>,
|
||||||
|
{
|
||||||
|
type Target = R;
|
||||||
|
|
||||||
|
fn agg(&self) -> Self::Target {
|
||||||
|
self.0.iter().copied().sum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::SepVec;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_parse_vec() {
|
||||||
|
let es = EString::from("1,2,3");
|
||||||
|
match es.parse::<Sum<i32, SepVec<i32, ','>>>() {
|
||||||
|
Ok(res) => assert_eq!(res, Sum::new(SepVec::from(vec![1, 2, 3]))),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_aggregate_vector() {
|
||||||
|
let es = EString::from("1,2,3");
|
||||||
|
let expr = es.parse::<Sum<i32, SepVec<i32, ','>>>().unwrap();
|
||||||
|
assert_eq!(expr.agg(), 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_aggregate_vector_with_inner_aggregation() {
|
||||||
|
let es = EString::from("1+2,2,3");
|
||||||
|
let expr = es
|
||||||
|
.parse::<Sum<_, SepVec<Sum<_, SepVec<i32, '+'>>, ','>>>()
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(expr.agg(), 6);
|
||||||
|
}
|
||||||
|
}
|
13
src/core.rs
13
src/core.rs
|
@ -110,6 +110,19 @@ pub trait ParseFragment: Sized {
|
||||||
fn parse_frag(es: EString) -> crate::Result<Self>;
|
fn parse_frag(es: EString) -> crate::Result<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: add example
|
||||||
|
/// Trait to represent structures that can act as aggregators.
|
||||||
|
///
|
||||||
|
/// The `agg` method works with data that has already been parsed. For this reason, **this trait
|
||||||
|
/// should never fail**.
|
||||||
|
pub trait Aggregate {
|
||||||
|
/// The resulting type after aggregation.
|
||||||
|
type Target: ?Sized;
|
||||||
|
|
||||||
|
/// Aggregates the value.
|
||||||
|
fn agg(&self) -> Self::Target;
|
||||||
|
}
|
||||||
|
|
||||||
/// Wrapper under ``String`` type.
|
/// Wrapper under ``String`` type.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
|
Reference in a new issue