pasta_lua 0.2.0

Pasta Lua - Lua integration for Pasta DSL
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! Pasta Loader - Startup sequence orchestration.
//!
//! This module provides the integrated startup sequence for pasta_lua,
//! from directory discovery to runtime initialization.
//!
//! # Architecture
//!
//! - `PastaLoader` - Main entry point for startup sequence
//! - `PastaConfig` / `LoaderConfig` - Configuration file handling
//! - `LoaderContext` - Runtime initialization context
//! - `LoaderError` - Error types for startup sequence
//!
//! # Example
//!
//! ```rust,ignore
//! use pasta_lua::loader::PastaLoader;
//!
//! // Load from startup directory
//! let runtime = PastaLoader::load("ghost/master/")?;
//!
//! // Execute a scene
//! let result = runtime.exec("some_scene_call()")?;
//! ```

mod cache;
mod config;
mod context;
mod discovery;
mod error;
mod extract;

pub use cache::{CURRENT_VERSION, CacheManager};
pub use config::{
    LoaderConfig, LoggingConfig, LuaConfig, PastaConfig, PersistenceConfig, TalkConfig,
    default_libs, default_log_file_path, default_lua_search_paths,
};
pub use context::LoaderContext;
pub use error::{LoaderError, TranspileFailure};

use crate::context::TranspileContext;
use crate::runtime::{PastaLuaRuntime, RuntimeConfig};
use crate::transpiler::LuaTranspiler;

use std::fs;
use std::path::Path;
use tracing::{debug, error, info, warn};

/// Process statistics for logging.
struct ProcessStats {
    transpiled: usize,
    skipped: usize,
    failed: usize,
    copied: usize,
}

/// Pasta Loader - Unified startup sequence API.
///
/// Orchestrates the complete startup sequence:
/// 1. Load configuration from pasta.toml
///    1.5. Create logger with config, register, and update tracing filter
/// 2. Prepare profile directories and cache with version check
/// 3. Discover .pasta files in dic/*/*.pasta
/// 4. Incremental transpile (only changed files)
/// 5. Generate scene_dic.lua
/// 6. Initialize PastaLuaRuntime and load scene_dic
pub struct PastaLoader;

impl PastaLoader {
    /// Load runtime from startup directory with default configuration.
    ///
    /// This is the main entry point for the startup sequence.
    /// Executes all phases: config → discovery → transpile → runtime.
    ///
    /// # Arguments
    /// * `base_dir` - Startup directory path (ghost/master/ equivalent)
    ///
    /// # Returns
    /// * `Ok(PastaLuaRuntime)` - Initialized runtime ready for execution
    /// * `Err(LoaderError)` - Startup sequence failed
    pub fn load(base_dir: impl AsRef<Path>) -> Result<PastaLuaRuntime, LoaderError> {
        Self::load_with_config(base_dir, RuntimeConfig::new())
    }

    /// Load runtime from startup directory with custom runtime configuration.
    ///
    /// # Arguments
    /// * `base_dir` - Startup directory path
    /// * `runtime_config` - Custom runtime configuration
    ///
    /// # Returns
    /// * `Ok(PastaLuaRuntime)` - Initialized runtime
    /// * `Err(LoaderError)` - Startup sequence failed
    pub fn load_with_config(
        base_dir: impl AsRef<Path>,
        runtime_config: RuntimeConfig,
    ) -> Result<PastaLuaRuntime, LoaderError> {
        let base_dir = base_dir.as_ref();

        // Check if base directory exists
        if !base_dir.exists() {
            return Err(LoaderError::DirectoryNotFound(base_dir.to_path_buf()));
        }

        info!(path = %base_dir.display(), "Starting pasta loader");

        // Phase 1: Load configuration
        debug!("Phase 1: Loading configuration");
        let config = PastaConfig::load(base_dir)?;

        // Stage 1.5: Create logger with config and update tracing filter
        debug!("Stage 1.5: Applying logging configuration");
        let logger = Self::create_and_register_logger(base_dir, &config)?;

        // Phase 2: Prepare directories and cache (with version check)
        debug!("Phase 2: Preparing directories and cache");
        Self::prepare_directories(base_dir, &config.loader)?;
        let cache_manager =
            CacheManager::new(base_dir.to_path_buf(), &config.loader.transpiled_output_dir);
        cache_manager.prepare_cache_dir()?;

        // Phase 2.5: Self-deploy framework scripts (non-fatal)
        // base_dir is finalized by Phase 2; sync the embedded canonical pasta_scripts
        // to disk BEFORE Phase 3 / package.path construction (Req 6.1). Failure must
        // NOT abort startup (Req 3.2): log ERROR and continue with existing scripts.
        debug!("Phase 2.5: Self-deploying framework scripts");
        match extract::sync_pasta_scripts(base_dir) {
            Ok(_outcome) => {
                // Outcome already logged inside (DEBUG on skip / INFO on deploy).
            }
            Err(e) => {
                error!(
                    path = %base_dir.join("profile/pasta/pasta_scripts").display(),
                    error = %e,
                    "Self-deploy failed; continuing startup with existing scripts (version drift unresolved)"
                );
            }
        }

        // Phase 3: Discover files
        debug!("Phase 3: Discovering pasta and lua files");
        let (pasta_files, lua_files) =
            Self::discover_all_files(base_dir, &config.loader.pasta_patterns)?;
        let total_files = pasta_files.len() + lua_files.len();
        if total_files == 0 {
            warn!(path = %base_dir.display(), "No .pasta or .lua files found");
        } else {
            info!(
                pasta = pasta_files.len(),
                lua = lua_files.len(),
                "Found files"
            );
        }

        // Phase 4: Incremental process (transpile .pasta, copy .lua)
        debug!("Phase 4: Incremental processing");
        let (context, module_names, stats) =
            Self::process_incremental(base_dir, &pasta_files, &lua_files, &cache_manager)?;

        // Log statistics in debug mode
        if config.loader.debug_mode {
            info!(
                transpiled = stats.transpiled,
                skipped = stats.skipped,
                failed = stats.failed,
                copied = stats.copied,
                "Processing statistics"
            );
        }

        // Check for orphaned caches
        let all_source_files: Vec<_> = pasta_files
            .iter()
            .chain(lua_files.iter())
            .cloned()
            .collect();
        let orphans = cache_manager.find_orphaned_caches(&all_source_files);
        if !orphans.is_empty() && config.loader.debug_mode {
            for orphan in &orphans {
                warn!(path = %orphan.display(), "Orphaned cache file detected");
            }
        }

        // Phase 5: Generate scene_dic.lua
        debug!("Phase 5: Generating scene_dic.lua");
        let scene_dic_path = cache_manager.generate_scene_dic(&module_names)?;

        // Phase 6: Initialize runtime and load scene_dic
        debug!("Phase 6: Initializing runtime");
        let loader_context = LoaderContext::from_config(base_dir, &config);
        let runtime = PastaLuaRuntime::from_loader_with_scene_dic(
            context,
            loader_context,
            runtime_config,
            Some(config),
            logger,
            &scene_dic_path,
        )?;

        info!(path = %base_dir.display(), "Startup sequence completed");
        Ok(runtime)
    }

    /// Create an instance-specific logger, register it with the global registry,
    /// and update the tracing filter with config from pasta.toml.
    ///
    /// Called at Stage 1.5 (after Phase 1 config load).
    fn create_and_register_logger(
        base_dir: &Path,
        config: &PastaConfig,
    ) -> Result<Option<std::sync::Arc<crate::logging::PastaLogger>>, LoaderError> {
        let logging_config = config.logging();

        match crate::logging::PastaLogger::new(base_dir, logging_config.as_ref()) {
            Ok(logger) => {
                let logger = std::sync::Arc::new(logger);
                info!(path = %logger.log_path().display(), "Created instance logger");

                // Register with global registry (overwrites Stage 1 default writer)
                crate::logging::GlobalLoggerRegistry::instance()
                    .register(base_dir.to_path_buf(), logger.clone());

                // Update tracing filter with config
                if let Some(ref cfg) = logging_config {
                    crate::logging::update_tracing_filter(cfg);
                }

                Ok(Some(logger))
            }
            Err(e) => {
                // Log warning but don't fail startup
                warn!(error = %e, "Failed to create instance logger, logging disabled");
                Ok(None)
            }
        }
    }

    /// Prepare profile directories.
    ///
    /// Note: Cache directory management (including version-based clearing)
    /// is now handled by CacheManager::prepare_cache_dir().
    fn prepare_directories(base_dir: &Path, config: &LoaderConfig) -> Result<(), LoaderError> {
        let dirs = [
            "profile/pasta/save",
            "profile/pasta/save/lua",
            "profile/pasta/cache",
            &config.transpiled_output_dir,
        ];

        for dir in &dirs {
            let path = base_dir.join(dir);
            if !path.exists() {
                fs::create_dir_all(&path).map_err(|e| LoaderError::io(&path, e))?;
                debug!(path = %path.display(), "Created directory");
            }
        }

        // Note: Cache clearing is now handled by CacheManager with version checking
        // We no longer unconditionally delete cache/lua directory

        Ok(())
    }

