Skip to main content

Crate code_extract

Crate code_extract 

Source
Expand description

Deterministic structure extraction for source files.

Bytes in, facts out. This crate never opens a file, never walks a directory and never touches a database: everything it knows about the surrounding tree arrives through the caller’s closures. That is what makes the resulting graph reproducible — two runs over the same working tree produce byte-identical facts.

§What comes out

extract turns one file’s bytes into a FileFacts: a content hash, a line count, the symbols defined in the file (qualified in-file, with their doc line, signature and outgoing calls), the imports as written, and — for Markdown — headings, mentions and the body text.

§Resolving what came out

The extraction step deliberately keeps raw text. resolve_import, resolve_mention and resolve_call turn that raw text into paths and symbol keys, using caller-supplied lookups:

  • known(path) — true when path names a file in the working tree.
  • files_in(dir) — the working-tree paths of the files directly inside dir, empty when the directory does not exist.
  • by_basename(name) — every working-tree path whose file name is name.

All paths, in and out, are working-tree-relative files that use / separators. No result is ever a directory.

SymbolIndex keys are <file path>#<qualified symbol name>; the caller builds the index in that shape so resolve_call can prefer a definition in the calling file or its directory. resolve_call also takes the calling file’s resolved imports, which is what lets a call reach a definition in another crate whose name is not unique repository-wide — and is the only thing that can reach one at all for a call written on a receiver, which never falls back to repository-wide uniqueness.

A method is stored qualified — Store.flush — and written on a receiver — store.flush() — so the index files it under the bare name too. Those entries are read only where the receiver identifies the type, because stripping a receiver says which method is wanted and nothing about what it belongs to: see resolve_call.

§Determinism

Every returned collection is sorted and deduplicated, and nothing depends on hash-map iteration order. Output is capped so a pathological file cannot blow up the store: see MAX_FILE_BYTES, MAX_BODY_BYTES, MAX_TEXT_CHARS and MAX_CALLS.

Structs§

CallFact
One call as written, and enough about how it was written to resolve it.
CallScope
What the calling file can see, beyond the symbol index itself.
FileFacts
Everything one file contributes to the graph.
ImportFact
One import as written in the source.
SymbolFact
One definition found in a file.
SymbolIndex
Symbol keys grouped by symbol name, built by the caller.

Enums§

Lang
The languages this crate can read.

Constants§

MAX_BODY_BYTES
Upper bound on the stored Markdown body, in bytes.
MAX_CALLS
Upper bound on the calls recorded for one symbol.
MAX_FILE_BYTES
Files larger than this are reduced to hash, language and line count.
MAX_TEXT_CHARS
Upper bound, in characters, on a symbol signature or doc line.
PARSE_BUDGET_MS
Wall-clock budget for parsing one file.

Functions§

call_lookup_names
Every name resolve_call may look callee up under in the index.
extract
Extract the facts for one file.
indexed_under
Every name SymbolIndex files a definition called name under.
lang_of
Classify a path by its extension. Unknown extensions are Lang::Other, which yields hash-only facts.
resolve_call
Resolve a callee written in from_file to the key of the symbol it names.
resolve_import
Resolve one import to the working-tree paths it names.
resolve_mention
Resolve a Markdown mention to a working-tree path.
written_as_method
Whether a callee as written names a receiver rather than a path.