1
#![allow(unused)]
2

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

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

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

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

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

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

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

            
65
    #[serde(default = "default_true", rename = "validate-with-conjure")]
66
    pub validate_with_conjure: bool,
67
}
68

            
69
impl Default for TestConfig {
70
3336
    fn default() -> Self {
71
3336
        Self {
72
3336
            parser: vec!["tree-sitter".to_string(), "via-conjure".to_string()],
73
3336
            rewriter: vec!["naive".to_string()],
74
3336
            comprehension_expander: vec![
75
3336
                "native".to_string(),
76
3336
                "via-solver".to_string(),
77
3336
                "via-solver-ac".to_string(),
78
3336
            ],
79
3336
            solver: {
80
3336
                let mut solvers = vec![
81
3336
                    "minion".to_string(),
82
3336
                    "sat-log".to_string(),
83
3336
                    "sat-direct".to_string(),
84
3336
                    "sat-order".to_string(),
85
3336
                ];
86
3336
                #[cfg(feature = "smt")]
87
3336
                {
88
3336
                    solvers.extend([
89
3336
                        "smt".to_string(),
90
3336
                        "smt-lia-arrays-nodiscrete".to_string(),
91
3336
                        "smt-lia-atomic".to_string(),
92
3336
                        "smt-lia-atomic-nodiscrete".to_string(),
93
3336
                        "smt-bv-arrays".to_string(),
94
3336
                        "smt-bv-arrays-nodiscrete".to_string(),
95
3336
                        "smt-bv-atomic".to_string(),
96
3336
                        "smt-bv-atomic-nodiscrete".to_string(),
97
3336
                    ]);
98
3336
                }
99
3336
                solvers
100
3336
            },
101
3336
            validate_with_conjure: true,
102
3336
        }
103
3336
    }
104
}
105

            
106
impl TestConfig {
107
1156
    pub fn configured_parsers(&self) -> Result<Vec<Parser>, String> {
108
1156
        parse_values(&self.parser)
109
1156
    }
110

            
111
1090
    pub fn configured_rewriters(&self) -> Result<Vec<Rewriter>, String> {
112
1090
        if self.rewriter.is_empty() {
113
            return Err("setting 'rewriter' has no values".to_string());
114
1090
        }
115

            
116
1090
        parse_values(&self.rewriter)
117
1090
    }
118

            
119
1090
    pub fn configured_comprehension_expanders(&self) -> Result<Vec<QuantifiedExpander>, String> {
120
1090
        let values = if self.comprehension_expander.is_empty() {
121
            vec!["native".to_string()]
122
        } else {
123
1090
            self.comprehension_expander.clone()
124
        };
125

            
126
1090
        parse_values(&values)
127
1090
    }
128

            
129
1090
    pub fn configured_solvers(&self) -> Result<Vec<SolverFamily>, String> {
130
1090
        parse_values(&self.solver)
131
1090
    }
132

            
133
    pub fn uses_smt_solver(&self) -> bool {
134
        self.solver
135
            .iter()
136
            .any(|solver| solver == "smt" || solver.starts_with("smt-"))
137
    }
138
}