solidb 1.0.1

A lightweight, high-performance structured database server written in Rust.
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
//! Test runner for Lua API tests
//!
//! Discovers and executes test files from the scripts/tests/ directory,
//! testing actual API endpoints against the running server.

use colored::Colorize;
use mlua::{Function, Lua, MultiValue, Result as LuaResult, Table, Value};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use walkdir::WalkDir;

use super::config::Config;
use super::test_http::TestHttpClient;

/// Test result status
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TestStatus {
    Passed,
    Failed(String),
    Skipped,
}

/// A single test case result
#[derive(Debug, Clone)]
pub struct TestResult {
    pub name: String,
    pub describe: String,
    pub status: TestStatus,
    pub duration: Duration,
}

/// Test runner configuration
#[derive(Default)]
pub struct TestRunnerConfig {
    pub verbose: bool,
    pub filter: Option<String>,
    pub file: Option<PathBuf>,
}

/// Summary of test execution
pub struct TestSummary {
    pub passed: usize,
    pub failed: usize,
    pub skipped: usize,
    pub total_duration: Duration,
    pub results: Vec<TestResult>,
}

impl TestSummary {
    pub fn new() -> Self {
        Self {
            passed: 0,
            failed: 0,
            skipped: 0,
            total_duration: Duration::ZERO,
            results: Vec::new(),
        }
    }

    pub fn add(&mut self, result: TestResult) {
        match &result.status {
            TestStatus::Passed => self.passed += 1,
            TestStatus::Failed(_) => self.failed += 1,
            TestStatus::Skipped => self.skipped += 1,
        }
        self.total_duration += result.duration;
        self.results.push(result);
    }
}

impl Default for TestSummary {
    fn default() -> Self {
        Self::new()
    }
}

/// The main test runner
pub struct TestRunner {
    config: Config,
    runner_config: TestRunnerConfig,
    scripts_dir: PathBuf,
}

impl TestRunner {
    /// Create a new test runner
    pub fn new(config: Config, scripts_dir: PathBuf, runner_config: TestRunnerConfig) -> Self {
        Self {
            config,
            runner_config,
            scripts_dir,
        }
    }

    /// Discover test files in the tests/ subdirectory
    pub fn discover_tests(&self) -> Vec<PathBuf> {
        let tests_dir = self.scripts_dir.join("tests");

        if !tests_dir.exists() {
            return Vec::new();
        }

        // If a specific file was requested, only return that
        if let Some(ref file) = self.runner_config.file {
            let file_path = if file.is_absolute() {
                file.clone()
            } else {
                tests_dir.join(file)
            };

            if file_path.exists() {
                return vec![file_path];
            } else {
                return Vec::new();
            }
        }

        let mut files = Vec::new();

        for entry in WalkDir::new(&tests_dir)
            .follow_links(true)
            .into_iter()
            .filter_map(|e| e.ok())
        {
            let path = entry.path();

            // Only include .lua files
            if path.is_file() && path.extension().map(|e| e == "lua").unwrap_or(false) {
                files.push(path.to_path_buf());
            }
        }

        files.sort();
        files
    }

    /// Run all discovered tests
    pub fn run(&self) -> anyhow::Result<TestSummary> {
        let test_files = self.discover_tests();

        if test_files.is_empty() {
            println!(
                "{} No test files found in {}",
                "!".yellow(),
                self.scripts_dir.join("tests").display()
            );
            return Ok(TestSummary::new());
        }

        println!("Running tests from {}...\n", self.scripts_dir.display());

        let mut summary = TestSummary::new();

        for file in test_files {
            self.run_test_file(&file, &mut summary)?;
        }

        self.print_summary(&summary);
        Ok(summary)
    }

    /// Run a single test file
    fn run_test_file(&self, path: &Path, summary: &mut TestSummary) -> anyhow::Result<()> {
        let code = std::fs::read_to_string(path)?;

        // Create Lua state with test framework
        let lua = Lua::new();
        self.setup_lua(&lua)?;

        // Create HTTP client
        let http_client = TestHttpClient::new(
            &self.config.base_url(),
            &self.config.database,
            &self.config.default_service(),
            self.config.auth_token.clone(),
        );

        // Register HTTP module
        super::test_http::register_http_module(&lua, http_client)?;

        // Collect test results
        let results: Arc<Mutex<Vec<TestResult>>> = Arc::new(Mutex::new(Vec::new()));
        let current_describe: Arc<Mutex<String>> = Arc::new(Mutex::new(String::new()));
        let verbose = self.runner_config.verbose;
        let filter = self.runner_config.filter.clone();

        // Register describe/it functions
        self.register_test_functions(
            &lua,
            results.clone(),
            current_describe.clone(),
            verbose,
            filter,
        )?;

        // Execute the test file
        if let Err(e) = lua.load(&code).exec() {
            eprintln!(
                "{} Error in {}: {}",
                "✗".red(),
                path.file_name().unwrap_or_default().to_string_lossy(),
                e
            );
            return Ok(());
        }

        // Add results to summary
        let results_guard = results
            .lock()
            .map_err(|e| anyhow::anyhow!("Lock error: {}", e))?;
        for result in results_guard.iter() {
            summary.add(result.clone());
        }

        Ok(())
    }

    /// Setup Lua environment with test utilities
    fn setup_lua(&self, lua: &Lua) -> LuaResult<()> {
        let globals = lua.globals();

        // Remove unsafe globals
        globals.set("os", Value::Nil)?;
        globals.set("io", Value::Nil)?;
        globals.set("debug", Value::Nil)?;
        globals.set("loadfile", Value::Nil)?;
        globals.set("dofile", Value::Nil)?;

        // Add JSON module
        self.register_json_module(lua)?;

        // Add print function (stores output for verbose mode)
        let print_fn = lua.create_function(|_, args: MultiValue| {
            let parts: Vec<String> = args
                .iter()
                .map(|v| match v {
                    Value::String(s) => s
                        .to_str()
                        .map(|s| s.to_string())
                        .unwrap_or_else(|_| "[invalid string]".to_string()),
                    Value::Number(n) => n.to_string(),
                    Value::Integer(i) => i.to_string(),
                    Value::Boolean(b) => b.to_string(),
                    Value::Nil => "nil".to_string(),
                    Value::Table(_) => "[table]".to_string(),
                    _ => format!("{:?}", v),
                })
                .collect();
            println!("    {}", parts.join("\t").dimmed());
            Ok(())
        })?;
        globals.set("print", print_fn)?;

        Ok(())
    }

    /// Register JSON encode/decode functions
    fn register_json_module(&self, lua: &Lua) -> LuaResult<()> {
        let globals = lua.globals();
        let json = lua.create_table()?;

        // json.encode
        let encode = lua.create_function(|lua, value: Value| {
            let json_value = lua_to_json(lua, &value)?;
            serde_json::to_string(&json_value).map_err(|e| mlua::Error::RuntimeError(e.to_string()))
        })?;
        json.set("encode", encode)?;

        // json.decode
        let decode = lua.create_function(|lua, s: String| {
            let json_value: serde_json::Value =
                serde_json::from_str(&s).map_err(|e| mlua::Error::RuntimeError(e.to_string()))?;
            json_to_lua(lua, &json_value)
        })?;
        json.set("decode", decode)?;

        globals.set("json", json)?;
        Ok(())
    }

    /// Register describe/it/expect functions
    fn register_test_functions(
        &self,
        lua: &Lua,
        results: Arc<Mutex<Vec<TestResult>>>,
        current_describe: Arc<Mutex<String>>,
        _verbose: bool,
        filter: Option<String>,
    ) -> LuaResult<()> {
        let globals = lua.globals();

        // describe(name, fn)
        let describe_current = Arc::clone(&current_describe);
        let describe_filter = filter.clone();
        let describe_fn = lua.create_function(move |lua, (name, func): (String, Function)| {
            // Check filter
            if let Some(ref f) = describe_filter {
                if !name.to_lowercase().contains(&f.to_lowercase()) {
                    return Ok(());
                }
            }

            println!("{}", name);
            if let Ok(mut current) = describe_current.lock() {
                *current = name;
            }

            // Create hooks storage
            let hooks: Table = lua.create_table()?;
            hooks.set("before", Value::Nil)?;
            hooks.set("after", Value::Nil)?;
            hooks.set("before_all", Value::Nil)?;
            hooks.set("after_all", Value::Nil)?;
            lua.globals().set("__test_hooks", hooks)?;

            // Run the describe block
            func.call::<()>(())?;

            // Clear hooks
            lua.globals().set("__test_hooks", Value::Nil)?;

            println!();
            Ok(())
        })?;
        globals.set("describe", describe_fn)?;

        // it(name, fn)
        let it_results = Arc::clone(&results);
        let it_current = Arc::clone(&current_describe);
        let it_fn = lua.create_function(move |lua, (name, func): (String, Function)| {
            let describe_name = it_current
                .lock()
                .map(|guard| guard.clone())
                .unwrap_or_default();
            let start = Instant::now();

            // Run before hook
            if let Ok(hooks) = lua.globals().get::<Table>("__test_hooks") {
                if let Ok(before) = hooks.get::<Function>("before") {
                    let _ = before.call::<()>(());
                }
            }

            // Run the test
            let result = func.call::<()>(());
            let duration = start.elapsed();

            // Run after hook
            if let Ok(hooks) = lua.globals().get::<Table>("__test_hooks") {
                if let Ok(after) = hooks.get::<Function>("after") {
                    let _ = after.call::<()>(());
                }
            }

            let status = match result {
                Ok(_) => {
                    println!(
                        "  {} {} {}",
                        "✓".green(),
                        name,
                        format!("({}ms)", duration.as_millis()).dimmed()
                    );
                    TestStatus::Passed
                }
                Err(e) => {
                    let msg = e.to_string();
                    println!(
                        "  {} {} {}",
                        "✗".red(),
                        name,
                        format!("({}ms)", duration.as_millis()).dimmed()
                    );
                    println!("    {}", msg.red());
                    TestStatus::Failed(msg)
                }
            };

            if let Ok(mut results) = it_results.lock() {
                results.push(TestResult {
                    name,
                    describe: describe_name,
                    status,
                    duration,
                });
            }

            Ok(())
        })?;
        globals.set("it", it_fn)?;

        // before(fn) - run before each test
        let before_fn = lua.create_function(|lua, func: Function| {
            if let Ok(hooks) = lua.globals().get::<Table>("__test_hooks") {
                hooks.set("before", func)?;
            }
            Ok(())
        })?;
        globals.set("before", before_fn)?;

        // after(fn) - run after each test
        let after_fn = lua.create_function(|lua, func: Function| {
            if let Ok(hooks) = lua.globals().get::<Table>("__test_hooks") {
                hooks.set("after", func)?;
            }
            Ok(())
        })?;
        globals.set("after", after_fn)?;

        // before_all(fn) - run once before all tests
        let before_all_fn = lua.create_function(|_lua, func: Function| {
            // Execute immediately
            func.call::<()>(())?;
            Ok(())
        })?;
        globals.set("before_all", before_all_fn)?;

        // after_all(fn) - run once after all tests
        let after_all_fn = lua.create_function(|lua, func: Function| {
            if let Ok(hooks) = lua.globals().get::<Table>("__test_hooks") {
                hooks.set("after_all", func)?;
            }
            Ok(())
        })?;
        globals.set("after_all", after_all_fn)?;

        // expect(value) - returns expectation object
        self.register_expect(lua)?;

        Ok(())
    }

    /// Register the expect function and matchers
    fn register_expect(&self, lua: &Lua) -> LuaResult<()> {
        let globals = lua.globals();

        let expect_fn = lua.create_function(|lua, value: Value| {
            let expectation = lua.create_table()?;
            expectation.set("__value", value.clone())?;

            // to_equal(expected)
            let to_equal = lua.create_function(|lua, (this, expected): (Table, Value)| {
                let actual: Value = this.get("__value")?;
                if !values_equal(&actual, &expected) {
                    let actual_str = value_to_string(lua, &actual)?;
                    let expected_str = value_to_string(lua, &expected)?;
                    return Err(mlua::Error::RuntimeError(format!(
                        "Expected {} to equal {}",
                        actual_str, expected_str
                    )));
                }
                Ok(())
            })?;
            expectation.set("to_equal", to_equal)?;

            // to_exist()
            let to_exist = lua.create_function(|_, this: Table| {
                let actual: Value = this.get("__value")?;
                if matches!(actual, Value::Nil) {
                    return Err(mlua::Error::RuntimeError(
                        "Expected value to exist, got nil".to_string(),
                    ));
                }
                Ok(())
            })?;
            expectation.set("to_exist", to_exist)?;

            // to_be_nil()
            let to_be_nil = lua.create_function(|lua, this: Table| {
                let actual: Value = this.get("__value")?;
                if !matches!(actual, Value::Nil) {
                    let actual_str = value_to_string(lua, &actual)?;
                    return Err(mlua::Error::RuntimeError(format!(
                        "Expected nil, got {}",
                        actual_str
                    )));
                }
                Ok(())
            })?;
            expectation.set("to_be_nil", to_be_nil)?;

            // to_contain(substring)
            let to_contain = lua.create_function(|_, (this, substring): (Table, String)| {
                let actual: Value = this.get("__value")?;
                match actual {
                    Value::String(s) => {
                        let s = s.to_str()?;
                        if !s.contains(&substring) {
                            return Err(mlua::Error::RuntimeError(format!(
                                "Expected '{}' to contain '{}'",
                                s, substring
                            )));
                        }
                    }
                    _ => {
                        return Err(mlua::Error::RuntimeError(
                            "to_contain can only be used with strings".to_string(),
                        ));
                    }
                }
                Ok(())
            })?;
            expectation.set("to_contain", to_contain)?;

            // to_match(pattern)
            let to_match = lua.create_function(|_, (this, pattern): (Table, String)| {
                let actual: Value = this.get("__value")?;
                match actual {
                    Value::String(s) => {
                        let s_str = s.to_str()?.to_string();
                        let re = regex::Regex::new(&pattern).map_err(|e| {
                            mlua::Error::RuntimeError(format!("Invalid regex: {}", e))
                        })?;
                        if !re.is_match(&s_str) {
                            return Err(mlua::Error::RuntimeError(format!(
                                "Expected '{}' to match pattern '{}'",
                                s_str, pattern
                            )));
                        }
                    }
                    _ => {
                        return Err(mlua::Error::RuntimeError(
                            "to_match can only be used with strings".to_string(),
                        ));
                    }
                }
                Ok(())
            })?;
            expectation.set("to_match", to_match)?;

            // to_be_greater_than(n)
            let to_be_greater_than = lua.create_function(|_, (this, n): (Table, f64)| {
                let actual: Value = this.get("__value")?;
                let actual_num = match actual {
                    Value::Number(num) => num,
                    Value::Integer(i) => i as f64,
                    _ => {
                        return Err(mlua::Error::RuntimeError(
                            "to_be_greater_than can only be used with numbers".to_string(),
                        ));
                    }
                };
                if actual_num <= n {
                    return Err(mlua::Error::RuntimeError(format!(
                        "Expected {} to be greater than {}",
                        actual_num, n
                    )));
                }
                Ok(())
            })?;
            expectation.set("to_be_greater_than", to_be_greater_than)?;

            // to_be_less_than(n)
            let to_be_less_than = lua.create_function(|_, (this, n): (Table, f64)| {
                let actual: Value = this.get("__value")?;
                let actual_num = match actual {
                    Value::Number(num) => num,
                    Value::Integer(i) => i as f64,
                    _ => {
                        return Err(mlua::Error::RuntimeError(
                            "to_be_less_than can only be used with numbers".to_string(),
                        ));
                    }
                };
                if actual_num >= n {
                    return Err(mlua::Error::RuntimeError(format!(
                        "Expected {} to be less than {}",
                        actual_num, n
                    )));
                }
                Ok(())
            })?;
            expectation.set("to_be_less_than", to_be_less_than)?;

            // to_throw()
            let to_throw = lua.create_function(|_, this: Table| {
                let func: Value = this.get("__value")?;
                match func {
                    Value::Function(f) => {
                        let result: LuaResult<()> = f.call(());
                        if result.is_ok() {
                            return Err(mlua::Error::RuntimeError(
                                "Expected function to throw, but it did not".to_string(),
                            ));
                        }
                    }
                    _ => {
                        return Err(mlua::Error::RuntimeError(
                            "to_throw can only be used with functions".to_string(),
                        ));
                    }
                }
                Ok(())
            })?;
            expectation.set("to_throw", to_throw)?;

            // to_be_true()
            let to_be_true = lua.create_function(|_, this: Table| {
                let actual: Value = this.get("__value")?;
                match actual {
                    Value::Boolean(true) => Ok(()),
                    Value::Boolean(false) => Err(mlua::Error::RuntimeError(
                        "Expected true, got false".to_string(),
                    )),
                    _ => Err(mlua::Error::RuntimeError(
                        "Expected true, got non-boolean".to_string(),
                    )),
                }
            })?;
            expectation.set("to_be_true", to_be_true)?;

            // to_be_false()
            let to_be_false = lua.create_function(|_, this: Table| {
                let actual: Value = this.get("__value")?;
                match actual {
                    Value::Boolean(false) => Ok(()),
                    Value::Boolean(true) => Err(mlua::Error::RuntimeError(
                        "Expected false, got true".to_string(),
                    )),
                    _ => Err(mlua::Error::RuntimeError(
                        "Expected false, got non-boolean".to_string(),
                    )),
                }
            })?;
            expectation.set("to_be_false", to_be_false)?;

            Ok(expectation)
        })?;
        globals.set("expect", expect_fn)?;

        Ok(())
    }

