Rust Preprocessor Advanced Pattern Support
Summary
The Rust preprocessor (rust_preprocessor/src/ast_preprocessor.rs) has been enhanced to support all major Python assignment patterns for automatic variable name injection into netherframe construct instantiations.
Implemented Patterns
1. Dictionary Literals ✓
# Before
resources = {"web": Resource(type="web"), "db": Resource(type="db")}
# After
resources = {"web": _nf_wrap_with_varname(Resource, "web", type="web"), "db": _nf_wrap_with_varname(Resource, "db", type="db")}
2. Dictionary Key Assignment ✓
# Before
resources["vpc"] = Resource(type="aws_vpc")
# After
resources["vpc"] = _nf_wrap_with_varname(Resource, "vpc", type="aws_vpc")
3. Walrus Operator ✓
# Before
if app := App():
pass
# After
if app := _nf_wrap_with_varname(App, "app"):
pass
4. Tuple Unpacking ✓
# Before
a, b = App(), Stack(App())
# After
a, b = _nf_wrap_with_varname(App, "a"), _nf_wrap_with_varname(Stack, "b", App())
5. Context Managers ✓
# Before
with App() as app:
pass
# After
with _nf_wrap_with_varname(App, "app") as app:
pass
6. Method Chains ✓
# Before
resource = Resource(type="test").configure()
# After
resource = _nf_wrap_with_varname(Resource, "resource", type="test").configure()
Implementation Details
The Rust preprocessor uses the ruff_python_ast crate to parse Python code and apply transformations:
- AST Visitor Pattern: Implements
Visitortrait to traverse the AST - Pattern Matching: Uses Rust's pattern matching to identify transformable constructs
- Preserve Formatting: Maintains exact source formatting and line numbers
- Import Management: Intelligently places import statements at module level
Test Coverage
Comprehensive test suite in tests/test_preprocessor_advanced_patterns.py:
- 38 test cases covering all implemented patterns
- Edge cases and error conditions
- Real-world infrastructure patterns
- All tests passing ✓
Performance
The Rust preprocessor is significantly faster than the Python implementation: - No Python AST overhead - Direct source text manipulation - Efficient pattern matching - Single-pass transformation
Patterns NOT Implemented
These lower-priority patterns were identified but not implemented: 1. List/set comprehensions with class instantiations 2. Chained assignments (a = b = Class()) 3. Starred expressions (*rest = ...)
These can be added if needed in the future.
Migration Status
- Preprocessor supports both Rust and libCST implementations
- Rust preprocessor is preferred for performance
preprocessor_selector.pyautomatically chooses best available implementation- libCST provides Python fallback when Rust is not available
- Full backward compatibility maintained