Skip to main content

main/
main.rs

1use cstea::fill::{FillCsvArg, FillCsTea};
2use cstea::pour::{PourCsvArg, PourCsTea};
3use rettle::{
4    Brewery,
5    Pot,
6    Argument,
7    Steep,
8};
9
10use std::any::Any;
11use serde::{Deserialize, Serialize};
12
13#[derive(Default, Clone, Debug, Deserialize, Serialize)]
14struct CsTea {
15    id: i32,
16    name: String,
17    value: i32
18}
19
20pub struct SteepArgs {
21    pub increment: i32,
22}
23
24impl Argument for SteepArgs {
25    fn as_any(&self) -> &dyn Any {
26        self
27    }
28}
29
30fn main() {
31    let test_fill_csvarg = FillCsvArg::new("fixtures/fill.csv", 50);
32    let test_pour_csvarg = PourCsvArg::new("fixtures/pour.csv");
33    let steep_args = SteepArgs { increment: 10000 };
34
35    let brewery = Brewery::new(4);
36    let mut new_pot = Pot::new();
37    let fill_cstea = FillCsTea::new::<CsTea>("csv_tea_source", "csv_fixture", test_fill_csvarg);
38    let pour_cstea = PourCsTea::new::<CsTea>("csv_pour_test", test_pour_csvarg);
39
40    new_pot = new_pot.add_source(fill_cstea);
41
42    // Add ingredients to pot
43    new_pot = new_pot.add_ingredient(Box::new(Steep{
44        name: String::from("steep1"),
45        computation: Box::new(|tea_batch: Vec<CsTea>, args| {
46            tea_batch
47                .into_iter()
48                .map(|mut tea| {
49                    match args {
50                        None => panic!("No params passed, not editing object!"),
51                        Some(box_args) => {
52                            let box_args = box_args.as_any().downcast_ref::<SteepArgs>().unwrap();
53                            tea.value = tea.value - box_args.increment;
54                        }
55                    }
56                    tea
57                })
58                .collect()
59        }),
60        params: Some(Box::new(steep_args)),
61    }));
62
63    new_pot = new_pot.add_ingredient(pour_cstea);
64
65    new_pot.brew(&brewery);
66}