1
#![allow(unused)]
2

            
3
use conjure_cp::settings::{Parser, QuantifiedExpander, Rewriter, SolverFamily};
4
use serde::Deserialize;
5
use std::str::FromStr;
6

            
7
3849
fn parse_values<T>(values: &[String]) -> Result<Vec<T>, String>
8
3849
where
9
3849
    T: FromStr<Err = String>,
10
{
11
6501
    values.iter().map(|value| value.parse()).collect()
12
3849
}
13

            
14
6368
fn deserialize_string_or_vec<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
15
6368
where
16
6368
    D: serde::Deserializer<'de>,
17
{
18
    #[derive(Deserialize)]
19
    #[serde(untagged)]
20
    enum StringOrVec {
21
        String(String),
22
        Vec(Vec<String>),
23
    }
24

            
25
6368
    Ok(match StringOrVec::deserialize(deserializer)? {
26
37
        StringOrVec::String(s) => vec![s],
27
6331
        StringOrVec::Vec(v) => v,
28
    })
29
6368
}
30

            
31
2559
fn default_true() -> bool {
32
2559
    true
33
2559
}
34

            
35
5615
fn default_minion_discrete_threshold() -> usize {
36
5615
    conjure_cp::settings::DEFAULT_MINION_DISCRETE_THRESHOLD
37
5615
}
38

            
39
#[derive(Deserialize, Debug)]
40
#[serde(default)]
41
#[serde(deny_unknown_fields)]
42
pub struct TestConfig {
43
    #[serde(
44
        default,
45
        rename = "parser",
46
        deserialize_with = "deserialize_string_or_vec"
47
    )]
48
    pub parser: Vec<String>, // Stage 1a: list of parsers (tree-sitter or via-conjure)
49

            
50
    #[serde(
51
        default,
52
        rename = "rewriter",
53
        deserialize_with = "deserialize_string_or_vec"
54
    )]
55
    pub rewriter: Vec<String>,
56
    #[serde(
57
        default,
58
        rename = "comprehension-expander",
59
        deserialize_with = "deserialize_string_or_vec"
60
    )]
61
    pub comprehension_expander: Vec<String>,
62
    #[serde(
63
        default,
64
        rename = "solver",
65
        deserialize_with = "deserialize_string_or_vec"
66
    )]
67
    pub solver: Vec<String>,
68

            
69
    #[serde(
70
        default = "default_minion_discrete_threshold",
71
        rename = "minion-discrete-threshold"
72
    )]
73
    pub minion_discrete_threshold: usize,
74

            
75
    #[serde(default = "default_true", rename = "validate-with-conjure")]
76
    pub validate_with_conjure: bool,
77

            
78
    // Generate this test but do not run it
79
    pub skip: bool,
80
}
81

            
82
impl Default for TestConfig {
83
2986
    fn default() -> Self {
84
2986
        Self {
85
2986
            skip: false,
86
2986
            parser: vec!["tree-sitter".to_string(), "via-conjure".to_string()],
87
2986
            rewriter: vec!["naive".to_string()],
88
2986
            comprehension_expander: vec![
89
2986
                "native".to_string(),
90
2986
                "via-solver".to_string(),
91
2986
                "via-solver-ac".to_string(),
92
2986
            ],
93
2986
            solver: {
94
2986
                let mut solvers = vec![
95
2986
                    "minion".to_string(),
96
2986
                    "sat-log".to_string(),
97
2986
                    "sat-direct".to_string(),
98
2986
                    "sat-order".to_string(),
99
2986
                ];
100
2986

            
101
2986
                {
102
2986
                    solvers.extend([
103
2986
                        "smt".to_string(),
104
2986
                        "smt-lia-arrays-nodiscrete".to_string(),
105
2986
                        "smt-lia-atomic".to_string(),
106
2986
                        "smt-lia-atomic-nodiscrete".to_string(),
107
2986
                        "smt-bv-arrays".to_string(),
108
2986
                        "smt-bv-arrays-nodiscrete".to_string(),
109
2986
                        "smt-bv-atomic".to_string(),
110
2986
                        "smt-bv-atomic-nodiscrete".to_string(),
111
2986
                    ]);
112
2986
                }
113
2986
                solvers
114
2986
            },
115
2986
            minion_discrete_threshold: default_minion_discrete_threshold(),
116
2986
            validate_with_conjure: true,
117
2986
        }
118
2986
    }
119
}
120

            
121
impl TestConfig {
122
1194
    pub fn configured_parsers(&self) -> Result<Vec<Parser>, String> {
123
1194
        parse_values(&self.parser)
124
1194
    }
125

            
126
885
    pub fn configured_rewriters(&self) -> Result<Vec<Rewriter>, String> {
127
885
        if self.rewriter.is_empty() {
128
            return Err("setting 'rewriter' has no values".to_string());
129
885
        }
130

            
131
885
        parse_values(&self.rewriter)
132
885
    }
133

            
134
885
    pub fn configured_comprehension_expanders(&self) -> Result<Vec<QuantifiedExpander>, String> {
135
885
        let values = if self.comprehension_expander.is_empty() {
136
            vec!["native".to_string()]
137
        } else {
138
885
            self.comprehension_expander.clone()
139
        };
140

            
141
885
        parse_values(&values)
142
885
    }
143

            
144
885
    pub fn configured_solvers(&self) -> Result<Vec<SolverFamily>, String> {
145
885
        parse_values(&self.solver)
146
885
    }
147

            
148
    pub fn uses_smt_solver(&self) -> bool {
149
        self.solver
150
            .iter()
151
            .any(|solver| solver == "smt" || solver.starts_with("smt-"))
152
    }
153
}