src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 161
0.0%
Branches:
0 / 60, 38 excl
0.0%
Functions:
0 / 12
0.0%

util/manifest.spice
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 // Std imports
4 import "std/io/file";
5 import "std/io/filepath";
6 import "std/text/toml/toml-parser";
7 import "std/text/toml/toml-value";
8 import "std/type/type-conversion";
9
10 // Package imports
11 import "@/spice_packages/manifest/manifest";
12 import "@/spice_packages/manifest/manifest-file";
13
14 // Own imports
15 import "@/exception/compiler-error";
16 import "@/exception/fatal-error";
17 import "@/formatter/format-config";
18 import "@/linter/lint-config";
19 import "@/linter/lint-finding";
20 import "@/util/test-util";
21
22 // Constants
23 const string LINT_RULES_TABLE_NAME = "rules";
24 const long NO_UPPER_BOUND = -1l; // For ManifestReader.readInteger
25
26 /*
27 * The manifest (spice.toml) of a project describes the package for Spice Shaker and configures the tooling of the compiler. Its
28 * format is specified by the manifest package (https://github.com/spicelang/packages/tree/main/packages/manifest), which also
29 * finds the manifest of a source file (findManifest in manifest-file) for the compiler:
30 *
31 * [lint.rules]
32 * naming-convention = { enabled = false }
33 * too-many-parameters = { max = 8, severity = "error" }
34 *
35 * [format]
36 * indent-width = 2
37 * max-blank-lines = 2
38 *
39 * The compiler only reads the [lint] and [format] tables and leaves the other tables to Spice Shaker. The manifest package
40 * leaves the contents of [lint] and [format] to the compiler. Within them, the compiler rejects unknown keys, so that a typo
41 * does not silently fall back to the default.
42 */
43
44 /**
45 * Reads the values of a manifest and reports the errors in it
46 */
47 ✗ type ManifestReader struct {
48 FilePath path
49 String displayPath // Path of the manifest in error messages
50 }
51
52 /**
53 * @param path Path of the manifest
54 * @param displayBaseDir Dir, that the path of the manifest is printed relative to in error messages
55 */
56 ✗ p ManifestReader.ctor(const FilePath& path, const FilePath& displayBaseDir) {
57
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ this.path = path;
58 // Like the code locations of the compiler: './' for paths within the base dir and the absolute path, if there is no relative one
59 ✗ this.displayPath = manifestDisplayPath(path, displayBaseDir);
60 ✗ }
61
62 /**
63 * Read and parse the manifest. Aborts, if it cannot be read or is no valid TOML.
64 *
65 * @return Root table of the manifest, which the caller has to release with deleteTomlValue
66 */
67 ✗ f<TomlValue*> ManifestReader.parse() {
68 ✗ Result<String> readResult = readFile(this.path.toString());
69
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if !readResult.isOk() {
70 ✗ this.raiseError(String("The manifest cannot be read"));
71 ✗ }
72 ✗ Result<TomlValue*> parseResult = parseToml(readResult.unwrap());
73
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if !parseResult.isOk() {
74 ✗ this.raiseError(String("The manifest is no valid TOML: ") + parseResult.getErr().getMessage());
75 ✗ }
76 ✗ return parseResult.unwrap();
77 ✗ }
78
79 /**
80 * Read the configuration of the individual lint rules from the [lint.rules] table
81 *
82 * @param rulesTable Value of the [lint.rules] table
83 * @param config Configuration to update
84 */
85 ✗ p ManifestReader.readLintRulesTable(TomlValue* rulesTable, LintConfig& config) {
86 ✗ const String rulesTableName = String(TABLE_LINT) + "." + LINT_RULES_TABLE_NAME;
87 ✗ this.expectTable(rulesTable, rulesTableName);
88
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for unsigned long i = 0l; i < rulesTable.getTableSize(); i++ {
89 ✗ const String& ruleId = rulesTable.getTableKey(i);
90 ✗ LintRuleConfig* ruleConfig = config.getRuleConfig(ruleId);
91
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if ruleConfig == nil<LintRuleConfig*> {
92 ✗ this.raiseError("Unknown lint rule '" + ruleId + "'");
93 ✗ }
94 ✗ const String ruleTableName = rulesTableName + "." + ruleId;
95 ✗ TomlValue* ruleTable = rulesTable.getTableValue(i);
96 ✗ this.expectTable(ruleTable, ruleTableName);
97
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for unsigned long j = 0l; j < ruleTable.getTableSize(); j++ {
98 ✗ const String& key = ruleTable.getTableKey(j);
99 ✗ TomlValue* value = ruleTable.getTableValue(j);
100 ✗ const String keyName = ruleTableName + "." + key;
101
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if key == "enabled" {
102
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if !value.isBool() {
103 ✗ this.raiseError("'" + keyName + "' has to be a boolean");
104 ✗ }
105 ✗ ruleConfig.enabled = value.getBool();
106
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if key == "severity" {
107 ✗ ruleConfig.severity = this.readLintSeverity(value, keyName);
108
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ } else if key == "max" && hasRuleThreshold(ruleId) {
109 ✗ ruleConfig.max = this.readInteger(value, keyName, 1l, NO_UPPER_BOUND);
110 ✗ } else {
111 ✗ this.raiseUnknownKey(keyName);
112 ✗ }
113 ✗ }
114 ✗ }
115 ✗ }
116
117 /**
118 * Read a lint severity ("info", "warning" or "error")
119 *
120 * @param value Value to read
121 * @param keyName Full name of the key, which errors refer to
122 * @return Lint severity
123 */
124 ✗ f<LintSeverity> ManifestReader.readLintSeverity(TomlValue* value, const String& keyName) {
125
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if value.isString() {
126 ✗ const String& severity = value.getString();
127
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if severity == "info" {
128 ✗ return LintSeverity::INFO;
129 }
130
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if severity == "warning" {
131 ✗ return LintSeverity::WARNING;
132 }
133
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if severity == "error" {
134 ✗ return LintSeverity::ERROR;
135 }
136 ✗ }
137 ✗ this.raiseError("'" + keyName + "' has to be one of \"info\", \"warning\" or \"error\"");
138 ✗ }
139
140 /**
141 * Read an integer within the given bounds
142 *
143 * @param value Value to read
144 * @param keyName Full name of the key, which errors refer to
145 * @param min Minimum value
146 * @param max Maximum value or NO_UPPER_BOUND
147 * @return Integer
148 */
149 ✗ f<long> ManifestReader.readInteger(TomlValue* value, const String& keyName, long min, long max) {
150
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ if !value.isInteger() || value.getInteger() < min || (max != NO_UPPER_BOUND && value.getInteger() > max) {
151 ✗ String message = "'" + keyName + "' has to be an integer";
152
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if max == NO_UPPER_BOUND {
153 ✗ message += " of at least " + toString(min);
154 ✗ } else {
155 ✗ message += " from " + toString(min) + " to " + toString(max);
156 ✗ }
157 ✗ this.raiseError(message);
158 ✗ }
159 ✗ return value.getInteger();
160 }
161
162 /**
163 * Abort, if the given value is no table
164 *
165 * @param value Value to check
166 * @param keyName Full name of the key, which errors refer to
167 */
168 ✗ p ManifestReader.expectTable(TomlValue* value, const String& keyName) {
169
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if !value.isTable() {
170 ✗ this.raiseError("'" + keyName + "' has to be a table");
171 ✗ }
172 ✗ }
173
174 /**
175 * Abort with an error about an unknown key
176 *
177 * @param keyName Full name of the key
178 */
179 ✗ #[core.compiler.noReturn]
180 ✗ p ManifestReader.raiseUnknownKey(const String& keyName) {
181 ✗ this.raiseError("Unknown key '" + keyName + "'");
182 ✗ }
183
184 /**
185 * Abort with an error about the manifest
186 *
187 * @param message Error message
188 */
189 ✗ #[core.compiler.noReturn]
190 ✗ p ManifestReader.raiseError(const String& message) {
191 ✗ const String fullMessage = this.displayPath + ": " + message;
192 ✗ const CompilerError error = CompilerError(CompilerErrorType::INVALID_MANIFEST, fullMessage.getRaw());
193 ✗ abortWithError(error.getMessage());
194 ✗ }
195
196 /**
197 * Read the configuration of the lint rules from the given manifest. Rules, that the manifest does not configure, keep their
198 * default configuration. Aborts the compilation, if the configuration is invalid.
199 *
200 * @param manifestPath Path of the manifest. Empty for the default configuration
201 * @param displayBaseDir Dir, that the path of the manifest is printed relative to in error messages
202 * @return Configuration of the lint rules
203 */
204 ✗ public f<LintConfig> readLintConfig(const FilePath& manifestPath, const FilePath& displayBaseDir) {
205 ✗ LintConfig config;
206
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if manifestPath.isEmpty() {
207 ✗ return config;
208 }
209 ✗ ManifestReader reader = ManifestReader(manifestPath, displayBaseDir);
210 ✗ TomlValue* root = reader.parse();
211 ✗ TomlValue* lintTable = root.getFieldOrNil(TABLE_LINT);
212
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if lintTable != nil<TomlValue*> {
213 ✗ reader.expectTable(lintTable, String(TABLE_LINT));
214
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for unsigned long i = 0l; i < lintTable.getTableSize(); i++ {
215 ✗ const String& key = lintTable.getTableKey(i);
216
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if key != LINT_RULES_TABLE_NAME {
217 ✗ reader.raiseUnknownKey(String(TABLE_LINT) + "." + key);
218 ✗ }
219 ✗ reader.readLintRulesTable(lintTable.getTableValue(i), config);
220 ✗ }
221 ✗ }
222 ✗ deleteTomlValue(root);
223 ✗ return config;
224 ✗ }
225
226 /**
227 * Read the configuration of the source formatter from the given manifest. Options, that the manifest does not set, keep their
228 * default value. Aborts, if the configuration is invalid.
229 *
230 * @param manifestPath Path of the manifest. Empty for the default configuration
231 * @param displayBaseDir Dir, that the path of the manifest is printed relative to in error messages
232 * @return Configuration of the source formatter
233 */
234 ✗ public f<FormatConfig> readFormatConfig(const FilePath& manifestPath, const FilePath& displayBaseDir) {
235 ✗ FormatConfig config;
236
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if manifestPath.isEmpty() {
237 ✗ return config;
238 }
239 ✗ ManifestReader reader = ManifestReader(manifestPath, displayBaseDir);
240 ✗ TomlValue* root = reader.parse();
241 ✗ TomlValue* formatTable = root.getFieldOrNil(TABLE_FORMAT);
242
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if formatTable != nil<TomlValue*> {
243 ✗ reader.expectTable(formatTable, String(TABLE_FORMAT));
244
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for unsigned long i = 0l; i < formatTable.getTableSize(); i++ {
245 ✗ const String& key = formatTable.getTableKey(i);
246 ✗ TomlValue* value = formatTable.getTableValue(i);
247 ✗ const String keyName = String(TABLE_FORMAT) + "." + key;
248
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if key == "indent-width" {
249 ✗ config.indentWidth = cast<unsigned long>(reader.readInteger(value, keyName, MIN_INDENT_WIDTH, MAX_INDENT_WIDTH));
250
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if key == "max-blank-lines" {
251 ✗ config.maxBlankLines = cast<unsigned long>(reader.readInteger(value, keyName, 0l, MAX_MAX_BLANK_LINES));
252 ✗ } else {
253 ✗ reader.raiseUnknownKey(keyName);
254 ✗ }
255 ✗ }
256 ✗ }
257 ✗ deleteTomlValue(root);
258 ✗ return config;
259 ✗ }
260
261 // ============================================================== Tests ==============================================================
262
263 ✗ #[test]
264 f<bool> testManifestConfig() {
265 ✗ const FilePath testDir = makeUniqueTestDir("spice-manifest-test");
266 ✗ const FilePath subDir = testDir / "src" / "sub";
267 ✗ createTestDirs(subDir);
268 ✗ writeTestFile(subDir / "main.spice", "f<int> main() {}\n");
269
270 // Without manifest, the default configuration applies
271 ✗ const FilePath noManifest = findManifest(subDir / "main.spice");
272 ✗ const LintConfig defaultLintConfig = readLintConfig(noManifest, subDir);
273 ✗ const FormatConfig defaultFormatConfig = readFormatConfig(noManifest, subDir);
274
275 // The manifest in a parent dir is found. The tables of Spice Shaker are left alone
276 ✗ writeTestFile(testDir / MANIFEST_FILE, "[package]\nname = \"test\"\n\n[lint.rules]\nnaming-convention = { enabled = false }\ntoo-many-parameters = { max = 3, severity = \"error\" }\n\n[format]\nindent-width = 2\nmax-blank-lines = 0\n");
277 ✗ const FilePath manifestFromFile = findManifest(subDir / "main.spice");
278 ✗ const FilePath manifestFromDir = findManifest(subDir);
279 ✗ const LintConfig lintConfig = readLintConfig(manifestFromFile, subDir);
280 ✗ const FormatConfig formatConfig = readFormatConfig(manifestFromFile, subDir);
281
282 // The path of the manifest in error messages
283 ✗ const ManifestReader readerFromSubDir = ManifestReader(manifestFromFile, subDir);
284 ✗ const ManifestReader readerFromTestDir = ManifestReader(manifestFromFile, testDir);
285
286 // Clean up before asserting, so that a failure does not leave the files behind
287 ✗ removeTestDir(testDir);
288 ✗ assert noManifest.isEmpty();
289 ✗ assert defaultLintConfig.namingConvention.enabled && defaultLintConfig.namingConvention.severity == LintSeverity::WARNING;
290 ✗ assert defaultLintConfig.tooManyParameters.max == 6l && defaultLintConfig.cyclomaticComplexity.max == 10l;
291 ✗ assert defaultFormatConfig.indentWidth == 4l && defaultFormatConfig.maxBlankLines == 1l;
292 ✗ assert manifestFromFile.toGenericString() == manifestFromDir.toGenericString();
293 ✗ assert String(manifestFromFile.toGenericString()).endsWith("/spice.toml");
294 ✗ assert !lintConfig.namingConvention.enabled;
295 ✗ assert lintConfig.tooManyParameters.enabled && lintConfig.tooManyParameters.max == 3l;
296 ✗ assert lintConfig.tooManyParameters.severity == LintSeverity::ERROR;
297 ✗ assert lintConfig.cyclomaticComplexity.enabled && lintConfig.cyclomaticComplexity.max == 10l;
298 ✗ assert formatConfig.indentWidth == 2l && formatConfig.maxBlankLines == 0l;
299 ✗ assert readerFromSubDir.displayPath == "../../spice.toml";
300 ✗ assert readerFromTestDir.displayPath == "./spice.toml";
301 ✗ return true;
302 ✗ }
303