Compare commits
3 commits
Author | SHA1 | Date | |
---|---|---|---|
51f39b6793 | |||
c4b68acb54 | |||
931b570aea |
8 changed files with 204 additions and 11 deletions
5
Cargo.lock
generated
5
Cargo.lock
generated
|
@ -1,6 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "estring"
|
||||
version = "0.1.2"
|
||||
|
||||
version = "0.2.1"
|
||||
|
|
|
@ -1,2 +1,6 @@
|
|||
//! This module will contain aggregate functions (Sum, Product, etc)
|
||||
//!
|
||||
|
||||
mod sum;
|
||||
|
||||
pub use sum::*;
|
||||
|
|
87
src/agg/sum.rs
Normal file
87
src/agg/sum.rs
Normal file
|
@ -0,0 +1,87 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use crate::{Aggregatable, Aggregate, EString, ParseFragment};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct Sum<R, T>(T, PhantomData<R>);
|
||||
|
||||
impl<R, T> Sum<R, T> {
|
||||
fn new(inner: T) -> Self {
|
||||
Self(inner, PhantomData::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<R, T> ParseFragment for Sum<R, T>
|
||||
where
|
||||
T: ParseFragment,
|
||||
{
|
||||
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: std::iter::Sum,
|
||||
T: Aggregatable<Item = R>,
|
||||
{
|
||||
type Target = R;
|
||||
|
||||
fn agg(self) -> Self::Target {
|
||||
self.0.items().into_iter().sum()
|
||||
}
|
||||
}
|
||||
|
||||
impl<R, T> Aggregatable for Sum<R, T>
|
||||
where
|
||||
R: std::iter::Sum,
|
||||
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 PlusVec<T> = SepVec<T, '+'>;
|
||||
|
||||
#[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]))),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_aggregate_vector() {
|
||||
let es = EString::from("1,2,3");
|
||||
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<_, CommaVec<Sum<_, PlusVec<i32>>>>>()
|
||||
.unwrap();
|
||||
assert_eq!(expr.agg(), 8);
|
||||
}
|
||||
}
|
50
src/core.rs
50
src/core.rs
|
@ -110,6 +110,29 @@ pub trait ParseFragment: Sized {
|
|||
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;
|
||||
}
|
||||
|
||||
// TODO: add example
|
||||
/// Trait to represent structures that can iterate values for the aggregator.
|
||||
pub trait Aggregatable {
|
||||
/// The type of the elements being iterated over.
|
||||
type Item;
|
||||
|
||||
/// Returns Vec of aggregatable values
|
||||
fn items(self) -> Vec<Self::Item>;
|
||||
}
|
||||
|
||||
/// Wrapper under ``String`` type.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -217,6 +240,15 @@ impl ParseFragment for EString {
|
|||
}
|
||||
}
|
||||
|
||||
impl Aggregatable for EString {
|
||||
type Item = Self;
|
||||
|
||||
#[inline]
|
||||
fn items(self) -> Vec<Self::Item> {
|
||||
vec![self]
|
||||
}
|
||||
}
|
||||
|
||||
impl ParseFragment for String {
|
||||
#[inline]
|
||||
fn parse_frag(es: EString) -> crate::Result<Self> {
|
||||
|
@ -231,6 +263,15 @@ impl ToEString for String {
|
|||
}
|
||||
}
|
||||
|
||||
impl Aggregatable for String {
|
||||
type Item = Self;
|
||||
|
||||
#[inline]
|
||||
fn items(self) -> Vec<Self::Item> {
|
||||
vec![self]
|
||||
}
|
||||
}
|
||||
|
||||
impl ParseFragment for &'static str {
|
||||
#[inline]
|
||||
fn parse_frag(es: EString) -> crate::Result<Self> {
|
||||
|
@ -245,6 +286,15 @@ impl<'a> ToEString for &'a str {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Aggregatable for &'a str {
|
||||
type Item = Self;
|
||||
|
||||
#[inline]
|
||||
fn items(self) -> Vec<Self::Item> {
|
||||
vec![self]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::core::{EString, ParseFragment, ToEString};
|
||||
use crate::core::{Aggregatable, EString, ParseFragment, ToEString};
|
||||
use crate::error::{Error, Reason};
|
||||
|
||||
impl ParseFragment for bool {
|
||||
|
@ -19,6 +19,15 @@ impl ToEString for bool {
|
|||
}
|
||||
}
|
||||
|
||||
impl Aggregatable for bool {
|
||||
type Item = Self;
|
||||
|
||||
#[inline]
|
||||
fn items(self) -> Vec<Self::Item> {
|
||||
vec![self]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::core::{EString, ParseFragment, ToEString};
|
||||
use crate::core::{Aggregatable, EString, ParseFragment, ToEString};
|
||||
use crate::error::{Error, Reason};
|
||||
|
||||
#[doc(hidden)]
|
||||
|
@ -18,6 +18,15 @@ macro_rules! from_env_string_numbers_impl {
|
|||
EString(self.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl Aggregatable for $ty {
|
||||
type Item = Self;
|
||||
|
||||
#[inline]
|
||||
fn items(self) -> Vec<Self::Item> {
|
||||
vec![self]
|
||||
}
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::core::{EString, ParseFragment, ToEString};
|
||||
use crate::core::{Aggregatable, EString, ParseFragment, ToEString};
|
||||
|
||||
impl<T> ToEString for Option<T>
|
||||
where
|
||||
|
@ -25,6 +25,17 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Aggregatable for Option<T>
|
||||
where
|
||||
T: Aggregatable,
|
||||
{
|
||||
type Item = T::Item;
|
||||
|
||||
fn items(self) -> Vec<Self::Item> {
|
||||
self.map(T::items).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//! Contains the implementations to vec type
|
||||
//!
|
||||
|
||||
use crate::core::{EString, ParseFragment, ToEString};
|
||||
use crate::core::{Aggregatable, 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> Aggregatable for SepVec<T, SEP>
|
||||
where
|
||||
T: Aggregatable,
|
||||
{
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue