src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
245 / 248
98.8%
Branches:
132 / 156, 46 excl
84.6%
Functions:
22 / 22
100.0%

spice_packages/manifest/src/layout.spice
Line Branch Exec Source
1 // Std imports
2 import "std/data/vector";
3 import "std/text/analysis";
4 import "std/type/type-conversion";
5
6 // ---------------------------------------------------------------------------------------------------------------------
7 // Line-level view of spice.toml, used to edit dependencies without touching anything else in the file.
8 //
9 // The std TOML parser produces a value tree without comments, so writing the tree back would lose them. Instead, this scanner
10 // finds the line of every dependency, and edits replace, insert or delete exactly those lines. This works because every
11 // dependency has to be a single `name = value` line inside a [dependencies] or [dev-dependencies] section (see the format
12 // restriction in the README). The scanner does not validate TOML; the manifest is always parsed by the std parser as well.
13 // ---------------------------------------------------------------------------------------------------------------------
14
15 const string SECTION_DEPENDENCIES = "dependencies";
16 const string SECTION_DEV_DEPENDENCIES = "dev-dependencies";
17 148 public type DependencyLine struct {
18 public String name
19 37 public bool dev = false
20 37 public unsigned long line = 0l // Index into ManifestLayout.lines
21 37 public unsigned long valueStart = 0l // Column where the value starts
22 37 public unsigned long valueEnd = 0l // Column after the value, before trailing spaces and the comment
23 }
24
25 54 public type ManifestLayout struct {
26 public Vector<String> lines
27 public Vector<DependencyLine> entries
28 27 public long dependenciesHeader = -1l // Line index of [dependencies], -1 if there is none
29 27 public long devDependenciesHeader = -1l // Line index of [dev-dependencies], -1 if there is none
30 public Vector<String> problems // Violations of the single-line rule
31 }
32
33 /**
34 * State of the value lexer, which is carried from one line to the next
35 */
36 54 type LexState struct {
37 public String openMultiline // `"""` or `'''` while inside a multi-line string, empty otherwise
38 27 public long depth = 0l // Nesting of brackets and braces, so that multi-line arrays are not mistaken for headers
39 }
40
41 /**
42 * Lines of a text without their line breaks. A trailing line break does not produce an empty last line.
43 */
44 28 f<Vector<String>> splitManifestLines(const String& text) {
45 28 Vector<String> lines;
46 28 String current;
47
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 2720 times.
2748 for unsigned long i = 0l; i < text.getLength(); i++ {
48 2720 const char c = text[i];
49
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 2504 times.
2720 if c == '\n' {
50 216 lines.pushBack(current);
51 216 current = String("");
52
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2503 times.
2720 } else if c != '\r' {
53 2503 current.append(c);
54 2504 }
55 2720 }
56
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if !current.isEmpty() { lines.pushBack(current); }
57 56 return lines;
58 28 }
59
60 13 f<String> joinManifestLines(const Vector<String>& lines) {
61 13 String text;
62
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 13 times.
117 for unsigned long i = 0l; i < lines.getSize(); i++ {
63 104 text += lines.get(i);
64 104 text += "\n";
65 104 }
66 13 return text;
67 }
68
69 123 f<bool> isBareKeyChar(char c) {
70
4/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 40 times.
123 return isAlphaNum(c) || c == '-' || c == '_';
71 }
72
73 1637 f<bool> matchesAt(const String& text, unsigned long pos, string needle) {
74 1637 const String n = String(needle);
75
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1553 times.
1637 if pos + n.getLength() > text.getLength() { return false; }
76
2/2
✓ Branch 0 taken 1647 times.
✓ Branch 1 taken 2 times.
1649 for unsigned long i = 0l; i < n.getLength(); i++ {
77
2/2
✓ Branch 0 taken 1551 times.
✓ Branch 1 taken 96 times.
1647 if text[pos + i] != n[i] { return false; }
78 96 }
79 2 return true;
80 1637 }
81
82 /**
83 * Lex a line (or the rest of it) from `pos` on. Skips strings, tracks the bracket depth and stops at a comment.
84 * Returns the column where the meaningful content ends, i.e. before trailing spaces and the comment.
85 */
86 95 #[ignoreUnusedReturnValue]
87 95 f<unsigned long> lexLine(const String& line, unsigned long pos, LexState& state) {
88 95 const unsigned long len = line.getLength();
89 95 unsigned long contentEnd = pos;
90 95 unsigned long i = pos;
91
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 822 times.
901 while i < len {
92 // Continue a multi-line string that was opened before
93
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 819 times.
822 if !state.openMultiline.isEmpty() {
94 3 const bool basic = state.openMultiline == "\"\"\"";
95
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 3 times.
30 while i < len && !matchesAt(line, i, state.openMultiline.getRaw()) {
96
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 if basic && line[i] == '\\' { i++; }
97 27 i++;
98 27 }
99
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 if i >= len { return len; }
100 1 i += 3l;
101 // A multi-line string may end with up to two additional quotes
102 1 const char quote = basic ? '"' : '\'';
103
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 while i < len && line[i] == quote { i++; }
104 1 state.openMultiline = String("");
105 1 contentEnd = i;
106 1 continue;
107 }
108 819 const char c = line[i];
109
2/2
✓ Branch 0 taken 805 times.
✓ Branch 1 taken 14 times.
819 if c == '#' { break; }
110
4/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 804 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 804 times.
805 if matchesAt(line, i, "\"\"\"") || matchesAt(line, i, "'''") {
111 1 state.openMultiline = line.getSubstring(i, 3l);
112 1 i += 3l;
113 1 continue;
114 1 }
115
4/4
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 716 times.
✓ Branch 2 taken 89 times.
✓ Branch 3 taken 715 times.
804 if c == '"' || c == '\'' {
116 89 i++;
117
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 441 times.
✓ Branch 2 taken 352 times.
✓ Branch 3 taken 89 times.
441 while i < len && line[i] != c {
118
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 349 times.
✓ Branch 2 taken 352 times.
✗ Branch 3 not taken.
352 if c == '"' && line[i] == '\\' { i++; }
119 352 i++;
120 352 }
121 89 i++;
122
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 54 times.
89 contentEnd = i < len ? i : len;
123 89 continue;
124 }
125
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 709 times.
✓ Branch 2 taken 694 times.
✓ Branch 3 taken 21 times.
715 if c == '[' || c == '{' { state.depth++; }
126
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 709 times.
✓ Branch 2 taken 694 times.
✓ Branch 3 taken 21 times.
715 if c == ']' || c == '}' { state.depth--; }
127
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 526 times.
715 if !isWhitespace(c) { contentEnd = i + 1l; }
128 715 i++;
129 715 }
130 93 return contentEnd;
131 }
132
133 /**
134 * Name of a table header line like `[name]`, `[ name ] # comment` or `[[name]]`. Empty if the line is no header.
135 */
136 60 f<String> headerName(const String& trimmed) {
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if !trimmed.startsWith("[") { return String(""); }
138 60 const bool arrayHeader = trimmed.startsWith("[[");
139 60 const unsigned long start = arrayHeader ? 2l : 1l;
140 60 const long close = trimmed.find(']', start);
141
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if close < 0l { return String(""); }
142 60 const String name = trimmed.getSubstring(start, close - cast<long>(start));
143 120 return name.trim();
144 60 }
145
146 27 public f<ManifestLayout> scanManifest(const String& text) {
147 27 ManifestLayout layout;
148 27 layout.lines = splitManifestLines(text);
149 27 LexState state;
150 27 String section;
151
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 27 times.
240 for unsigned long lineIdx = 0l; lineIdx < layout.lines.getSize(); lineIdx++ {
152 213 const String& line = layout.lines.get(lineIdx);
153
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 210 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 208 times.
213 if !state.openMultiline.isEmpty() || state.depth > 0l {
154 5 lexLine(line, 0l, state);
155 5 continue;
156 }
157 208 const String trimmed = line.trim();
158
4/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 172 times.
✓ Branch 2 taken 58 times.
✓ Branch 3 taken 150 times.
208 if trimmed.isEmpty() || trimmed.startsWith("#") { continue; }
159
160
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 90 times.
150 if trimmed.startsWith("[") {
161 60 section = headerName(trimmed);
162
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 35 times.
60 if section == SECTION_DEPENDENCIES { layout.dependenciesHeader = cast<long>(lineIdx); }
163
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 54 times.
60 if section == SECTION_DEV_DEPENDENCIES { layout.devDependenciesHeader = cast<long>(lineIdx); }
164
4/4
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 59 times.
60 if section.startsWith("dependencies.") || section.startsWith("dev-dependencies.") {
165 1 layout.problems.pushBack(String("line ") + toString(lineIdx + 1l) + ": dependencies must be written as one " +
166 1 "line each, e.g. name = { github = \"owner/repo\" }, not as [" + section + "]");
167 1 }
168 60 continue;
169 60 }
170
171
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 33 times.
90 const bool dependencySection = section == SECTION_DEPENDENCIES || section == SECTION_DEV_DEPENDENCIES;
172
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 39 times.
90 if !dependencySection {
173 51 lexLine(line, 0l, state);
174 51 continue;
175 }
176
177 // A dependency line: <bare key> = <value> [# comment]
178 39 unsigned long pos = 0l;
179
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
39 while pos < line.getLength() && isWhitespace(line[pos]) { pos++; }
180 39 const unsigned long keyStart = pos;
181
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 123 times.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 39 times.
123 while pos < line.getLength() && isBareKeyChar(line[pos]) { pos++; }
182 39 const String key = line.getSubstring(keyStart, cast<long>(pos - keyStart));
183
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 39 times.
76 while pos < line.getLength() && isWhitespace(line[pos]) { pos++; }
184
5/6
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 37 times.
39 if key.isEmpty() || pos >= line.getLength() || line[pos] != '=' {
185 2 layout.problems.pushBack(String("line ") + toString(lineIdx + 1l) + ": dependencies must be written as " +
186 2 "name = value, with a bare name (no quotes, no dots)");
187 2 lexLine(line, pos, state);
188 2 continue;
189 2 }
190 37 pos++;
191
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 37 times.
74 while pos < line.getLength() && isWhitespace(line[pos]) { pos++; }
192 37 DependencyLine entry;
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 entry.name = key;
194 37 entry.dev = section == SECTION_DEV_DEPENDENCIES;
195 37 entry.line = lineIdx;
196 37 entry.valueStart = pos;
197 37 entry.valueEnd = lexLine(line, pos, state);
198
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
37 if !state.openMultiline.isEmpty() || state.depth > 0l {
199 ✗ layout.problems.pushBack(String("line ") + toString(lineIdx + 1l) + ": the value of dependency '" + key +
200 ✗ "' has to fit on one line");
201 ✗ }
202 37 layout.entries.pushBack(entry);
203 208 }
204 54 return layout;
205 27 }
206
207 26 public f<long> ManifestLayout.indexOf(const String& name) {
208
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 5 times.
42 for unsigned long i = 0l; i < this.entries.getSize(); i++ {
209
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 16 times.
37 if this.entries.get(i).name == name { return cast<long>(i); }
210 16 }
211 5 return -1l;
212 }
213
214 /**
215 * Lines with the given line replaced (replace = true) or removed
216 */
217 5 f<Vector<String>> withLine(const Vector<String>& lines, unsigned long target, const String& replacement, bool replace) {
218 5 Vector<String> edited;
219
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 5 times.
50 for unsigned long i = 0l; i < lines.getSize(); i++ {
220
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 5 times.
45 if i != target { edited.pushBack(lines.get(i)); continue; }
221
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 if replace { edited.pushBack(replacement); }
222 5 }
223 5 return edited;
224 }
225
226 /**
227 * Text with the dependency line of `name` removed. Returns the text unchanged if there is no such line.
228 */
229 4 public f<String> removeDependencyLine(const String& text, const String& name) {
230 4 const ManifestLayout layout = scanManifest(text);
231 4 const long idx = layout.indexOf(name);
232
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if idx < 0l { return text; }
233 4 const DependencyLine& entry = layout.entries.get(cast<unsigned long>(idx));
234 8 return joinManifestLines(withLine(layout.lines, entry.line, String(""), false));
235 4 }
236
237 /**
238 * Text with the dependency `name` set to `value` (already formatted as TOML) in the given section. An existing line in the
239 * same section keeps its position and comment; a line in the other section is moved.
240 */
241 8 public f<String> setDependencyLine(const String& text, const String& name, const String& value, bool dev) {
242 8 ManifestLayout layout = scanManifest(text);
243 8 const long idx = layout.indexOf(name);
244
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
8 if idx >= 0l {
245 3 const DependencyLine& entry = layout.entries.get(cast<unsigned long>(idx));
246
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 if entry.dev == dev {
247 1 const String& line = layout.lines.get(entry.line);
248 1 const String updated = line.getSubstring(0l, cast<long>(entry.valueStart)) + value + line.getSubstring(entry.valueEnd);
249 2 return joinManifestLines(withLine(layout.lines, entry.line, updated, true));
250 1 }
251 2 layout = scanManifest(removeDependencyLine(text, name));
252 2 }
253
254 7 const String newLine = name + " = " + value;
255
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 const long header = dev ? layout.devDependenciesHeader : layout.dependenciesHeader;
256
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 if header < 0l {
257 3 Vector<String> lines = layout.lines;
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if lines.getSize() > 0l {
259 3 const String& lastLine = lines.back();
260 3 const String lastTrimmed = lastLine.trim();
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if !lastTrimmed.isEmpty() { lines.pushBack(String("")); }
262 3 }
263
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 lines.pushBack(String("[") + (dev ? SECTION_DEV_DEPENDENCIES : SECTION_DEPENDENCIES) + "]");
264 3 lines.pushBack(newLine);
265 6 return joinManifestLines(lines);
266 3 }
267 // Insert after the last dependency of the section, or right after the header if it has none
268 4 unsigned long insertAfter = cast<unsigned long>(header);
269
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 for unsigned long i = 0l; i < layout.entries.getSize(); i++ {
270 3 const DependencyLine& entry = layout.entries.get(i);
271
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if entry.dev == dev && entry.line > insertAfter { insertAfter = entry.line; }
272 3 }
273 4 Vector<String> lines = layout.lines;
274 4 lines.insertAt(insertAfter + 1l, newLine);
275 8 return joinManifestLines(lines);
276 8 }
277
278 1 #[test]
279 f<bool> testScanManifest() {
280 1 const String text = String("[package]\nname = \"x\" # [dependencies]\ndescription = \"\"\"\n[dependencies]\nnot = \"a dep\"\n\"\"\"\n") +
281 1 "authors = [\n \"a\",\n]\n\n[dependencies]\n# comment\na = \"^1.0.0\" # pinned\nb-c = { github = \"o/r\", rev = \"x#y\" }\n" +
282 1 "\n[ dev-dependencies ]\nt = '1.x'\n";
283 1 const ManifestLayout layout = scanManifest(text);
284 1 assert layout.problems.getSize() == 0l;
285 1 assert layout.entries.getSize() == 3l;
286 1 assert layout.dependenciesHeader == 10l;
287 1 assert layout.devDependenciesHeader == 15l;
288 1 const DependencyLine& a = layout.entries.get(0l);
289 1 assert a.name == "a" && !a.dev && a.line == 12l;
290 1 const String& aLine = layout.lines.get(a.line);
291 1 assert aLine.getSubstring(a.valueStart, cast<long>(a.valueEnd - a.valueStart)) == "\"^1.0.0\"";
292 1 const DependencyLine& b = layout.entries.get(1l);
293 1 const String& bLine = layout.lines.get(b.line);
294 1 assert bLine.getSubstring(b.valueStart, cast<long>(b.valueEnd - b.valueStart)) == "{ github = \"o/r\", rev = \"x#y\" }";
295 1 const DependencyLine& t = layout.entries.get(2l);
296 1 assert t.name == "t" && t.dev;
297 1 return true;
298 1 }
299
300 1 #[test]
301 f<bool> testScanManifestProblems() {
302 1 const ManifestLayout dotted = scanManifest(String("[dependencies]\na.version = \"1\"\n"));
303 1 assert dotted.problems.getSize() == 1l;
304 1 const ManifestLayout quoted = scanManifest(String("[dependencies]\n\"a\" = \"1\"\n"));
305 1 assert quoted.problems.getSize() == 1l;
306 1 const ManifestLayout subTable = scanManifest(String("[dependencies.a]\nversion = \"1\"\n"));
307 1 assert subTable.problems.getSize() == 1l;
308 1 return true;
309 1 }
310
311 1 #[test]
312 f<bool> testManifestLines() {
313 1 const Vector<String> lines = splitManifestLines(String("a\r\n\nb\n"));
314 1 assert lines.getSize() == 3l && lines.get(1l) == "" && lines.get(2l) == "b";
315 1 assert joinManifestLines(lines) == "a\n\nb\n";
316 1 return true;
317 1 }
318
319 1 #[test]
320 f<bool> testEditDependencyLines() {
321 1 const String text = String("# my project\n[package]\nname = \"x\"\n\n[dependencies]\na = \"^1.0.0\" # keep me\nb = \"*\"\n\n# tail\n");
322 // Change in place, keeping the comment
323 1 const String changed = setDependencyLine(text, String("a"), String("\"^2.0.0\""), false);
324 1 assert changed == "# my project\n[package]\nname = \"x\"\n\n[dependencies]\na = \"^2.0.0\" # keep me\nb = \"*\"\n\n# tail\n";
325 // Add to an existing section
326 1 const String added = setDependencyLine(text, String("c"), String("{ path = \"../c\" }"), false);
327 1 assert added == "# my project\n[package]\nname = \"x\"\n\n[dependencies]\na = \"^1.0.0\" # keep me\nb = \"*\"\nc = { path = \"../c\" }\n\n# tail\n";
328 // Move to a section that does not exist yet
329 1 const String moved = setDependencyLine(text, String("b"), String("\"*\""), true);
330 1 assert moved == "# my project\n[package]\nname = \"x\"\n\n[dependencies]\na = \"^1.0.0\" # keep me\n\n# tail\n\n[dev-dependencies]\nb = \"*\"\n";
331 // Add to an empty section
332 1 const String empty = setDependencyLine(String("[package]\n[dependencies]\n[tool.x]\n"), String("a"), String("\"1\""), false);
333 1 assert empty == "[package]\n[dependencies]\na = \"1\"\n[tool.x]\n";
334 // Remove
335 1 const String removed = removeDependencyLine(text, String("a"));
336 1 assert removed == "# my project\n[package]\nname = \"x\"\n\n[dependencies]\nb = \"*\"\n\n# tail\n";
337 1 return true;
338 1 }
339