1use crate::descriptor::{Action, Field, Form, Kind};
4
5#[must_use]
7pub fn escape_str(s: &str) -> String {
8 let mut out = String::with_capacity(s.len() + 2);
9 out.push('"');
10 for ch in s.chars() {
11 match ch {
12 '"' => out.push_str("\\\""),
13 '\\' => out.push_str("\\\\"),
14 '\n' => out.push_str("\\n"),
15 '\r' => out.push_str("\\r"),
16 other => out.push(other),
17 }
18 }
19 out.push('"');
20 out
21}
22
23fn flag(value: bool) -> &'static str {
24 if value { "t" } else { "nil" }
25}
26
27fn action_symbol(action: Action) -> &'static str {
28 match action {
29 Action::Mutation => "mutation",
30 Action::Query => "query",
31 Action::LoadMutation => "load-mutation",
32 }
33}
34
35fn kind_sexpr(kind: Kind) -> String {
36 match kind {
37 Kind::Text { multiline, suggest } => format!(
38 ":kind text{}{}",
39 if multiline { " :multiline t" } else { "" },
40 suggest.map_or_else(String::new, |s| format!(" :suggest {}", s.name()))
41 ),
42 Kind::Select {
43 source,
44 fills,
45 empty,
46 } => format!(
47 ":kind select :source {}{}{}",
48 source.name(),
49 fills.map_or_else(String::new, |cell| format!(" :fills {cell}")),
50 empty.map_or_else(String::new, |hint| format!(" :empty {}", escape_str(hint)))
51 ),
52 Kind::Amount => ":kind amount".to_string(),
53 Kind::Date {
54 range_end,
55 whole_day,
56 } => format!(
57 ":kind date{}{}",
58 if range_end { " :range-end t" } else { "" },
59 if whole_day { " :whole-day t" } else { "" }
60 ),
61 Kind::Rows { fields, .. } => format!(":kind rows :fields {}", fields_sexpr(fields)),
62 Kind::Hidden => ":kind hidden".to_string(),
63 Kind::Choice { options, default } => format!(
64 ":kind choice :options {}{}",
65 options_sexpr(options),
66 default.map_or_else(String::new, |key| format!(" :default {}", escape_str(key)))
67 ),
68 }
69}
70
71fn options_sexpr(options: &[(&str, &str)]) -> String {
72 let items: Vec<String> = options
73 .iter()
74 .map(|(key, label)| format!("({} {})", escape_str(key), escape_str(label)))
75 .collect();
76 format!("({})", items.join(" "))
77}
78
79fn field_sexpr(field: &Field) -> String {
80 format!(
81 "(:key {} :label {} {} :required {}{}{})",
82 escape_str(field.key),
83 escape_str(field.label),
84 kind_sexpr(field.kind),
85 flag(field.required),
86 field
87 .nested_under()
88 .map(|parent| format!(" :nested-under {}", escape_str(parent)))
89 .unwrap_or_default(),
90 if field.is_paging() { " :paging t" } else { "" }
91 )
92}
93
94fn fields_sexpr(fields: &[Field]) -> String {
95 let items: Vec<String> = fields.iter().map(field_sexpr).collect();
96 format!("({})", items.join(" "))
97}
98
99#[must_use]
100pub fn descriptor_sexpr(form: &Form) -> String {
101 format!(
102 "(:name {} :title {} :action {} :fields {})",
103 escape_str(form.name),
104 escape_str(form.title),
105 action_symbol(form.action),
106 fields_sexpr(form.fields)
107 )
108}
109
110#[must_use]
111pub fn descriptors_sexpr(forms: &[&Form]) -> String {
112 let items: Vec<String> = forms.iter().map(|form| descriptor_sexpr(form)).collect();
113 format!("({})", items.join(" "))
114}