Debian Codegraph @

deps/clic/src/clic-config.adb

raw
1 with Ada.Strings.Unbounded;
2
3 with Simple_Logging;
4
5 package body CLIC.Config is
6
7 use Ada.Strings.Unbounded;
8 use TOML;
9
10 package Trace renames Simple_Logging;
11
12 function Image (F : TOML.Any_Float) return String;
13
14 ----------------------
15 -- Import_Recursive --
16 ----------------------
17
18 procedure Import_Recursive (This : in out Instance;
19 Table : TOML.TOML_Value;
20 Origin : String;
21 Check : Check_Import := null;
22 Prefix : String := "")
23 is
24 begin
25 if Table = No_TOML_Value
26 or else
27 Table.Kind /= TOML_Table
28 then
29 return;
30 end if;
31
32 for Ent of Iterate_On_Table (Table) loop
33 declare
34 Key : constant String :=
35 (if Prefix = "" then "" else Prefix & ".") &
36 To_String (Ent.Key);
37
38 begin
39 if not Is_Valid_Config_Key (Key) then
40 Trace.Error ("Invalid configuration key '" & Key & "' in " &
41 "'" & Origin & "'");
42 elsif Ent.Value.Kind = TOML_Table then
43
44 -- Recursive call on the table
45 Import_Recursive (This, Ent.Value, Origin, Check, Key);
46 else
47
48 Trace.Debug ("Load config key: '" & Key & "' = '" &
49 Ent.Value.Kind'Img & "'");
50
51 if Ent.Value.Kind not in TOML_String | TOML_Float |
52 TOML_Integer | TOML_Boolean
53 then
54 Trace.Error ("Invalid type '" & Ent.Value.Kind'Img &
55 "' for key '" & Key &
56 "' in configuration file '" &
57 Origin & "'");
58 Trace.Error ("'" & Key & "' is ignored");
59 elsif Check /= null and then not Check (Key, Ent.Value) then
60 Trace.Error ("'" & Key & "' is ignored");
61 else
62 -- Insert the config value, potentially replacing a previous
63 -- definition.
64 This.Config_Map.Include
65 (To_Unbounded_String (Key),
66 (Value => Ent.Value,
67 Origin => To_Unbounded_String (Origin)));
68 end if;
69 end if;
70 end;
71 end loop;
72 end Import_Recursive;
73
74 ------------
75 -- Import --
76 ------------
77
78 procedure Import (This : in out Instance;
79 Table : TOML.TOML_Value;
80 Origin : String;
81 Check : Check_Import := null)
82 is
83 begin
84 Import_Recursive (This, Table, Origin, Check);
85 end Import;
86
87 -------------
88 -- Defined --
89 -------------
90
91 function Defined (This : Instance;
92 Key : Config_Key)
93 return Boolean
94 is
95 begin
96 return This.Config_Map.Contains (+Key);
97 end Defined;
98
99 -------------------
100 -- Get_As_String --
101 -------------------
102
103 function Get_As_String (This : Instance;
104 Key : Config_Key)
105 return String
106 is
107 begin
108 if This.Defined (Key) then
109 return Image (This.Get (Key).Value);
110 else
111 return "";
112 end if;
113 end Get_As_String;
114
115 ---------
116 -- Get --
117 ---------
118
119 function Get (This : Instance; Key : Config_Key) return Config_Value is
120 begin
121 if This.Defined (Key) then
122 return This.Config_Map.Element (+Key);
123 else
124 return No_Config_Value;
125 end if;
126 end Get;
127
128 ---------
129 -- Get --
130 ---------
131
132 function Get (This : Instance;
133 Key : Config_Key;
134 Default : Boolean)
135 return Boolean
136 is
137 function Get_With_Default_Bool is new Get_With_Default_Gen
138 (Boolean, TOML_Boolean, "Boolean", TOML.As_Boolean, Boolean'Image);
139
140 begin
141 return Get_With_Default_Bool (This, Key, Default);
142 end Get;
143
144 ---------
145 -- Get --
146 ---------
147
148 function Get (This : Instance;
149 Key : Config_Key;
150 Default : String)
151 return String
152 is
153 function Id (Str : String) return String
154 is (Str);
155
156 function Get_With_Default_Str is new Get_With_Default_Gen
157 (String, TOML_String, "String", TOML.As_String, Id);
158
159 begin
160 return Get_With_Default_Str (This, Key, Default);
161 end Get;
162
163 ---------
164 -- Get --
165 ---------
166
167 function Get (This : Instance;
168 Key : Config_Key;
169 Default : TOML.Any_Integer)
170 return TOML.Any_Integer
171 is
172 function Get_With_Default_Int is new Get_With_Default_Gen
173 (TOML.Any_Integer, TOML_Integer, "Integer", TOML.As_Integer,
174 Any_Integer'Image);
175
176 begin
177 return Get_With_Default_Int (This, Key, Default);
178 end Get;
179
180 ---------
181 -- Get --
182 ---------
183
184 function Get (This : Instance;
185 Key : Config_Key;
186 Default : TOML.Any_Float)
187 return TOML.Any_Float
188 is
189 function Get_With_Default_Int is new Get_With_Default_Gen
190 (TOML.Any_Float, TOML_Float, "Float", TOML.As_Float, Image);
191 begin
192 return Get_With_Default_Int (This, Key, Default);
193 end Get;
194
195 -----------
196 -- Clear --
197 -----------
198
199 procedure Clear (This : in out Instance) is
200 begin
201 This.Config_Map.Clear;
202 end Clear;
203
204 --------------------------
205 -- Get_With_Default_Gen --
206 --------------------------
207
208 function Get_With_Default_Gen (This : Instance;
209 Key : Config_Key;
210 Default : Return_Type)
211 return Return_Type
212 is
213 Val : constant Config_Value := This.Get (Key);
214 begin
215 if Val.Value.Is_Null then
216 Trace.Detail ("Using default value for configuration '" & Key &
217 "': '" & Image (Default) & "'");
218 return Default;
219
220 elsif Val.Value.Kind /= Expected_TOML_Kind then
221 Trace.Error ("Invalid type ('" & Val.Value.Kind'Img &
222 "') for configuration '" & Key & "'");
223 Trace.Error ("in '" & To_String (Val.Origin) & "'");
224 Trace.Error (Type_Name & " expected");
225 Trace.Error ("Using default: '" & Image (Default) & "'");
226 return Default;
227
228 else
229 return TOML_As_Return_Type (Val.Value);
230 end if;
231 end Get_With_Default_Gen;
232
233 -------------------
234 -- To_TOML_Value --
235 -------------------
236
237 function To_TOML_Value (Str : String) return TOML.TOML_Value is
238 Result : constant TOML.Read_Result := TOML.Load_String ("key=" & Str);
239 begin
240 if not Result.Success
241 or else
242 Result.Value.Kind /= TOML_Table
243 or else
244 not Result.Value.Has ("key")
245 then
246
247 -- Conversion failed
248
249 -- Interpret as a string
250 return Create_String (Str);
251 else
252 return Result.Value.Get ("key");
253 end if;
254 end To_TOML_Value;
255
256 ---------------------
257 -- No_Config_Value --
258 ---------------------
259
260 function No_Config_Value return Config_Value
261 is (Value => TOML.No_TOML_Value,
262 Origin => Null_Unbounded_String);
263
264 -----------
265 -- Image --
266 -----------
267
268 function Image (F : TOML.Any_Float) return String is
269 begin
270 case F.Kind is
271 when Regular =>
272 return AAA.Strings.Trim (F.Value'Image);
273 when NaN | Infinity =>
274 return (if F.Positive then "" else "-") &
275 (if F.Kind = NaN then "nan" else "inf");
276 end case;
277 end Image;
278
279 -----------
280 -- Image --
281 -----------
282
283 function Image (Val : TOML.TOML_Value) return String is
284 begin
285 case Val.Kind is
286 when TOML_Boolean =>
287 return (if Val.As_Boolean then "true" else "false");
288 when TOML_Integer =>
289 return AAA.Strings.Trim (Val.As_Integer'Img);
290 when TOML_Float =>
291 return Image (Val.As_Float);
292 when TOML_String =>
293 return Val.As_String;
294 when others =>
295 return "";
296 end case;
297 end Image;
298
299 end CLIC.Config;