devscout-rs 0.10.0

Fast code index for C# and TypeScript codebases -- map, find, refs, impact from the CLI.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! The `src/` source texts every page check reads.
//!
//! Two things are applied before anything is read out of a file, and both are
//! part of the check's own scope rather than an implementation detail:
//!
//! - **Non-test source.** `tests.rs`, `*_tests.rs` and any path with a `tests/`
//!   or `*_tests/` directory component are skipped outright, and every
//!   `#[cfg(test)] mod … { … }` body is removed from the files that stay: a
//!   fact only a test asserts is not a fact the pages describe.
//! - **Comments and literal bodies are blanked, not removed.** Every byte
//!   offset survives, so a hit is still reported at the line a reader sees, and
//!   a usage line quoted inside a comment cannot stand in for a usage constant.
//!
//! Everything here fails closed. An unterminated literal or block comment, a
//! string escape the scanner does not know, a brace it cannot balance, or a
//! walk that finds no file at all is an error rather than an empty result,
//! because an empty result is exactly what would let a completeness check pass
//! on a tree it never read.

use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};

/// A scan that could not be completed, with the file and detail that stopped
/// it.
#[derive(Debug)]
pub struct ScanError {
    pub path: PathBuf,
    pub detail: String,
}

impl fmt::Display for ScanError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}: {}", self.path.display(), self.detail)
    }
}

/// One string literal's value and the byte offset it started at.
#[derive(Debug)]
pub struct Literal {
    pub offset: usize,
    pub value: String,
}

/// One scanned file.
#[derive(Debug)]
pub struct Scanned {
    /// Path relative to the scanned root.
    pub path: PathBuf,
    /// Source text with comments, literal bodies and `#[cfg(test)]` module
    /// bodies blanked -- every offset preserved.
    pub blanked: String,
    pub literals: Vec<Literal>,
}

impl Scanned {
    /// The one-based line an offset falls on.
    pub fn line_of(&self, offset: usize) -> usize {
        self.blanked[..offset].matches('\n').count() + 1
    }

    /// Every literal value in this file, in source order.
    pub fn values(&self) -> impl Iterator<Item = &str> {
        self.literals.iter().map(|literal| literal.value.as_str())
    }
}

/// Whether a source path is test-only, applied to the path relative to the
/// scanned root: `cli/tests/args.rs`, `cli/tests.rs` and `store_tests/mod.rs`
/// are test-only, `cli/args.rs` and `attest.rs` are not.
pub fn is_test_path(relative: &str) -> bool {
    let components: Vec<&str> = relative.split('/').collect();
    let (last, parents) = components.split_last().expect("a path has components");
    for component in parents {
        if *component == "tests" || component.ends_with("_tests") {
            return true;
        }
    }
    *last == "tests.rs" || last.ends_with("_tests.rs")
}

/// Every non-test `.rs` file under `root`, in path order.
pub fn scan_tree(root: &Path) -> Result<Vec<Scanned>, ScanError> {
    let mut files = Vec::new();
    collect(root, root, &mut files)?;
    files.sort_by(|left, right| left.path.cmp(&right.path));
    if files.is_empty() {
        return Err(ScanError {
            path: root.to_path_buf(),
            detail: "no non-test Rust source found under this root".to_string(),
        });
    }
    Ok(files)
}

fn collect(root: &Path, directory: &Path, into: &mut Vec<Scanned>) -> Result<(), ScanError> {
    let mut paths: Vec<PathBuf> = Vec::new();
    let entries = fs::read_dir(directory).map_err(|error| ScanError {
        path: directory.to_path_buf(),
        detail: format!("cannot list directory: {error}"),
    })?;
    for entry in entries {
        let entry = entry.map_err(|error| ScanError {
            path: directory.to_path_buf(),
            detail: format!("cannot read directory entry: {error}"),
        })?;
        paths.push(entry.path());
    }
    paths.sort();
    for path in paths {
        let name = path
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or_default()
            .to_string();
        let relative = path
            .strip_prefix(root)
            .unwrap_or(&path)
            .to_string_lossy()
            .replace('\\', "/");
        if path.is_dir() {
            if name == "tests" || name.ends_with("_tests") {
                continue;
            }
            collect(root, &path, into)?;
            continue;
        }
        if !name.ends_with(".rs") || is_test_path(&relative) {
            continue;
        }
        let text = fs::read_to_string(&path).map_err(|error| ScanError {
            path: path.clone(),
            detail: format!("cannot read file: {error}"),
        })?;
        let (blanked, literals) = analyze(&path, &text)?;
        let (blanked, dropped) = drop_cfg_test_modules(&path, &blanked)?;
        let literals = literals
            .into_iter()
            .filter(|literal| {
                !dropped
                    .iter()
                    .any(|(start, end)| *start <= literal.offset && literal.offset < *end)
            })
            .collect();
        into.push(Scanned {
            path: PathBuf::from(relative),
            blanked,
            literals,
        });
    }
    Ok(())
}

