agg: add aggregatable trait

This commit is contained in:
Dmitriy Pleshevskiy 2022-07-28 12:19:56 +03:00
parent 931b570aea
commit c4b68acb54
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
6 changed files with 124 additions and 31 deletions

View File

@ -1,18 +1,11 @@
use std::marker::PhantomData;
use crate::{Aggregate, EString, ParseFragment};
use crate::{Aggregate, Aggregateble, 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>>;
struct Sum<R, T>(T, PhantomData<R>);
impl<R, T> Sum<R, T>
where
R: Copy + std::iter::Sum,
T: ParseFragment + std::ops::Deref<Target = Vec<R>>,
{
impl<R, T> Sum<R, T> {
fn new(inner: T) -> Self {
Self(inner, PhantomData::default())
}
@ -20,8 +13,7 @@ where
impl<R, T> ParseFragment for Sum<R, T>
where
R: Copy + std::iter::Sum,
T: ParseFragment + std::ops::Deref<Target = Vec<R>>,
T: ParseFragment,
{
fn parse_frag(es: EString) -> crate::Result<Self> {
T::parse_frag(es).map(Self::new)
@ -30,13 +22,25 @@ where
impl<R, T> Aggregate for Sum<R, T>
where
R: Copy + std::iter::Sum,
T: ParseFragment + std::ops::Deref<Target = Vec<R>>,
R: std::iter::Sum,
T: Aggregateble<Item = R>,
{
type Target = R;
fn agg(&self) -> Self::Target {
self.0.iter().copied().sum()
fn agg(self) -> Self::Target {
self.0.items().into_iter().sum()
}
}
impl<R, T> Aggregateble for Sum<R, T>
where
R: std::iter::Sum,
T: Aggregateble<Item = R>,
{
type Item = R;
fn items(self) -> Vec<Self::Item> {
vec![self.agg()]
}
}
@ -46,11 +50,14 @@ mod tests {
use super::*;
type CommaVec<T> = SepVec<T, ','>;
type PlusVec<T> = SepVec<T, '+'>;
#[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]))),
match es.parse::<Sum<i32, CommaVec<i32>>>() {
Ok(res) => assert_eq!(res, Sum::new(CommaVec::from(vec![1, 2, 3]))),
_ => unreachable!(),
}
}
@ -58,16 +65,23 @@ mod tests {
#[test]
fn should_aggregate_vector() {
let es = EString::from("1,2,3");
let expr = es.parse::<Sum<i32, SepVec<i32, ','>>>().unwrap();
let expr = es.parse::<Sum<i32, 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();
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<_, SepVec<Sum<_, SepVec<i32, '+'>>, ','>>>()
.parse::<Sum<_, CommaVec<Sum<_, PlusVec<i32>>>>>()
.unwrap();
assert_eq!(expr.agg(), 6);
assert_eq!(expr.agg(), 8);
}
}

View File

@ -120,7 +120,13 @@ pub trait Aggregate {
type Target: ?Sized;
/// Aggregates the value.
fn agg(&self) -> Self::Target;
fn agg(self) -> Self::Target;
}
pub trait Aggregateble {
type Item;
fn items(self) -> Vec<Self::Item>;
}
/// Wrapper under ``String`` type.
@ -230,6 +236,14 @@ impl ParseFragment for EString {
}
}
impl Aggregateble for EString {
type Item = Self;
fn items(self) -> Vec<Self::Item> {
vec![self]
}
}
impl ParseFragment for String {
#[inline]
fn parse_frag(es: EString) -> crate::Result<Self> {
@ -244,6 +258,14 @@ impl ToEString for String {
}
}
impl Aggregateble for String {
type Item = Self;
fn items(self) -> Vec<Self::Item> {
vec![self]
}
}
impl ParseFragment for &'static str {
#[inline]
fn parse_frag(es: EString) -> crate::Result<Self> {
@ -258,6 +280,14 @@ impl<'a> ToEString for &'a str {
}
}
impl<'a> Aggregateble for &'a str {
type Item = Self;
fn items(self) -> Vec<Self::Item> {
vec![self]
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -1,4 +1,4 @@
use crate::core::{EString, ParseFragment, ToEString};
use crate::core::{Aggregateble, EString, ParseFragment, ToEString};
use crate::error::{Error, Reason};
impl ParseFragment for bool {
@ -19,6 +19,14 @@ impl ToEString for bool {
}
}
impl Aggregateble for bool {
type Item = Self;
fn items(self) -> Vec<Self::Item> {
vec![self]
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -1,4 +1,4 @@
use crate::core::{EString, ParseFragment, ToEString};
use crate::core::{Aggregateble, EString, ParseFragment, ToEString};
use crate::error::{Error, Reason};
#[doc(hidden)]
@ -18,6 +18,14 @@ macro_rules! from_env_string_numbers_impl {
EString(self.to_string())
}
}
impl Aggregateble for $ty {
type Item = Self;
fn items(self) -> Vec<Self::Item> {
vec![self]
}
}
)+
};
}

View File

@ -1,4 +1,4 @@
use crate::core::{EString, ParseFragment, ToEString};
use crate::core::{Aggregateble, EString, ParseFragment, ToEString};
impl<T> ToEString for Option<T>
where
@ -25,6 +25,17 @@ where
}
}
impl<T> Aggregateble for Option<T>
where
T: Aggregateble,
{
type Item = T::Item;
fn items(self) -> Vec<Self::Item> {
self.map(T::items).unwrap_or_default()
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -1,7 +1,7 @@
//! Contains the implementations to vec type
//!
use crate::core::{EString, ParseFragment, ToEString};
use crate::core::{Aggregateble, EString, ParseFragment, ToEString};
use std::fmt::Write;
/// Wrapper for ``Vec`` to split string by a separator (`SEP`).
@ -92,17 +92,25 @@ where
}
}
impl<T, const SEP: char> Aggregateble for SepVec<T, SEP>
where
T: Aggregateble,
{
type Item = T::Item;
fn items(self) -> Vec<Self::Item> {
self.0.into_iter().flat_map(T::items).collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Pair;
use crate::{Error, Reason};
const COMMA: char = ',';
const SEMI: char = ';';
type CommaVec<T> = SepVec<T, COMMA>;
type SemiVec<T> = SepVec<T, SEMI>;
type CommaVec<T> = SepVec<T, ','>;
type SemiVec<T> = SepVec<T, ';'>;
#[test]
fn should_parse_into_vec() {
@ -171,4 +179,18 @@ d,e";
let vec = SepVec::<_, ','>::from(vec![PlusPair::from((1, 2)), PlusPair::from((3, 4))]);
assert_eq!(vec.to_estring(), EString(String::from("1+2,3+4")));
}
#[test]
fn should_returns_aggregatable_items() {
let estr = EString::from("1,2,3,4,5");
let res = estr.parse::<CommaVec<i32>>().unwrap();
assert_eq!(res.items(), vec![1, 2, 3, 4, 5]);
}
#[test]
fn should_returns_flatten_aggregatable_items() {
let estr = EString::from("1,2; 3,4,5; 6,7");
let res = estr.parse::<SemiVec<CommaVec<i32>>>().unwrap();
assert_eq!(res.items(), vec![1, 2, 3, 4, 5, 6, 7]);
}
}