summaryrefslogtreecommitdiffstatshomepage
path: root/src/types.rs
blob: 1d5d369d410b6715677420bee40b1eaad17153d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiceResult {
    pub expression: String,
    pub total: i32,
    pub rolls: Vec<Vec<i32>>,
    pub details: String,
    pub comment: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct DiceRoll {
    pub count: i32,
    pub sides: i32,
    pub modifiers: Vec<DiceModifier>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum DiceModifier {
    Explode,           // !
    ExplodeAlias,      // e (alias of !)
    ExplodeKeepHigh(i32), // KX == explode then keep high X
    Reroll(i32),       // rX
    RerollOnce(i32),   // roX
    RerollUntil(i32),  // RX (until > X; with cap)
    RerollAndAdd(i32), // aX (reroll if <= X and add)
    KeepAlias(i32),    // kX == khX
    KeepHigh(i32),     // khX
    KeepLow(i32),      // klX
    DropHigh(i32),     // dhX
    DropLow(i32),      // dlX
    Unique,            // u
    Sort,              // s (sort results)
    Count(i32),        // cV (count value V)
}


#[derive(Debug, Clone)]
pub enum Expression {
    Number(i32),
    DiceRoll(DiceRoll),
    Add(Box<Expression>, Box<Expression>),
    Subtract(Box<Expression>, Box<Expression>),
    Multiply(Box<Expression>, Box<Expression>),
    Divide(Box<Expression>, Box<Expression>),
    Power(Box<Expression>, Box<Expression>),
    Paren(Box<Expression>),
    WithComment(Box<Expression>, Option<String>),
}

// TODO: Variable storage
#[derive(Debug, Clone, Default)]
pub struct VariableStore {
    pub variables: HashMap<String, i32>,
}

impl VariableStore {
    pub fn new() -> Self {
        Self {
            variables: HashMap::new(),
        }
    }
    
    pub fn set(&mut self, name: &str, value: i32) {
        self.variables.insert(name.to_string(), value);
    }
    
    pub fn get(&self, name: &str) -> Option<i32> {
        self.variables.get(name).copied()
    }
}