1
//! Descriptor writer: deterministic plist text read by the clients.
2

            
3
use crate::descriptor::{Action, Field, Form, Kind};
4

            
5
/// Quote a string for a nomiscript literal.
6
#[must_use]
7
3065836
pub fn escape_str(s: &str) -> String {
8
3065836
    let mut out = String::with_capacity(s.len() + 2);
9
3065836
    out.push('"');
10
3065836
    for ch in s.chars() {
11
68992960
        match ch {
12
1950338
            '"' => out.push_str("\\\""),
13
270914
            '\\' => out.push_str("\\\\"),
14
86208
            '\n' => out.push_str("\\n"),
15
32832
            '\r' => out.push_str("\\r"),
16
66652668
            other => out.push(other),
17
        }
18
    }
19
3065836
    out.push('"');
20
3065836
    out
21
3065836
}
22

            
23
221952
fn flag(value: bool) -> &'static str {
24
221952
    if value { "t" } else { "nil" }
25
221952
}
26

            
27
36096
fn action_symbol(action: Action) -> &'static str {
28
36096
    match action {
29
15936
        Action::Mutation => "mutation",
30
10368
        Action::Query => "query",
31
9792
        Action::LoadMutation => "load-mutation",
32
    }
33
36096
}
34

            
35
221952
fn kind_sexpr(kind: Kind) -> String {
36
221952
    match kind {
37
101952
        Kind::Text { multiline, suggest } => format!(
38
            ":kind text{}{}",
39
101952
            if multiline { " :multiline t" } else { "" },
40
101952
            suggest.map_or_else(String::new, |s| format!(" :suggest {}", s.name()))
41
        ),
42
        Kind::Select {
43
            source,
44
            fills,
45
            empty,
46
33792
        } => format!(
47
            ":kind select :source {}{}{}",
48
33792
            source.name(),
49
33792
            fills.map_or_else(String::new, |cell| format!(" :fills {cell}")),
50
33792
            empty.map_or_else(String::new, |hint| format!(" :empty {}", escape_str(hint)))
51
        ),
52
9984
        Kind::Amount => ":kind amount".to_string(),
53
        Kind::Date {
54
            range_end,
55
            whole_day,
56
18048
        } => format!(
57
            ":kind date{}{}",
58
18048
            if range_end { " :range-end t" } else { "" },
59
18048
            if whole_day { " :whole-day t" } else { "" }
60
        ),
61
18048
        Kind::Rows { fields, .. } => format!(":kind rows :fields {}", fields_sexpr(fields)),
62
23808
        Kind::Hidden => ":kind hidden".to_string(),
63
16320
        Kind::Choice { options, default } => format!(
64
            ":kind choice :options {}{}",
65
16320
            options_sexpr(options),
66
16320
            default.map_or_else(String::new, |key| format!(" :default {}", escape_str(key)))
67
        ),
68
    }
69
221952
}
70

            
71
16320
fn options_sexpr(options: &[(&str, &str)]) -> String {
72
16320
    let items: Vec<String> = options
73
16320
        .iter()
74
46848
        .map(|(key, label)| format!("({} {})", escape_str(key), escape_str(label)))
75
        .collect();
76
16320
    format!("({})", items.join(" "))
77
16320
}
78

            
79
221952
fn field_sexpr(field: &Field) -> String {
80
221952
    format!(
81
        "(:key {} :label {} {} :required {}{}{})",
82
221952
        escape_str(field.key),
83
221952
        escape_str(field.label),
84
221952
        kind_sexpr(field.kind),
85
221952
        flag(field.required),
86
221952
        field
87
221952
            .nested_under()
88
4416
            .map(|parent| format!(" :nested-under {}", escape_str(parent)))
89
            .unwrap_or_default(),
90
221952
        if field.is_paging() { " :paging t" } else { "" }
91
    )
92
221952
}
93

            
94
54144
fn fields_sexpr(fields: &[Field]) -> String {
95
54144
    let items: Vec<String> = fields.iter().map(field_sexpr).collect();
96
54144
    format!("({})", items.join(" "))
97
54144
}
98

            
99
#[must_use]
100
36096
pub fn descriptor_sexpr(form: &Form) -> String {
101
36096
    format!(
102
        "(:name {} :title {} :action {} :fields {})",
103
36096
        escape_str(form.name),
104
36096
        escape_str(form.title),
105
36096
        action_symbol(form.action),
106
36096
        fields_sexpr(form.fields)
107
    )
108
36096
}
109

            
110
#[must_use]
111
1536
pub fn descriptors_sexpr(forms: &[&Form]) -> String {
112
32256
    let items: Vec<String> = forms.iter().map(|form| descriptor_sexpr(form)).collect();
113
1536
    format!("({})", items.join(" "))
114
1536
}