1
use std::collections::{HashMap, HashSet};
2
use std::fmt::Display;
3

            
4
use thiserror::Error;
5

            
6
use crate::rule_engine::{get_rule_set_by_name, get_rule_sets_for_solver_family, Rule, RuleSet};
7
use crate::solver::SolverFamily;
8

            
9
#[derive(Debug, Error)]
10
pub enum ResolveRulesError {
11
    RuleSetNotFound,
12
}
13

            
14
impl Display for ResolveRulesError {
15
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16
        match self {
17
            ResolveRulesError::RuleSetNotFound => write!(f, "Rule set not found."),
18
        }
19
    }
20
}
21

            
22
/// Helper function to get a rule set by name, or return an error if it doesn't exist.
23
///
24
/// # Arguments
25
/// - `rule_set_name` The name of the rule set to get.
26
///
27
/// # Returns
28
/// - The rule set with the given name or `RuleSetError::RuleSetNotFound` if it doesn't exist.
29
1635
fn get_rule_set(rule_set_name: &str) -> Result<&'static RuleSet<'static>, ResolveRulesError> {
30
1635
    match get_rule_set_by_name(rule_set_name) {
31
1635
        Some(rule_set) => Ok(rule_set),
32
        None => Err(ResolveRulesError::RuleSetNotFound),
33
    }
34
1635
}
35

            
36
/// Resolve a list of rule sets (and dependencies) by their names
37
///
38
/// # Arguments
39
/// - `rule_set_names` The names of the rule sets to resolve.
40
///
41
/// # Returns
42
/// - A list of the given rule sets and all of their dependencies, or error
43
///
44
#[allow(clippy::mutable_key_type)] // RuleSet is 'static so it's fine
45
585
pub fn rule_sets_by_names<'a>(
46
585
    rule_set_names: &Vec<String>,
47
585
) -> Result<HashSet<&'a RuleSet<'static>>, ResolveRulesError> {
48
585
    let mut rs_set: HashSet<&'static RuleSet<'static>> = HashSet::new();
49

            
50
2220
    for rule_set_name in rule_set_names {
51
1635
        let rule_set = get_rule_set(rule_set_name)?;
52
1635
        let new_dependencies = rule_set.get_dependencies();
53
1635
        rs_set.insert(rule_set);
54
1635
        rs_set.extend(new_dependencies);
55
    }
56

            
57
585
    Ok(rs_set)
58
585
}
59

            
60
/// Resolves the final set of rule sets to apply based on target solver and extra rule set names.
61
///
62
/// # Arguments
63
/// - `target_solver` The solver to resolve the rule sets for.
64
/// - `extra_rs_names` The names of the extra rule sets to use
65
///
66
/// # Returns
67
/// - A vector of rule sets to apply.
68
///
69
#[allow(clippy::mutable_key_type)] // RuleSet is 'static so it's fine
70
585
pub fn resolve_rule_sets<'a>(
71
585
    target_solver: SolverFamily,
72
585
    extra_rs_names: &Vec<String>,
73
585
) -> Result<Vec<&'a RuleSet<'static>>, ResolveRulesError> {
74
585
    let mut ans = HashSet::new();
75

            
76
585
    for rs in get_rule_sets_for_solver_family(target_solver) {
77
585
        ans.extend(rs.with_dependencies());
78
585
    }
79

            
80
585
    ans.extend(rule_sets_by_names(extra_rs_names)?);
81
585
    Ok(ans.iter().cloned().collect())
82
585
}
83

            
84
/// Convert a list of rule sets into a final map of rules to their priorities.
85
///
86
/// # Arguments
87
/// - `rule_sets` The rule sets to get the rules from.
88
/// # Returns
89
/// - A map of rules to their priorities.
90
675
pub fn get_rule_priorities<'a>(
91
675
    rule_sets: &Vec<&'a RuleSet<'a>>,
92
675
) -> Result<HashMap<&'a Rule<'a>, u16>, ResolveRulesError> {
93
675
    let mut rule_priorities: HashMap<&'a Rule<'a>, (&'a RuleSet<'a>, u16)> = HashMap::new();
94

            
95
3225
    for rs in rule_sets {
96
23850
        for (rule, priority) in rs.get_rules() {
97
23850
            if let Some((old_rs, _)) = rule_priorities.get(rule) {
98
                if rs.order >= old_rs.order {
99
                    rule_priorities.insert(rule, (rs, *priority));
100
                }
101
23850
            } else {
102
23850
                rule_priorities.insert(rule, (rs, *priority));
103
23850
            }
104
        }
105
    }
106

            
107
675
    let mut ans: HashMap<&'a Rule<'a>, u16> = HashMap::new();
108
24525
    for (rule, (_, priority)) in rule_priorities {
109
23850
        ans.insert(rule, priority);
110
23850
    }
111

            
112
675
    Ok(ans)
113
675
}
114

            
115
/// Compare two rules by their priorities and names.
116
///
117
/// Takes the rules and a map of rules to their priorities.
118
/// If rules are not in the map, they are assumed to have priority 0.
119
/// If the rules have the same priority, they are compared by their names.
120
///
121
/// # Arguments
122
/// - `a` first rule to compare.
123
/// - `b` second rule to compare.
124
/// - `rule_priorities` The priorities of the rules.
125
///
126
/// # Returns
127
/// - The ordering of the two rules.
128
141270
pub fn rule_cmp<'a>(
129
141270
    a: &Rule<'a>,
130
141270
    b: &Rule<'a>,
131
141270
    rule_priorities: &HashMap<&'a Rule<'a>, u16>,
132
141270
) -> std::cmp::Ordering {
133
141270
    let a_priority = *rule_priorities.get(a).unwrap_or(&0);
134
141270
    let b_priority = *rule_priorities.get(b).unwrap_or(&0);
135
141270

            
136
141270
    if a_priority == b_priority {
137
36570
        return a.name.cmp(b.name);
138
104700
    }
139
104700

            
140
104700
    b_priority.cmp(&a_priority)
141
141270
}
142

            
143
/// Get a final ordering of rules based on their priorities and names.
144
///
145
/// # Arguments
146
/// - `rule_priorities` The priorities of the rules.
147
///
148
/// # Returns
149
/// - A list of rules sorted by their priorities and names.
150
675
pub fn get_rules_vec<'a>(rule_priorities: &HashMap<&'a Rule<'a>, u16>) -> Vec<&'a Rule<'a>> {
151
675
    let mut rules: Vec<&'a Rule<'a>> = rule_priorities.keys().copied().collect();
152
141270
    rules.sort_by(|a, b| rule_cmp(a, b, rule_priorities));
153
675
    rules
154
675
}