// One pass over the text. Comments, literal bodies and `#[cfg(test)]` bodies
// are blanked in place -- newlines kept, every other byte replaced by a space
// -- so an offset in the result is still an offset in the file on disk.
fn analyze(path: &Path, text: &str) -> Result<(String, Vec<Literal>), ScanError> {
    let bytes = text.as_bytes();
    let mut blanked = String::with_capacity(text.len());
    let mut literals = Vec::new();
    let mut index = 0usize;
    while index < bytes.len() {
        let byte = bytes[index];
        if byte == b'/' && bytes.get(index + 1) == Some(&b'/') {
            let end = text[index..]
                .find('\n')
                .map(|at| index + at)
                .unwrap_or(bytes.len());
            blank(&mut blanked, &text[index..end]);
            index = end;
            continue;
        }
        if byte == b'/' && bytes.get(index + 1) == Some(&b'*') {
            let end = block_comment_end(path, text, index)?;
            blank(&mut blanked, &text[index..end]);
            index = end;
            continue;
        }
        if let Some((opening, hashes)) = raw_string_start(text, index) {
            let end = raw_string_end(path, text, opening, hashes)?;
            literals.push(Literal {
                offset: index,
                value: text[opening + 1..end - 1 - hashes].to_string(),
            });
            blank(&mut blanked, &text[index..end]);
            index = end;
            continue;
        }
        if byte == b'"' {
            let (end, raw) = string_end(path, text, index)?;
            literals.push(Literal {
                offset: index,
                value: unescape(path, raw)?,
            });
            blank(&mut blanked, &text[index..end]);
            index = end;
            continue;
        }
        if byte == b'\'' {
            if let Some(end) = char_literal_end(text, index) {
                blank(&mut blanked, &text[index..end]);
                index = end;
                continue;
            }
        }
        // Every other byte, the `b` of `b"…"` and `br#"…"#` included, is code.
        let character = text[index..].chars().next().unwrap_or('\0');
        blanked.push(character);
        index += character.len_utf8();
    }
    Ok((blanked, literals))
}

fn blank(into: &mut String, slice: &str) {
    for character in slice.chars() {
        into.push(if character == '\n' { '\n' } else { ' ' });
    }
}

fn block_comment_end(path: &Path, text: &str, start: usize) -> Result<usize, ScanError> {
    let bytes = text.as_bytes();
    let mut depth = 1usize;
    let mut cursor = start + 2;
    while cursor < bytes.len() {
        if bytes[cursor] == b'/' && bytes.get(cursor + 1) == Some(&b'*') {
            depth += 1;
            cursor += 2;
        } else if bytes[cursor] == b'*' && bytes.get(cursor + 1) == Some(&b'/') {
            depth -= 1;
            cursor += 2;
            if depth == 0 {
                return Ok(cursor);
            }
        } else {
            cursor += 1;
        }
    }
    Err(ScanError {
        path: path.to_path_buf(),
        detail: format!(
            "unterminated block comment opened at line {}",
            text[..start].matches('\n').count() + 1
        ),
    })
}

// `r"…"`, `r#"…"#`, `br#"…"#`: the hash count is the delimiter, so the literal
// ends at the first `"` followed by exactly that many `#`.
fn raw_string_start(text: &str, index: usize) -> Option<(usize, usize)> {
    let bytes = text.as_bytes();
    let mut cursor = index;
    if bytes.get(cursor) == Some(&b'b') {
        cursor += 1;
    }
    if bytes.get(cursor) != Some(&b'r') {
        return None;
    }
    cursor += 1;
    let mut hashes = 0usize;
    while bytes.get(cursor) == Some(&b'#') {
        hashes += 1;
        cursor += 1;
    }
    if bytes.get(cursor) == Some(&b'"') {
        Some((cursor, hashes))
    } else {
        None
    }
}

fn raw_string_end(
    path: &Path,
    text: &str,
    opening: usize,
    hashes: usize,
) -> Result<usize, ScanError> {
    let terminator = format!("\"{}", "#".repeat(hashes));
    match text[opening + 1..].find(&terminator) {
        Some(at) => Ok(opening + 1 + at + terminator.len()),
        None => Err(ScanError {
            path: path.to_path_buf(),
            detail: format!(
                "unterminated raw string opened at line {}",
                text[..opening].matches('\n').count() + 1
            ),
        }),
    }
}

// The offset just past the closing quote, and the raw body between the quotes.
// A backslash skips the byte after it, so `\"` never ends the literal and a
// trailing backslash-newline line continuation survives to `unescape`.
fn string_end<'a>(path: &Path, text: &'a str, start: usize) -> Result<(usize, &'a str), ScanError> {
    let bytes = text.as_bytes();
    let mut cursor = start + 1;
    while cursor < bytes.len() {
        match bytes[cursor] {
            b'\\' => cursor += 2,
            b'"' => return Ok((cursor + 1, &text[start + 1..cursor])),
            _ => cursor += 1,
        }
    }
    Err(ScanError {
        path: path.to_path_buf(),
        detail: format!(
            "unterminated string opened at line {}",
            text[..start].matches('\n').count() + 1
        ),
    })
}