    /// Discover all files (.pasta and .lua) with conflict checking.
    ///
    /// Returns (pasta_files, lua_files) where lua_files has conflicts removed.
    /// Also checks for invalid filenames (init.lua, init.pasta).
    fn discover_all_files(
        base_dir: &Path,
        pasta_patterns: &[String],
    ) -> Result<(Vec<std::path::PathBuf>, Vec<std::path::PathBuf>), LoaderError> {
        // Discover .pasta files
        let pasta_files = discovery::discover_files(base_dir, pasta_patterns)?;

        // Generate .lua patterns from .pasta patterns
        let lua_patterns: Vec<String> = pasta_patterns
            .iter()
            .filter_map(|p| {
                if let Some(stem) = p.strip_suffix(".pasta") {
                    Some(format!("{}.lua", stem))
                } else {
                    warn!(pattern = %p, "Cannot convert pattern to .lua, skipping");
                    None
                }
            })
            .collect();

        // Discover .lua files
        let lua_files = if lua_patterns.is_empty() {
            Vec::new()
        } else {
            discovery::discover_files(base_dir, &lua_patterns).unwrap_or_else(|e| {
                warn!(error = %e, "Failed to discover .lua files, skipping");
                Vec::new()
            })
        };

        // Check for invalid filenames (init.lua, init.pasta)
        for file in pasta_files.iter().chain(lua_files.iter()) {
            if let Some(file_name) = file.file_name().and_then(|n| n.to_str())
                && (file_name == "init.lua" || file_name == "init.pasta")
            {
                return Err(LoaderError::invalid_file_name(file));
            }
        }

        // Build HashSet of .pasta module names for conflict detection
        let pasta_module_names: std::collections::HashSet<String> = pasta_files
            .iter()
            .map(|f| {
                // Use inline module name computation (same as CacheManager)
                let relative = f
                    .strip_prefix(base_dir)
                    .unwrap_or(f)
                    .to_string_lossy()
                    .to_string();
                let without_prefix = relative
                    .strip_prefix("dic")
                    .unwrap_or(&relative)
                    .trim_start_matches(['/', '\\'])
                    .to_string();
                let stem = std::path::Path::new(&without_prefix)
                    .with_extension("")
                    .to_string_lossy()
                    .to_string();
                stem.replace(['/', '\\'], ".").replace('-', "_")
            })
            .collect();

        // Filter out conflicting .lua files
        let mut filtered_lua_files = Vec::new();
        for lua_file in &lua_files {
            let relative = lua_file
                .strip_prefix(base_dir)
                .unwrap_or(lua_file)
                .to_string_lossy()
                .to_string();
            let without_prefix = relative
                .strip_prefix("dic")
                .unwrap_or(&relative)
                .trim_start_matches(['/', '\\'])
                .to_string();
            let stem = std::path::Path::new(&without_prefix)
                .with_extension("")
                .to_string_lossy()
                .to_string();
            let module_key = stem.replace(['/', '\\'], ".").replace('-', "_");

            if pasta_module_names.contains(&module_key) {
                warn!(
                    lua_file = %lua_file.display(),
                    module_name = %format!("pasta.scene.{}", module_key),
                    "Module name conflict: .pasta file takes priority, .lua file ignored"
                );
            } else {
                filtered_lua_files.push(lua_file.clone());
            }
        }

        Ok((pasta_files, filtered_lua_files))
    }

