genfile 0.6.0

CLI for genfile_core template archive management - create, manage, and materialize code generation templates.
Documentation
//! Shared state for all command handlers
//!
//! Provides thread-local storage for the current archive that is shared
//! across all handler modules (archive, file, etc.)
//!
//! # ⚠️ TEMPORARY WORKAROUND IMPLEMENTATION
//!
//! **Status:** This is the ACTUAL state management implementation currently in use.
//!
//! **Why This Exists:** `unilang::ExecutionContext` does not yet support passing custom
//! state to command handlers. This thread-local storage is the workaround to share
//! archive state across handler function calls within a single CLI invocation or REPL
//! session. See Workaround comments in `main.rs` and `repl.rs` for context.
//!
//! **Trade-offs of Thread-Local Approach:**
//! - ✅ Works with current unilang API constraints
//! - ✅ Simple, no lifetime/borrow complexity
//! - ⚠️ Thread-isolated (each thread has separate state)
//! - ⚠️ Global mutable state (less explicit than passing through context)
//! - ⚠️ Architectural debt vs. specification design
//!
//! **Migration Path:** When `ExecutionContext` gains state support, pass the archive
//! through context in each handler and delete this module entirely.
//!
//! ## Architectural Validation (2025-01-25)
//!
//! Comprehensive exploration of unilang codebase confirms this approach is correct:
//!
//! - **`ExecutionContext` Status**: Currently empty placeholder struct with no fields
//! - **Unilang Philosophy**: Explicitly stateless by design per pipeline.rs docs
//! - **Use Case Fit**: Single-threaded REPL doesn't require cross-thread state sharing
//!
//! **Conclusion**: This thread-local pattern is the recommended approach given current
//! unilang API constraints. Delete this module once `ExecutionContext` supports state.

use core::cell::RefCell;
use genfile_core::TemplateArchive;

thread_local! {
  static CURRENT_ARCHIVE : RefCell< Option< TemplateArchive > > = const { RefCell::new( None ) };
}

pub fn get_current_archive() -> Option< TemplateArchive >
{
  CURRENT_ARCHIVE.with( | arc | arc.borrow().clone() )
}

pub fn set_current_archive( archive : TemplateArchive )
{
  CURRENT_ARCHIVE.with( | arc | *arc.borrow_mut() = Some( archive ) );
}