agg: add aggregatable trait
This commit is contained in:
parent
931b570aea
commit
c4b68acb54
6 changed files with 124 additions and 31 deletions
|
@ -1,18 +1,11 @@
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use crate::{Aggregate, EString, ParseFragment};
|
use crate::{Aggregate, Aggregateble, EString, ParseFragment};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
struct Sum<R, T>(T, PhantomData<R>)
|
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>
|
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 {
|
fn new(inner: T) -> Self {
|
||||||
Self(inner, PhantomData::default())
|
Self(inner, PhantomData::default())
|
||||||
}
|
}
|
||||||
|
@ -20,8 +13,7 @@ where
|
||||||
|
|
||||||
impl<R, T> ParseFragment for Sum<R, T>
|
impl<R, T> ParseFragment for Sum<R, T>
|
||||||
where
|
where
|
||||||
R: Copy + std::iter::Sum,
|
T: ParseFragment,
|
||||||
T: ParseFragment + std::ops::Deref<Target = Vec<R>>,
|
|
||||||
{
|
{
|
||||||
fn parse_frag(es: EString) -> crate::Result<Self> {
|
fn parse_frag(es: EString) -> crate::Result<Self> {
|
||||||
T::parse_frag(es).map(Self::new)
|
T::parse_frag(es).map(Self::new)
|
||||||
|
@ -30,13 +22,25 @@ where
|
||||||
|
|
||||||
impl<R, T> Aggregate for Sum<R, T>
|
impl<R, T> Aggregate for Sum<R, T>
|
||||||
where
|
where
|
||||||
R: Copy + std::iter::Sum,
|
R: std::iter::Sum,
|
||||||
T: ParseFragment + std::ops::Deref<Target = Vec<R>>,
|
T: Aggregateble<Item = R>,
|
||||||
{
|
{
|
||||||
type Target = R;
|
type Target = R;
|
||||||
|
|
||||||
fn agg(&self) -> Self::Target {
|
fn agg(self) -> Self::Target {
|
||||||
self.0.iter().copied().sum()
|
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::*;
|
use super::*;
|
||||||
|
|
||||||
|
type CommaVec<T> = SepVec<T, ','>;
|
||||||
|
type PlusVec<T> = SepVec<T, '+'>;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_parse_vec() {
|
fn should_parse_vec() {
|
||||||
let es = EString::from("1,2,3");
|
let es = EString::from("1,2,3");
|
||||||
match es.parse::<Sum<i32, SepVec<i32, ','>>>() {
|
match es.parse::<Sum<i32, CommaVec<i32>>>() {
|
||||||
Ok(res) => assert_eq!(res, Sum::new(SepVec::from(vec![1, 2, 3]))),
|
Ok(res) => assert_eq!(res, Sum::new(CommaVec::from(vec![1, 2, 3]))),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,16 +65,23 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn should_aggregate_vector() {
|
fn should_aggregate_vector() {
|
||||||
let es = EString::from("1,2,3");
|
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);
|
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]
|
#[test]
|
||||||
fn should_aggregate_vector_with_inner_aggregation() {
|
fn should_aggregate_vector_with_inner_aggregation() {
|
||||||
let es = EString::from("1+2,2,3");
|
let es = EString::from("1+2,2,3");
|
||||||
let expr = es
|
let expr = es
|
||||||
.parse::<Sum<_, SepVec<Sum<_, SepVec<i32, '+'>>, ','>>>()
|
.parse::<Sum<_, CommaVec<Sum<_, PlusVec<i32>>>>>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(expr.agg(), 6);
|
assert_eq!(expr.agg(), 8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
32
src/core.rs
32
src/core.rs
|
@ -120,7 +120,13 @@ pub trait Aggregate {
|
||||||
type Target: ?Sized;
|
type Target: ?Sized;
|
||||||
|
|
||||||
/// Aggregates the value.
|
/// 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.
|
/// 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 {
|
impl ParseFragment for String {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_frag(es: EString) -> crate::Result<Self> {
|
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 {
|
impl ParseFragment for &'static str {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_frag(es: EString) -> crate::Result<Self> {
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::core::{EString, ParseFragment, ToEString};
|
use crate::core::{Aggregateble, EString, ParseFragment, ToEString};
|
||||||
use crate::error::{Error, Reason};
|
use crate::error::{Error, Reason};
|
||||||
|
|
||||||
impl ParseFragment for bool {
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::core::{EString, ParseFragment, ToEString};
|
use crate::core::{Aggregateble, EString, ParseFragment, ToEString};
|
||||||
use crate::error::{Error, Reason};
|
use crate::error::{Error, Reason};
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
@ -18,6 +18,14 @@ macro_rules! from_env_string_numbers_impl {
|
||||||
EString(self.to_string())
|
EString(self.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Aggregateble for $ty {
|
||||||
|
type Item = Self;
|
||||||
|
|
||||||
|
fn items(self) -> Vec<Self::Item> {
|
||||||
|
vec![self]
|
||||||
|
}
|
||||||
|
}
|
||||||
)+
|
)+
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::core::{EString, ParseFragment, ToEString};
|
use crate::core::{Aggregateble, EString, ParseFragment, ToEString};
|
||||||
|
|
||||||
impl<T> ToEString for Option<T>
|
impl<T> ToEString for Option<T>
|
||||||
where
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
//! Contains the implementations to vec type
|
//! Contains the implementations to vec type
|
||||||
//!
|
//!
|
||||||
|
|
||||||
use crate::core::{EString, ParseFragment, ToEString};
|
use crate::core::{Aggregateble, EString, ParseFragment, ToEString};
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
/// Wrapper for ``Vec`` to split string by a separator (`SEP`).
|
/// 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::Pair;
|
use crate::Pair;
|
||||||
use crate::{Error, Reason};
|
use crate::{Error, Reason};
|
||||||
|
|
||||||
const COMMA: char = ',';
|
type CommaVec<T> = SepVec<T, ','>;
|
||||||
const SEMI: char = ';';
|
type SemiVec<T> = SepVec<T, ';'>;
|
||||||
|
|
||||||
type CommaVec<T> = SepVec<T, COMMA>;
|
|
||||||
type SemiVec<T> = SepVec<T, SEMI>;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_parse_into_vec() {
|
fn should_parse_into_vec() {
|
||||||
|
@ -171,4 +179,18 @@ d,e";
|
||||||
let vec = SepVec::<_, ','>::from(vec![PlusPair::from((1, 2)), PlusPair::from((3, 4))]);
|
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")));
|
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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue