Expand description
Load partial values from multiple external sources into one parse result, as a plugin for the tabnas parsing engine over the jsonic relaxed-JSON grammar.
A directive character (@ by default) marks a reference in the
input. The plugin resolves the reference to a source, processes
that source into a value, and splices the value into the output,
recursively, so a loaded source can reference more sources.
use tabnas_multisource::{make_with, MapResolver, MultiSourceOptions};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let sources = MapResolver::from([("a.jsonic", "a:1")]);
let parser = make_with(MultiSourceOptions::new(sources));
assert_eq!(parser.parse(r#"@"a.jsonic" b:2"#)?.to_string(), r#"{"a":1,"b":2}"#);
Ok(())
}TypeScript is canonical: ts/src/multisource.ts defines behaviour,
option names and defaults. The shared fixtures in test/spec/*.tsv
are the parity contract across TypeScript, Go and Rust. Differences
are recorded in DIVERGENCE.md at the repository root.
§Untrusted input
Following @ references is this plugin’s whole job, which is exactly
why the boundary matters. A reference reaches only what the configured
Resolver can see, so the resolver is the sandbox:
MapResolverand aFileResolverover aMapFstouch no disk at all.FileResolver::with_rootconfines every candidate path to one directory, so@"../../etc/passwd"resolves to nothing rather than to a file. The root and the candidate are both resolved through the filesystem before they are compared, so a symbolic link inside the root is judged by where it points; a link created between that check and the read is beyond what a resolver can see.- A cycle between sources raises
multisource_cycle, and a chain longer thanMultiSourceOptions::max_depthraisesmultisource_depth, so neither hangs nor overflows the stack.
A loaded value is still data, never instructions, and splicing is not sanitising: escaping for SQL, HTML or a shell remains the caller’s job.
Structs§
- Dependency
- One record that target
tarpulled in sourcesrcduring a parse. - DirEntry
- One entry of a directory listing.
- File
Resolver - Resolve references against a filesystem.
- MapFs
- An in-memory filesystem: relative, slash-separated paths to content.
- MapResolver
- Resolve references against a map of path to content.
- Multi
Source Error - A registration failure: a host grammar without the rules the plugin modifies, a directive character already taken, or a grammar the engine refused.
- Multi
Source Options - Configuration for the plugin.
- OsFs
- The real filesystem, addressed by absolute native paths.
- Path
Spec - The path to a source, including the base prefix when there is one.
- PkgResolver
- Resolve references inside
node_modulesfolders. - Preload
Options - Which folders to scan, and for what.
- Processor
Input - What a
Processoris given. - Resolution
- A source and where it was found.
- Resolver
Input - What a
Resolveris given: the plugin options and the parse metadata for the enclosing source.
Enums§
- Processor
Entry - How one source kind becomes a value.
Constants§
- DEFAULT_
MARKCHAR - The default directive character.
- DEFAULT_
MAX_ DEPTH - How deep a chain of sources may nest before the parse is failed.
- NONE
- An unknown or missing source-reference extension.
- PLUGIN_
NAME - The plugin name, as the engine records it.
- RULE_
NAME - The rule and directive this plugin installs.
- TOP
- The top of the dependency tree: the
DependencyMaptarget key used for sources referenced directly by the top-level parse. - VERSION
- VERSION is this crate’s version. It MUST equal
ts/package.json“version” and theversionfield inrs/Cargo.toml: the release orchestrator rewrites all of them, andtests/version_test.rsfails the build if they drift. MirrorsVERSIONints/src/multisource.tsandconst VERSIONingo/multisource.go.
Traits§
- Processor
- Convert resolved source content into a value.
- Resolver
- Find the source content a reference names.
- Source
Fs - A read-only view of a filesystem.
Functions§
- build_
potentials - Every path a reference could name: the path itself, then the implicit extensions, then a folder index file, then a folder index file that repeats the folder name.
- default_
implicit_ ext - The default implicit extensions.
- default_
processor - The fallback: the source text, as a string.
- default_
processors - The default processor table: raw text for an unknown kind, the strict
JSON reader for
json, and a re-parse with the live engine forjsonicandjsc. - ext_
kind - The source kind: the extension of the LAST path segment, without the
dot, or
NONEwhen that segment has no dot. - json_
processor - Read the source as standard JSON.
- jsonic_
processor - Re-parse the source with the live engine, so a nested reference is resolved by the same plugin and every plugin installed beside it.
- make
- A jsonic parser with the plugin installed over default options: no
sources, so every reference raises
multisource_not_found. - make_
with - A jsonic parser with the plugin installed over
options. - multisource
- Install the plugin on
parser. - os_fs
- The real filesystem as a shared handle.
- parse
- Parse
srcwithoptions. - parse_
nested - Parse
srcas a source nesteddepthlevels inside the document. - plugin
- Build the plugin with default options: no sources, so every reference
raises
multisource_not_found. - plugin_
with - Build the plugin for
options. - preload_
files - Scan the folders in
optionson the real filesystem, returning full path to content for every file matching one of the extensions. - preload_
files_ with - Scan the folders in
optionsonfilesystem. - resolve_
path_ spec - Normalize a reference into a
PathSpecagainstbase. - source_
dir - The directory a relative reference inside
pathresolves against.
Type Aliases§
- Dependency
Map - A flattened dependency tree, keyed by target full path and then by source full path.
- Shared
Fs - A shared filesystem handle.
- Shared
Resolver - A shared resolver handle.