// A char literal is a quote, one character or one escape, then a quote. A
// lifetime (`'a`, `'static`) has no closing quote and stays code.
fn char_literal_end(text: &str, start: usize) -> Option<usize> {
    let rest = &text[start + 1..];
    let mut characters = rest.char_indices();
    let (_, first) = characters.next()?;
    if first == '\\' {
        let (at, escape) = characters.next()?;
        if escape == 'u' || escape == 'x' {
            let close = rest.find('\'')?;
            return Some(start + 1 + close + 1);
        }
        let (close, quote) = characters.next()?;
        return (quote == '\'').then_some(start + 1 + close + len_of(quote, at));
    }
    if first == '\'' {
        return None;
    }
    let (at, character) = characters.next()?;
    (character == '\'').then_some(start + 1 + at + character.len_utf8())
}

fn len_of(character: char, fallback: usize) -> usize {
    let _ = fallback;
    character.len_utf8()
}

fn unescape(path: &Path, raw: &str) -> Result<String, ScanError> {
    let mut value = String::with_capacity(raw.len());
    let mut characters = raw.chars();
    while let Some(character) = characters.next() {
        if character != '\\' {
            value.push(character);
            continue;
        }
        let Some(escape) = characters.next() else {
            return Err(ScanError {
                path: path.to_path_buf(),
                detail: "string ends in a lone backslash".to_string(),
            });
        };
        match escape {
            'n' => value.push('\n'),
            'r' => value.push('\r'),
            't' => value.push('\t'),
            '0' => value.push('\0'),
            '\\' => value.push('\\'),
            '\'' => value.push('\''),
            '"' => value.push('"'),
            // A backslash before a newline drops the newline and the leading
            // whitespace of the next line. No constant in this repository
            // depends on that whitespace, and the value has to stay comparable
            // with the bytes the constant holds.
            '\n' => {
                while matches!(characters.clone().next(), Some(' ') | Some('\t')) {
                    characters.next();
                }
            }
            // `\x41` takes two hex digits; `\u{1f600}` takes a braced run.
            'x' | 'u' => {
                let mut digits = String::new();
                if escape == 'x' {
                    for _ in 0..2 {
                        let Some(digit) = characters.next() else {
                            break;
                        };
                        digits.push(digit);
                    }
                } else {
                    if characters.next() != Some('{') {
                        return Err(ScanError {
                            path: path.to_path_buf(),
                            detail: "a unicode escape is not braced".to_string(),
                        });
                    }
                    loop {
                        match characters.next() {
                            Some('}') => break,
                            Some(digit) => digits.push(digit),
                            None => {
                                return Err(ScanError {
                                    path: path.to_path_buf(),
                                    detail: "a unicode escape is not closed".to_string(),
                                })
                            }
                        }
                    }
                }
                match u32::from_str_radix(&digits, 16)
                    .ok()
                    .and_then(char::from_u32)
                {
                    Some(decoded) => value.push(decoded),
                    None => {
                        return Err(ScanError {
                            path: path.to_path_buf(),
                            detail: format!("cannot decode the escape \\{escape}{digits}"),
                        })
                    }
                }
            }
            other => {
                return Err(ScanError {
                    path: path.to_path_buf(),
                    detail: format!("unknown string escape \\{other}"),
                })
            }
        }
    }
    Ok(value)
}

// `#[cfg(test)] mod … { … }` bodies go after blanking, so no check reads a
// number or a name that only a test carries. An unbalanced brace is an error
// rather than a silently short body.
fn drop_cfg_test_modules(
    path: &Path,
    text: &str,
) -> Result<(String, Vec<(usize, usize)>), ScanError> {
    let mut blanked = text.to_string();
    let mut dropped = Vec::new();
    let mut search_from = 0usize;
    while let Some(attribute) = blanked[search_from..]
        .find("#[cfg(test)]")
        .map(|at| at + search_from)
    {
        let Some(brace) = blanked[attribute..].find('{').map(|at| at + attribute) else {
            break;
        };
        let Some(end) = matching_brace(&blanked, brace) else {
            return Err(ScanError {
                path: path.to_path_buf(),
                detail: format!(
                    "unbalanced brace after the test module at line {}",
                    text[..attribute].matches('\n').count() + 1
                ),
            });
        };
        let replacement: String = blanked[attribute..=end]
            .chars()
            .map(|character| if character == '\n' { '\n' } else { ' ' })
            .collect();
        blanked.replace_range(attribute..=end, &replacement);
        dropped.push((attribute, end + 1));
        search_from = end + 1;
    }
    Ok((blanked, dropped))
}

fn matching_brace(text: &str, open: usize) -> Option<usize> {
    let mut depth = 0usize;
    for (offset, character) in text[open..].char_indices() {
        match character {
            '{' => depth += 1,
            '}' => {
                depth -= 1;
                if depth == 0 {
                    return Some(open + offset);
                }
            }
            _ => {}
        }
    }
    None
}