pub struct RuleRoot { /* private fields */ }Expand description
Where rule modules live, and what may be imported.
Implementations§
Source§impl RuleRoot
impl RuleRoot
Sourcepub fn new(root: impl AsRef<Path>) -> Result<Self, ResolveError>
pub fn new(root: impl AsRef<Path>) -> Result<Self, ResolveError>
Anchor resolution at a directory.
§Errors
Fails if the directory does not exist or cannot be canonicalized.
Sourcepub const fn with_builtins(self, builtins: BuiltinSource) -> Self
pub const fn with_builtins(self, builtins: BuiltinSource) -> Self
Serve built-in rules from embedded sources.
Built-ins resolve before anything on disk, so a project file cannot shadow one — a rule whose behavior depended on whether a same-named file happened to exist would be impossible to reason about.
Sourcepub fn resolve(
&self,
base: &str,
specifier: &str,
) -> Result<PathBuf, ResolveError>
pub fn resolve( &self, base: &str, specifier: &str, ) -> Result<PathBuf, ResolveError>
Resolve a specifier against the module that imported it.
§Errors
Returns ResolveError for a bare specifier, an escape from the root, or a
specifier matching no file.
Sourcepub fn read(
&self,
path: &Path,
typescript: &dyn Language,
javascript: &dyn Language,
) -> Result<String, ResolveError>
pub fn read( &self, path: &Path, typescript: &dyn Language, javascript: &dyn Language, ) -> Result<String, ResolveError>
Read a resolved module, stripping types when it is TypeScript.
Containment is re-checked here rather than trusted from RuleRoot::resolve.
Reading is the operation that actually touches a file, so it should be the thing
that enforces the boundary — otherwise the guarantee depends on every caller having
gone through the resolver first, which is exactly the sort of assumption that holds
until someone adds a second caller.
§Errors
Returns ResolveError::EscapesRoot if the path is outside the root, or
ResolveError::Unreadable if the file cannot be read or stripping rejects it.