Debian Codegraph @

deps/clic/src/clic-config.ads

raw
1 with AAA.Strings;
2
3 with TOML;
4
5 private with Ada.Containers.Hashed_Maps;
6 private with Ada.Strings.Unbounded.Hash;
7
8 package CLIC.Config with Preelaborate is
9
10 function Is_Valid_Config_Key (Key : String) return Boolean
11 is ((for all C of Key => C in '0' .. '9' | 'a' .. 'z' | 'A' .. 'Z' |
12 '-' | '.' | '_')
13 and then Key (Key'First) not in '-' | '.' | '_'
14 and then Key (Key'Last) not in '-' | '.' | '_'
15 and then not AAA.Strings.Contains (Key, ".."));
16 -- Rule that define a valid configuration key. Dots are used to separate
17 -- levels of configuration groups.
18 -- eg:
19 -- user.login
20 -- user.email
21
22 subtype Config_Key is String
23 with Dynamic_Predicate => Is_Valid_Config_Key (Config_Key);
24
25 type Instance is tagged limited private;
26 pragma Preelaborable_Initialization (Instance);
27
28 type Check_Import is
29 access function (Key : Config_Key; Value : TOML.TOML_Value)
30 return Boolean;
31 -- Return False when a Key/Value combination is not valid. Can be used to
32 -- check formating of string value like email address for instance.
33
34 procedure Import (This : in out Instance;
35 Table : TOML.TOML_Value;
36 Origin : String;
37 Check : Check_Import := null);
38 -- Import configuration from the TOML table.
39 --
40 -- The Origin parameter is used in error messages and when listing config
41 -- values to help the user identify the origin of the value. It can be just
42 -- a word (e.g. local vs global), or the full path to the config file.
43 --
44 -- When a Check function is provided, it will be called for each imported
45 -- config key/value. If Check return False, the value is ignored and not
46 -- imported in the configuration. Import will print an Error message saying
47 -- that the key/value is ignored. In addition the Check function can also
48 -- print an error message explaining why the key/value is invalid.
49
50 function Defined (This : Instance; Key : Config_Key) return Boolean;
51 -- Return True if a value is defined for the given key
52
53 function Get_As_String (This : Instance; Key : Config_Key) return String;
54 -- Return a string representation of the value for the given configuration
55 -- Key. If the key is not defined, an empty string is returned.
56
57 function Get (This : Instance;
58 Key : Config_Key;
59 Default : Boolean)
60 return Boolean;
61 -- Return the Boolean value for the given configuration Key. If the key is
62 -- not defined, the Default value is returned. If the key is defined but
63 -- not as a Boolean, an error message is displayed and the Default value
64 -- is returned.
65
66 function Get (This : Instance;
67 Key : Config_Key;
68 Default : String)
69 return String;
70 -- Return the String value for the given configuration Key. If the key is
71 -- not defined, the Default value is returned. If the key is defined but
72 -- not as a String, an error message is displayed and the Default value
73 -- is returned.
74
75 function Get (This : Instance;
76 Key : Config_Key;
77 Default : TOML.Any_Integer)
78 return TOML.Any_Integer;
79 -- Return the Integer value for the given configuration Key. If the key is
80 -- not defined, the Default value is returned. If the key is defined but
81 -- not as an Integer, an error message is displayed and the Default value
82 -- is returned.
83
84 function Get (This : Instance;
85 Key : Config_Key;
86 Default : TOML.Any_Float)
87 return TOML.Any_Float;
88 -- Return the Float value for the given configuration Key. If the key is
89 -- not defined, the Default value is returned. If the key is defined but
90 -- not as an Float, an error message is displayed and the Default value
91 -- is returned.
92
93 procedure Clear (This : in out Instance);
94 -- Remove all configuration keys
95
96 function Image (Val : TOML.TOML_Value) return String;
97
98 private
99
100 generic
101 type Return_Type (<>) is private;
102 Expected_TOML_Kind : TOML.Any_Value_Kind;
103 Type_Name : String;
104
105 with function TOML_As_Return_Type (Value : TOML.TOML_Value)
106 return Return_Type;
107
108 with function Image (V : Return_Type) return String;
109
110 function Get_With_Default_Gen (This : Instance;
111 Key : Config_Key;
112 Default : Return_Type)
113 return Return_Type;
114
115 function To_TOML_Value (Str : String) return TOML.TOML_Value;
116 -- Use the TOML parser to convert the string Str. If Str is not a valid
117 -- TOML value, No_TOML_Value is returned.
118
119 type Config_Value is record
120 Value : TOML.TOML_Value;
121 Origin : Ada.Strings.Unbounded.Unbounded_String;
122 end record;
123
124 function No_Config_Value return Config_Value;
125
126 package Config_Maps is new Ada.Containers.Hashed_Maps
127 (Key_Type => Ada.Strings.Unbounded.Unbounded_String,
128 Element_Type => Config_Value,
129 Hash => Ada.Strings.Unbounded.Hash,
130 Equivalent_Keys => Ada.Strings.Unbounded."=");
131
132 function "+" (Source : String) return Ada.Strings.Unbounded.Unbounded_String
133 renames Ada.Strings.Unbounded.To_Unbounded_String;
134
135 type Instance is tagged limited record
136 Config_Map : Config_Maps.Map;
137 end record;
138
139 function Get (This : Instance; Key : Config_Key) return Config_Value;
140
141 end CLIC.Config;