Skip to main content

deepwoken_reqparse/util/
statmap.rs

1use std::{collections::HashMap, ops::{Deref, DerefMut}};
2
3use serde::Serialize;
4
5use crate::Stat;
6
7/// Wrapper around a HashMap of stats to their values
8#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
9pub struct StatMap(pub HashMap<Stat, i64>);
10
11impl StatMap {
12    /// Creates a new empty Stats map.
13    pub fn new() -> Self {
14        StatMap(HashMap::new())
15    }
16
17    pub fn cost(&self) -> i64 {
18        self.0.values().sum::<i64>()
19            - (self.0.iter().filter(|(s, _)| s.is_attunement()).count() as i64 - 1).max(0)
20    }
21
22    pub fn get(&self, stat: &Stat) -> i64 {
23        *self.0.get(stat).unwrap_or(&0)
24    }
25}
26
27impl Default for StatMap {
28    fn default() -> Self {
29        Self::new()
30    }
31}
32
33impl Deref for StatMap {
34    type Target = HashMap<Stat, i64>;
35
36    fn deref(&self) -> &Self::Target {
37        &self.0
38    }
39}
40
41impl DerefMut for StatMap {
42    fn deref_mut(&mut self) -> &mut Self::Target {
43        &mut self.0
44    }
45}
46
47impl From<HashMap<Stat, i64>> for StatMap {
48    fn from(map: HashMap<Stat, i64>) -> Self {
49        StatMap(map)
50    }
51}
52
53impl Into<HashMap<Stat, i64>> for StatMap {
54    fn into(self) -> HashMap<Stat, i64> {
55        self.0
56    }
57}