1use aylis_core::{PropKind, RecordInfo};
6use serde_json::Value as Json;
7
8pub const MASTER_FIELD_RECORD: RecordInfo =
10 RecordInfo { name: "MasterField", fields: &[("field", PropKind::String), ("master_field", PropKind::String)] };
11
12pub const INDEX_DEF_RECORD: RecordInfo = RecordInfo { name: "IndexDef", fields: &[("name", PropKind::String), ("fields", PropKind::String)] };
14
15pub const FILTER_PARAM_RECORD: RecordInfo = RecordInfo { name: "FilterParam", fields: &[("name", PropKind::String), ("value", PropKind::String)] };
17
18fn items(text: &str) -> Vec<Json> {
20 match serde_json::from_str::<Json>(text) {
21 Ok(Json::Array(items)) => items,
22 _ => Vec::new(),
23 }
24}
25
26fn text(json: &Json, key: &str) -> String {
28 match &json[key] {
29 Json::String(s) => s.trim().to_string(),
30 Json::Null => String::new(),
31 other => other.to_string(),
32 }
33}
34
35pub(crate) fn master_fields(spec: &str) -> Vec<(String, String)> {
37 items(spec)
38 .iter()
39 .filter_map(|j| {
40 let field = text(j, "field");
41 if field.is_empty() {
42 return None;
43 }
44 let master = text(j, "master_field");
45 let master = if master.is_empty() { field.clone() } else { master };
46 Some((field, master))
47 })
48 .collect()
49}
50
51pub(crate) fn index_defs(spec: &str) -> Vec<(String, String)> {
53 items(spec).iter().map(|j| (text(j, "name"), text(j, "fields"))).filter(|(n, _)| !n.is_empty()).collect()
54}
55
56pub(crate) fn filter_params(spec: &str) -> Vec<(String, String)> {
58 items(spec).iter().map(|j| (text(j, "name"), text(j, "value"))).filter(|(n, _)| !n.is_empty()).collect()
59}