1
//! Functions for pretty printing Conjure models.
2
//!
3
//! Most things can be pretty printed using `Display`; however some, notably collections
4
//! can not, for example, Vec<Expression>
5

            
6
use std::fmt::Display;
7

            
8
use itertools::Itertools;
9

            
10
use super::{Expression, Name, SymbolTable};
11

            
12
/// Pretty prints a `Vec<Expression>` as if it were a top level constraint list in a `such that`.
13
///
14
/// Each expression is printed on a new line, and expressions are delimited by commas.
15
///
16
/// For some input expressions A,B,C:
17
/// ```text
18
/// A,
19
/// B,
20
/// C
21
/// ```
22
///
23
/// Each `Expression` is printed using its underlying `Display` implementation.
24
2856
pub fn pretty_expressions_as_top_level(expressions: &[Expression]) -> String {
25
5950
    expressions.iter().map(|x| format!("{}", x)).join(",\n")
26
2856
}
27

            
28
/// Pretty prints a `Vec<Expression>` as if it were a conjunction.
29
///
30
/// For some input expressions A,B,C:
31
///
32
/// ```text
33
/// (A /\ B /\ C)
34
/// ```
35
///
36
/// Each `Expression` is printed using its underlying `Display` implementation.
37
pub fn pretty_expressions_as_conjunction(expressions: &[Expression]) -> String {
38
    let mut str = expressions.iter().map(|x| format!("{}", x)).join(" /\\ ");
39

            
40
    str.insert(0, '(');
41
    str.push(')');
42

            
43
    str
44
}
45

            
46
/// Pretty prints a `Vec<T>` in a vector like syntax.
47
///
48
/// For some input values A,B,C:
49
///
50
/// ```text
51
/// [A,B,C]
52
/// ````
53
///
54
/// Each element is printed using its underlying `Display` implementation.
55
35598
pub fn pretty_vec<T: Display>(elems: &[T]) -> String {
56
50252
    let mut str = elems.iter().map(|x| format!("{}", x)).join(", ");
57
35598
    str.insert(0, '[');
58
35598
    str.push(']');
59
35598

            
60
35598
    str
61
35598
}
62

            
63
/// Pretty prints, in essence syntax, the variable declaration for the given symbol.
64
///
65
/// E.g.
66
///
67
/// ```text
68
/// a: int(1..5)
69
/// ```
70
///
71
/// Returns None if the symbol is not in the symbol table
72
10302
pub fn pretty_variable_declaration(symbol_table: &SymbolTable, var_name: &Name) -> Option<String> {
73
10302
    let var = symbol_table.get_var(var_name)?;
74
10302
    match &var.domain {
75
2108
        super::Domain::BoolDomain => Some(format!("{}: bool", var_name)),
76
8194
        super::Domain::IntDomain(domain) => {
77
8194
            let mut domain_ranges: Vec<String> = vec![];
78
16388
            for range in domain {
79
8194
                domain_ranges.push(match range {
80
204
                    super::Range::Single(a) => a.to_string(),
81
7990
                    super::Range::Bounded(a, b) => format!("{}..{}", a, b),
82
                });
83
            }
84

            
85
8194
            if domain_ranges.is_empty() {
86
                Some(format!("{}: int", var_name))
87
            } else {
88
8194
                Some(format!("{}: int({})", var_name, domain_ranges.join(",")))
89
            }
90
        }
91
    }
92
10302
}