Debian Codegraph @

app/qml/KeyboardRows/jsonParser.js

raw symbols SCIP
1 .pragma library
2
3 // This small library prints semantic errors of bad profiles.
4
5 function isAllowed(value, allowed) {
6 return allowed.indexOf(value) !== -1;
7 }
8
9 function raiseException(msg, obj) {
10 throw msg + "\n" + JSON.stringify(obj, null, 2);
11 }
12
13 function validateKeyAction(keyObject) {
14 if (!keyObject.key)
15 return raiseException("key is missing");
16 if (keyObject.mod && !isAllowed(keyObject.mod, ["Control", "Shift", "Alt"]))
17 return raiseException("mod is invalid in", keyObject);
18 }
19
20 function validateStringAction(stringObject) {
21 if (!stringObject.string)
22 raiseException("string is missing in", stringObject);
23 }
24
25 function validateAction(actionObject) {
26 if (!actionObject.type)
27 raiseException("type is missing in", actionObject);
28 if (!isAllowed(actionObject.type, ["key", "string"]))
29 raiseException("type must be either key or string in", actionObject);
30 if (!(actionObject.type === "key" ? actionObject.key : actionObject.string))
31 raiseException(actionObject.type + " is missing in", actionObject);
32 if (actionObject.id && actionObject.text)
33 raiseException("Should not define id and text together in", actionObject);
34 if (!actionObject.id && !actionObject.text && !(actionObject.type === "key" && actionObject.key)) // We can also build the text from the key
35 raiseException("text or id is missing in", actionObject);
36
37
38 // No need to check for "if (!actionObject.id && !actionObject.text)" as we will
39 // build the displayed text from the keys in that case
40
41 switch (actionObject.type) {
42 case "key":
43 validateKeyAction(actionObject);
44 break;
45 case "string":
46 validateStringAction(actionObject);
47 break;
48 }
49 }
50
51 function validateButton(buttonObject) {
52 if (!buttonObject.main_action)
53 raiseException("main_action is missing", buttonObject);
54
55 validateAction(buttonObject.main_action);
56
57 var other_actions = buttonObject.other_actions;
58 if (other_actions) {
59 if (!Array.isArray(other_actions))
60 raiseException("other_actions is not an array", other_actions);
61
62 for (var i = 0; i < other_actions.length; i++) {
63 validateAction(other_actions[i]);
64 }
65 }
66 }
67
68 function validateLayout(layoutObject) {
69 if (!layoutObject.id) {
70 if (!layoutObject.name)
71 raiseException("name or id is missing in", layoutObject);
72 if (!layoutObject.short_name)
73 raiseException("short_name or id is missing in", layoutObject);
74 } else {
75 if (layoutObject.name)
76 raiseException("Should not define id and name together in", layoutObject);
77 if (layoutObject.short_name)
78 raiseException("Should not define id and short_name together in", layoutObject);
79 }
80
81 if (!layoutObject.buttons)
82 raiseException("buttons is missing in", layoutObject);
83
84 for (var i = 0; i < layoutObject.buttons.length; i++) {
85 validateButton(layoutObject.buttons[i]);
86 }
87 }
88
89 function parseJson(jsonString) {
90 var jsonObject = JSON.parse(jsonString);
91 validateLayout(jsonObject);
92
93 return jsonObject;
94 }