[][src]Function mmap_json_file::sum_over_field

pub fn sum_over_field<'a, T: 'static, F, U>(f: String, filter: F) -> Result<U> where
    T: DeserializeOwned + Debug + Clone + Send + Serialize,
    F: Fn(T) -> U,
    U: Default + AddAssign

Sum values of a json field over the entire file.

  • f Filename and full accesislbe path of the input json file
  • filter A closure that can handle an input parameter of type T and provide the field with distincts.

Input types

  • T The type of the structure.
  • F Closure template with input function type.
  • U The type of the result. ( depending on the type of the field being summed over.)

Return

  • sum Total sum.

Example usage

use mmap_json_file;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
struct TestSimple {
    a: Option<String>,
    c: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
struct TestSimpleNested {
    b: Option<TestSimple>,
    c: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
struct TestSimpleCompound {
    a: Option<TestSimpleNested>,
    f: Option<String>,
}

let filter = |record: TestSimple| -> f64 {
   match record.a {
       Some(value) => {
           match value.parse::<f64>() {
               Ok(num) => num as f64,
               _ => 0f64
           }
       },
       _ => 0f64
   }
};

let _res = mmap_json_file::sum_over_field::<TestSimple, Box<dyn Fn(TestSimple) -> f64>, f64>(
   "data/test_simple_sum.json".to_string(),
   Box::new(filter)
);