refactor(env): introduce VarName enum to unify nameref resolution API#1105
Open
lu-zero wants to merge 1 commit into
Open
refactor(env): introduce VarName enum to unify nameref resolution API#1105lu-zero wants to merge 1 commit into
lu-zero wants to merge 1 commit into
Conversation
d2d7e03 to
29508f6
Compare
Replace dual method pairs (update_or_add/update_or_add_bypassing_nameref, unset/unset_bypassing_nameref, etc.) with a single VarName-typed parameter that encodes resolution strategy: - VarName::Auto — follow nameref chains (default for &str) - VarName::Resolved — already resolved, lookup directly - VarName::Direct — bypass nameref resolution Split env.rs into env/mod.rs + env/names.rs to separate name types from environment logic.
29508f6 to
4f48c86
Compare
Owner
|
Thanks for helping with these changes! I've skimmed through and I'm going to start merging some of it into my working branch -- but there's a few pieces I'd like to think more about first (on the read-only lookup side). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This is a stacked PR on top of the
nameref-expandedbranch. It refactors the environment API to use aVarNameenum that encodes nameref resolution strategy in the type system, eliminating the dual-method pattern.Problem
The nameref implementation added parallel method pairs to
ShellEnvironment:update_or_add()/update_or_add_bypassing_nameref()unset()/unset_bypassing_nameref()update_or_add_array_element()/update_or_add_array_element_raw()IntoVarLookup/IntoVarLookupMuttraits +DirectVarLookup/DirectVarLookupMutbuilder typesEvery caller had to pick the right variant, and getting it wrong produced subtle bash-incompatibility bugs.
Solution
Introduced a
VarNameenum with three variants:&str/Stringconvert toVarName::Autoby default (backward compatible)ResolvedNameconverts toVarName::ResolvedviaFromimplVarName::direct("name")for the bypass caseEliminated methods:
update_or_add/update_or_add_bypassing_nameref→ singleupdate_or_add(impl Into<VarName>)unset/unset_bypassing_nameref→ singleunset(impl Into<VarName>)update_or_add_array_element/_raw→ single methodVarLookup/VarLookupMut)Module split:
env.rs(2128 lines) →env/mod.rs+env/names.rsLookup builder changes
env.lookup("name").get()env.lookup("name").bypassing_nameref().get()env.lookup("name").get_direct()env.lookup(&resolved).get()env.lookup(&resolved).get()(returnsResolvedVarRef) or.get_direct()for rawenv.lookup("name").bypassing_nameref().in_scope(p).get()env.lookup(VarName::direct("name")).in_scope(p).get_direct()Test results
VarName-specific tests)Files changed
brush-core/src/env/names.rs— new:VarName,ResolvedName, name validation functionsbrush-core/src/env/mod.rs— refactoredShellEnvironmentmethodsbrush-core/src/expansion.rs,interp.rs,extendedtests.rs,shell.rs— adapted callersbrush-builtins/src/{declare,export,unset,mapfile,read}.rs— adapted callersFuture work (Phase 2)
The
VarNameenum usesStringfields now. When we add string interning (e.g.,lasso), we can introduce aNamenewtype and swap the fields to interned keys, makingVarNameCopy.