    /// Incremental processing - transpile .pasta files and copy .lua files.
    ///
    /// Uses CacheManager to check timestamps and skip unchanged files.
    fn process_incremental(
        _base_dir: &Path,
        pasta_files: &[std::path::PathBuf],
        lua_files: &[std::path::PathBuf],
        cache_manager: &CacheManager,
    ) -> Result<(TranspileContext, Vec<String>, ProcessStats), LoaderError> {
        let transpiler = LuaTranspiler::default();
        let mut combined_context = TranspileContext::new();
        let total_count = pasta_files.len() + lua_files.len();
        let mut module_names = Vec::with_capacity(total_count);
        let mut failures = Vec::new();
        let mut stats = ProcessStats {
            transpiled: 0,
            skipped: 0,
            failed: 0,
            copied: 0,
        };

        // Process .pasta files (transpile)
        for file_path in pasta_files {
            // Check if transpilation is needed
            let needs_transpile = cache_manager.needs_transpile(file_path).unwrap_or(true);

            // Always collect module name for scene_dic.lua
            let module_name = cache_manager.source_to_module_name(file_path);
            module_names.push(module_name.clone());

            if !needs_transpile {
                stats.skipped += 1;
                debug!(file = %file_path.display(), "Skipped (cache up-to-date)");
                continue;
            }

            // Read and parse
            let content = match fs::read_to_string(file_path) {
                Ok(c) => c,
                Err(e) => {
                    failures.push(TranspileFailure {
                        source_path: file_path.clone(),
                        error: format!("Read error: {}", e),
                    });
                    stats.failed += 1;
                    continue;
                }
            };

            let filename = file_path.to_string_lossy().to_string();
            let pasta_file = match pasta_dsl::parse_str(&content, &filename) {
                Ok(pf) => pf,
                Err(e) => {
                    failures.push(TranspileFailure {
                        source_path: file_path.clone(),
                        error: format!("Parse error: {}", e),
                    });
                    stats.failed += 1;
                    continue;
                }
            };

            // Transpile
            let mut output = Vec::new();
            let file_context = match transpiler.transpile(&pasta_file, &mut output) {
                Ok(ctx) => ctx,
                Err(e) => {
                    failures.push(TranspileFailure {
                        source_path: file_path.clone(),
                        error: format!("Transpile error: {}", e),
                    });
                    stats.failed += 1;
                    continue;
                }
            };

            // Merge registries
            combined_context.merge_from(file_context);

            let lua_code = match String::from_utf8(output) {
                Ok(s) => s,
                Err(e) => {
                    failures.push(TranspileFailure {
                        source_path: file_path.clone(),
                        error: format!("UTF-8 error: {}", e),
                    });
                    stats.failed += 1;
                    continue;
                }
            };

            // Save to cache
            if let Err(e) = cache_manager.save_cache(file_path, &lua_code) {
                warn!(file = %file_path.display(), error = %e, "Failed to save cache");
                // Continue anyway - cache write failure is not fatal
            }

            stats.transpiled += 1;
            debug!(file = %file_path.display(), module = %module_name, "Transpiled");
        }

        // Process .lua files (copy passthrough)
        for file_path in lua_files {
            let needs_copy = cache_manager.needs_transpile(file_path).unwrap_or(true);

            // Always collect module name for scene_dic.lua
            let module_name = cache_manager.source_to_module_name(file_path);
            module_names.push(module_name.clone());

            if !needs_copy {
                stats.skipped += 1;
                debug!(file = %file_path.display(), "Skipped .lua (cache up-to-date)");
                continue;
            }

            // Read .lua file content directly (no parse/transpile)
            let content = match fs::read_to_string(file_path) {
                Ok(c) => c,
                Err(e) => {
                    error!(
                        file = %file_path.display(),
                        error = %e,
                        "Failed to read .lua file"
                    );
                    failures.push(TranspileFailure {
                        source_path: file_path.clone(),
                        error: format!("Read error: {}", e),
                    });
                    stats.failed += 1;
                    continue;
                }
            };

            // Save to cache (direct copy)
            if let Err(e) = cache_manager.save_cache(file_path, &content) {
                error!(
                    file = %file_path.display(),
                    error = %e,
                    "Failed to copy .lua file to cache"
                );
                failures.push(TranspileFailure {
                    source_path: file_path.clone(),
                    error: format!("Cache write error: {}", e),
                });
                stats.failed += 1;
                continue;
            }

            stats.copied += 1;
            debug!(file = %file_path.display(), module = %module_name, "Copied .lua");
        }

        // Abort on any failures
        if !failures.is_empty() {
            for failure in &failures {
                error!(
                    path = %failure.source_path.display(),
                    error = %failure.error,
                    "Process failure"
                );
            }
            let succeeded = stats.transpiled + stats.skipped + stats.copied;
            return Err(LoaderError::partial_transpile(
                succeeded,
                stats.failed,
                failures,
            ));
        }

        Ok((combined_context, module_names, stats))
    }

}

/// Result of transpiling a single pasta file.
#[derive(Debug, Clone)]
pub struct TranspileResult {
    /// Module name derived from source path
    pub module_name: String,
    /// Generated Lua code
    pub lua_code: String,
    /// Original source file path
    pub source_path: std::path::PathBuf,
}