    /// Print the test summary
    fn print_summary(&self, summary: &TestSummary) {
        println!("{}", "─".repeat(50));

        let status_line = if summary.failed > 0 {
            format!(
                "Tests: {} passed, {} failed",
                summary.passed.to_string().green(),
                summary.failed.to_string().red()
            )
        } else {
            format!("Tests: {} passed", summary.passed.to_string().green())
        };

        if summary.skipped > 0 {
            println!("{}, {} skipped", status_line, summary.skipped);
        } else {
            println!("{}", status_line);
        }

        println!("Time:  {}ms", summary.total_duration.as_millis());
    }
}

/// Check if two Lua values are equal
fn values_equal(a: &Value, b: &Value) -> bool {
    match (a, b) {
        (Value::Nil, Value::Nil) => true,
        (Value::Boolean(a), Value::Boolean(b)) => a == b,
        (Value::Integer(a), Value::Integer(b)) => a == b,
        (Value::Number(a), Value::Number(b)) => (a - b).abs() < f64::EPSILON,
        (Value::Integer(a), Value::Number(b)) | (Value::Number(b), Value::Integer(a)) => {
            ((*a as f64) - b).abs() < f64::EPSILON
        }
        (Value::String(a), Value::String(b)) => a.to_str().ok() == b.to_str().ok(),
        (Value::Table(a), Value::Table(b)) => {
            // Simple table comparison - check all keys
            let a_pairs: Vec<_> = a.pairs::<Value, Value>().filter_map(|r| r.ok()).collect();
            let b_pairs: Vec<_> = b.pairs::<Value, Value>().filter_map(|r| r.ok()).collect();
            if a_pairs.len() != b_pairs.len() {
                return false;
            }
            for (key, val) in a_pairs {
                match b.get::<Value>(key.clone()) {
                    Ok(b_val) => {
                        if !values_equal(&val, &b_val) {
                            return false;
                        }
                    }
                    Err(_) => return false,
                }
            }
            true
        }
        _ => false,
    }
}

