1
use std::fmt::{Debug, Formatter};
2
use std::sync::{Arc, RwLock};
3

            
4
use derivative::Derivative;
5
use schemars::JsonSchema;
6
use serde::Serialize;
7
use serde_with::skip_serializing_none;
8

            
9
use crate::rule_engine::{Rule, RuleSet};
10
use crate::solver::SolverFamily;
11
use crate::stats::Stats;
12

            
13
#[skip_serializing_none]
14
#[derive(Clone, Serialize, Default, Derivative, JsonSchema)]
15
#[serde(rename_all = "camelCase")]
16
#[derivative(Eq, PartialEq)]
17
#[non_exhaustive]
18
pub struct Context<'a> {
19
    pub target_solver_family: Option<SolverFamily>,
20

            
21
    pub file_name: Option<String>,
22

            
23
    pub extra_rule_set_names: Vec<String>,
24

            
25
    #[serde(skip)]
26
    pub rules: Vec<&'a Rule<'a>>,
27

            
28
    #[serde(skip)]
29
    pub rule_sets: Vec<&'a RuleSet<'a>>,
30

            
31
    #[derivative(PartialEq = "ignore")]
32
    pub stats: Stats,
33
}
34

            
35
impl<'a> Context<'a> {
36
    pub fn new(
37
        target_solver_family: SolverFamily,
38
        extra_rule_set_names: Vec<String>,
39
        rules: Vec<&'a Rule<'a>>,
40
        rule_sets: Vec<&'a RuleSet<'a>>,
41
    ) -> Self {
42
        Context {
43
            target_solver_family: Some(target_solver_family),
44
            extra_rule_set_names,
45
            rules,
46
            rule_sets,
47
            stats: Default::default(),
48
            ..Default::default()
49
        }
50
    }
51
}
52

            
53
impl Context<'static> {
54
    pub fn new_ptr(
55
        target_solver_family: SolverFamily,
56
        extra_rule_set_names: Vec<String>,
57
        rules: Vec<&'static Rule<'static>>,
58
        rule_sets: Vec<&'static RuleSet<'static>>,
59
    ) -> Arc<RwLock<Context<'static>>> {
60
        Arc::new(RwLock::new(Context::new(
61
            target_solver_family,
62
            extra_rule_set_names,
63
            rules,
64
            rule_sets,
65
        )))
66
    }
67
}
68

            
69
impl Debug for Context<'_> {
70
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
71
        let target_solver_family: Option<SolverFamily> = self.target_solver_family;
72
        let extra_rule_set_names: Vec<String> = self.extra_rule_set_names.clone();
73
        let rules: Vec<&str> = self.rules.iter().map(|r| r.name).collect();
74
        let rule_sets: Vec<&str> = self.rule_sets.iter().map(|r| r.name).collect();
75

            
76
        write!(
77
            f,
78
            "Context {{\n\
79
            \ttarget_solver_family: {:?}\n\
80
            \textra_rule_set_names: {:?}\n\
81
            \trules: {:?}\n\
82
            \trule_sets: {:?}\n\
83
        }}",
84
            target_solver_family, extra_rule_set_names, rules, rule_sets
85
        )
86
    }
87
}