Skip to main content

deepwoken_reqparse/util/
traits.rs

1use std::borrow::Borrow;
2
3use crate::{req::Requirement, Stat, util::statmap::StatMap};
4
5
6// Utility for dealing with a group of reqs
7pub trait ReqVecExt {
8    fn map_names<F>(&mut self, f: F) 
9    where
10        F: Fn(&str) -> String;
11}
12
13impl ReqVecExt for Vec<Requirement> {
14    fn map_names<F>(&mut self, f: F)
15    where
16        F: Fn(&str) -> String,
17    {
18        for req in self.iter_mut() {
19            req.name = req.name.as_ref().map(|name| f(&name));
20
21            req.prereqs = req.prereqs.iter().map(|name| f(&name)).collect();
22        }
23    }
24}
25
26pub trait ReqIterExt {
27    fn max_map(self) -> StatMap;
28
29    fn max_total_req(self) -> i64;
30}
31
32impl<I> ReqIterExt for I
33where
34    I: Iterator,
35    I::Item: Borrow<Requirement>, 
36{
37    fn max_map(self) -> StatMap {
38        let mut maxes: StatMap = StatMap::new();
39
40        for req in self {
41            let req = req.borrow();
42
43            for atom in req.atoms() {
44                for &stat in &atom.stats {
45                    if stat == Stat::Total {
46                        continue;
47                    }
48
49                    // TODO! we cant do a trivial per-stat max here,
50                    // bc of sum reqs.
51                    maxes
52                        .entry(stat)
53                        .and_modify(|cur| *cur = (*cur).max(atom.value))
54                        .or_insert(atom.value);
55                }
56            }
57        }
58
59        maxes
60    }
61
62    fn max_total_req(self) -> i64 {
63        let mut max: i64 = 0;
64
65        for req in self {
66            let req = req.borrow();
67
68            for atom in req.atoms() {
69                if atom.stats.contains(&Stat::Total) {
70                    max = max.max(atom.value);
71                }
72            }
73        }
74
75        max
76    }
77}