/// Convert Lua value to string for display
fn value_to_string(lua: &Lua, value: &Value) -> LuaResult<String> {
    match value {
        Value::Nil => Ok("nil".to_string()),
        Value::Boolean(b) => Ok(b.to_string()),
        Value::Integer(i) => Ok(i.to_string()),
        Value::Number(n) => Ok(n.to_string()),
        Value::String(s) => Ok(format!("\"{}\"", s.to_str()?)),
        Value::Table(_) => {
            let json = lua_to_json(lua, value)?;
            Ok(serde_json::to_string(&json).unwrap_or_else(|_| "[table]".to_string()))
        }
        Value::Function(_) => Ok("[function]".to_string()),
        _ => Ok(format!("{:?}", value)),
    }
}

/// Convert Lua value to JSON
#[allow(clippy::only_used_in_recursion)]
fn lua_to_json(lua: &Lua, value: &Value) -> LuaResult<serde_json::Value> {
    match value {
        Value::Nil => Ok(serde_json::Value::Null),
        Value::Boolean(b) => Ok(serde_json::Value::Bool(*b)),
        Value::Integer(i) => Ok(serde_json::Value::Number((*i).into())),
        Value::Number(n) => serde_json::Number::from_f64(*n)
            .map(serde_json::Value::Number)
            .ok_or_else(|| mlua::Error::RuntimeError("Invalid number".to_string())),
        Value::String(s) => Ok(serde_json::Value::String(s.to_str()?.to_string())),
        Value::Table(t) => {
            // Check if it's an array (sequential integer keys starting from 1)
            let len = t.len()?;
            if len > 0 {
                let mut is_array = true;
                for i in 1..=len {
                    if t.get::<Value>(i).is_err() {
                        is_array = false;
                        break;
                    }
                }
                if is_array {
                    let mut arr = Vec::new();
                    for i in 1..=len {
                        let val: Value = t.get(i)?;
                        arr.push(lua_to_json(lua, &val)?);
                    }
                    return Ok(serde_json::Value::Array(arr));
                }
            }

            // It's an object
            let mut map = serde_json::Map::new();
            for pair in t.pairs::<Value, Value>() {
                let (k, v) = pair?;
                let key = match k {
                    Value::String(s) => s.to_str()?.to_string(),
                    Value::Integer(i) => i.to_string(),
                    _ => continue,
                };
                map.insert(key, lua_to_json(lua, &v)?);
            }
            Ok(serde_json::Value::Object(map))
        }
        _ => Ok(serde_json::Value::Null),
    }
}

/// Convert JSON to Lua value
fn json_to_lua(lua: &Lua, value: &serde_json::Value) -> LuaResult<Value> {
    match value {
        serde_json::Value::Null => Ok(Value::Nil),
        serde_json::Value::Bool(b) => Ok(Value::Boolean(*b)),
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                Ok(Value::Integer(i))
            } else if let Some(f) = n.as_f64() {
                Ok(Value::Number(f))
            } else {
                Ok(Value::Nil)
            }
        }
        serde_json::Value::String(s) => Ok(Value::String(lua.create_string(s)?)),
        serde_json::Value::Array(arr) => {
            let table = lua.create_table()?;
            for (i, v) in arr.iter().enumerate() {
                table.set(i + 1, json_to_lua(lua, v)?)?;
            }
            Ok(Value::Table(table))
        }
        serde_json::Value::Object(obj) => {
            let table = lua.create_table()?;
            for (k, v) in obj {
                table.set(k.as_str(), json_to_lua(lua, v)?)?;
            }
            Ok(Value::Table(table))
        }
    }
}