depyler-core 3.22.0

Core transpilation engine for the Depyler Python-to-Rust transpiler
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
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
//! JSON Module Code Generation - EXTREME TDD
//!
//! Handles Python `json` module method conversions to Rust serde_json.
//! Extracted from expr_gen.rs for testability and maintainability.
//!
//! Coverage target: 100% line coverage, 100% branch coverage

use crate::hir::{HirExpr, Type};
use crate::rust_gen::context::{CodeGenContext, ToRustExpr};
use anyhow::{bail, Result};
use syn::parse_quote;

/// Convert Python json module method calls to Rust serde_json
///
/// # Supported Methods
/// - `json.dumps(obj)` → `serde_json::to_string(&obj).unwrap()`
/// - `json.dumps(obj, indent=n)` → `serde_json::to_string_pretty(&obj).unwrap()`
/// - `json.loads(s)` → `serde_json::from_str::<Value>(&s).unwrap()`
/// - `json.dump(obj, file)` → `serde_json::to_writer(file, &obj).unwrap()`
/// - `json.load(file)` → `serde_json::from_reader::<_, Value>(file).unwrap()`
///
/// DEPYLER-1022: NASA mode support - returns stub implementations that don't
/// require serde_json external crate.
///
/// # Complexity: 5 (match with 5 branches)
pub fn convert_json_method(
    method: &str,
    args: &[HirExpr],
    ctx: &mut CodeGenContext,
) -> Result<Option<syn::Expr>> {
    // Convert arguments first
    let arg_exprs: Vec<syn::Expr> = args
        .iter()
        .map(|arg| arg.to_rust_expr(ctx))
        .collect::<Result<Vec<_>>>()?;

    // DEPYLER-1022: In NASA mode, use stub implementations
    if ctx.type_mapper.nasa_mode {
        let result = match method {
            "dumps" => convert_dumps_nasa(&arg_exprs)?,
            "loads" => convert_loads_nasa(&arg_exprs, ctx)?,
            "dump" => convert_dump_nasa(&arg_exprs)?,
            "load" => convert_load_nasa(&arg_exprs, ctx)?,
            _ => bail!("json.{} not implemented yet", method),
        };
        return Ok(Some(result));
    }

    // Mark that we need serde_json crate (non-NASA mode)
    ctx.needs_serde_json = true;

    let result = match method {
        "dumps" => convert_dumps(&arg_exprs)?,
        "loads" => convert_loads(&arg_exprs, ctx)?,
        "dump" => convert_dump(&arg_exprs)?,
        "load" => convert_load(&arg_exprs, ctx)?,
        _ => bail!("json.{} not implemented yet", method),
    };

    Ok(Some(result))
}

/// Convert json.dumps() call
fn convert_dumps(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.is_empty() || arg_exprs.len() > 2 {
        bail!("json.dumps() requires 1 or 2 arguments");
    }
    let obj = &arg_exprs[0];

    // DEPYLER-0377: Check if indent parameter is provided
    if arg_exprs.len() >= 2 {
        // json.dumps(obj, indent=n) → serde_json::to_string_pretty(&obj).unwrap()
        Ok(parse_quote! { serde_json::to_string_pretty(&#obj).unwrap() })
    } else {
        // json.dumps(obj) → serde_json::to_string(&obj).unwrap()
        Ok(parse_quote! { serde_json::to_string(&#obj).unwrap() })
    }
}

/// Convert json.loads() call
fn convert_loads(arg_exprs: &[syn::Expr], ctx: &mut CodeGenContext) -> Result<syn::Expr> {
    if arg_exprs.len() != 1 {
        bail!("json.loads() requires exactly 1 argument");
    }
    let s = &arg_exprs[0];

    // DEPYLER-0962: Check if return type is a Union of dict|list
    if let Some(union_name) = return_type_is_dict_list_union(ctx) {
        ctx.needs_hashmap = true;
        let union_ident: syn::Ident =
            syn::Ident::new(&union_name, proc_macro2::Span::call_site());
        Ok(parse_quote! {
            {
                let __json_val = serde_json::from_str::<serde_json::Value>(&#s).unwrap();
                match __json_val {
                    serde_json::Value::Object(obj) => #union_ident::Dict(obj.into_iter().collect()),
                    serde_json::Value::Array(arr) => #union_ident::List(arr),
                    _ => panic!("json.loads expected dict or list"),
                }
            }
        })
    } else if return_type_needs_json_dict(ctx) {
        // DEPYLER-0703: Check if return type is Dict[str, Any] → HashMap<String, Value>
        ctx.needs_hashmap = true;
        Ok(parse_quote! { serde_json::from_str::<std::collections::HashMap<String, serde_json::Value>>(&#s).unwrap() })
    } else {
        // json.loads(s) → serde_json::from_str::<Value>(&s).unwrap()
        Ok(parse_quote! { serde_json::from_str::<serde_json::Value>(&#s).unwrap() })
    }
}

/// Convert json.dump() call
fn convert_dump(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.len() != 2 {
        bail!("json.dump() requires exactly 2 arguments (obj, file)");
    }
    let obj = &arg_exprs[0];
    let file = &arg_exprs[1];
    Ok(parse_quote! { serde_json::to_writer(#file, &#obj).unwrap() })
}

/// Convert json.load() call
fn convert_load(arg_exprs: &[syn::Expr], ctx: &mut CodeGenContext) -> Result<syn::Expr> {
    if arg_exprs.len() != 1 {
        bail!("json.load() requires exactly 1 argument (file)");
    }
    let file = &arg_exprs[0];

    // DEPYLER-0962: Check if return type is a Union of dict|list
    if let Some(union_name) = return_type_is_dict_list_union(ctx) {
        ctx.needs_hashmap = true;
        let union_ident: syn::Ident =
            syn::Ident::new(&union_name, proc_macro2::Span::call_site());
        Ok(parse_quote! {
            {
                let __json_val = serde_json::from_reader::<_, serde_json::Value>(#file).unwrap();
                match __json_val {
                    serde_json::Value::Object(obj) => #union_ident::Dict(obj.into_iter().collect()),
                    serde_json::Value::Array(arr) => #union_ident::List(arr),
                    _ => panic!("json.load expected dict or list"),
                }
            }
        })
    } else {
        Ok(parse_quote! { serde_json::from_reader::<_, serde_json::Value>(#file).unwrap() })
    }
}

/// DEPYLER-0962: Check if return type is a Union of dict and list
pub fn return_type_is_dict_list_union(ctx: &CodeGenContext) -> Option<String> {
    if let Some(Type::Union(types)) = ctx.current_return_type.as_ref() {
        let has_dict = types.iter().any(|t| matches!(t, Type::Dict(_, _)));
        let has_list = types.iter().any(|t| matches!(t, Type::List(_)));
        if has_dict && has_list && types.len() == 2 {
            return Some("DictOrListUnion".to_string());
        }
    }
    None
}

/// DEPYLER-0560: Check if function return type requires serde_json::Value
pub fn return_type_needs_json_dict(ctx: &CodeGenContext) -> bool {
    // Check assignment target type first
    if let Some(ref assign_type) = ctx.current_assign_type {
        match assign_type {
            Type::Dict(_, value_type) => {
                if is_json_value_type(value_type.as_ref()) {
                    return true;
                }
            }
            Type::Custom(s) if s.contains("HashMap") && s.contains("Value") => return true,
            // DEPYLER-1004: Handle bare Dict from typing import (becomes Type::Custom("Dict"))
            Type::Custom(s) if s == "Dict" => return true,
            _ => {}
        }
    }

    // Check function return type
    if let Some(ref ret_type) = ctx.current_return_type {
        match ret_type {
            Type::Dict(_, value_type) => is_json_value_type(value_type.as_ref()),
            Type::Custom(s) if s.contains("HashMap") && s.contains("Value") => true,
            // DEPYLER-1004: Handle bare Dict from typing import (becomes Type::Custom("Dict"))
            Type::Custom(s) if s == "Dict" => true,
            _ => false,
        }
    } else {
        false
    }
}

/// Check if a type represents serde_json::Value
fn is_json_value_type(t: &Type) -> bool {
    match t {
        Type::Unknown => true,
        Type::Custom(s) => s.contains("Value") || s.contains("Any"),
        _ => false,
    }
}

/// DEPYLER-1153: Check if return type expects DepylerValue keys (bare Dict)
/// This happens when Python has `-> Dict` without type parameters
fn return_type_needs_depyler_value_keys(ctx: &CodeGenContext) -> bool {
    if let Some(ref ret_type) = ctx.current_return_type {
        match ret_type {
            // Bare Dict maps to HashMap<DepylerValue, DepylerValue>
            Type::Custom(s) if s == "Dict" => true,
            // Dict with unknown key type also uses DepylerValue keys
            Type::Dict(key_type, _) => matches!(key_type.as_ref(), Type::Unknown),
            _ => false,
        }
    } else {
        // No return type specified - default to DepylerValue keys for safety
        true
    }
}

// ============ NASA Mode Stub Functions ============
// DEPYLER-1022: These functions provide std-only implementations that compile
// without external crates. They use format!("{:?}", ...) for serialization
// and return empty containers for deserialization.

/// NASA mode: json.dumps() → format!("{:?}", obj)
fn convert_dumps_nasa(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.is_empty() || arg_exprs.len() > 2 {
        bail!("json.dumps() requires 1 or 2 arguments");
    }
    let obj = &arg_exprs[0];
    // Use Debug formatting as a simple serialization
    Ok(parse_quote! { format!("{:?}", #obj) })
}

/// NASA mode: json.loads() → empty HashMap stub
/// DEPYLER-1051: Uses DepylerValue for Hybrid Fallback Strategy
/// DEPYLER-1153: Use DepylerValue keys when return type is bare Dict
fn convert_loads_nasa(arg_exprs: &[syn::Expr], ctx: &mut CodeGenContext) -> Result<syn::Expr> {
    if arg_exprs.len() != 1 {
        bail!("json.loads() requires exactly 1 argument");
    }
    let _s = &arg_exprs[0];
    ctx.needs_hashmap = true;
    ctx.needs_depyler_value_enum = true; // DEPYLER-1051: Need DepylerValue enum
    // Return empty HashMap as stub - actual parsing requires serde_json
    // DEPYLER-1153: Check if return type expects DepylerValue keys (bare Dict)
    if return_type_needs_depyler_value_keys(ctx) {
        Ok(parse_quote! { std::collections::HashMap::<DepylerValue, DepylerValue>::new() })
    } else {
        // DEPYLER-1051: Use String keys when return type is Dict[str, Any] or similar
        Ok(parse_quote! { std::collections::HashMap::<String, DepylerValue>::new() })
    }
}

/// NASA mode: json.dump() → write Debug format to file (stub)
fn convert_dump_nasa(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.len() != 2 {
        bail!("json.dump() requires exactly 2 arguments (obj, file)");
    }
    let obj = &arg_exprs[0];
    let _file = &arg_exprs[1];
    // Stub: just format the object (file writing requires more complex handling)
    Ok(parse_quote! { format!("{:?}", #obj) })
}

/// NASA mode: json.load() → empty HashMap stub
/// DEPYLER-1051: Uses DepylerValue for Hybrid Fallback Strategy
/// DEPYLER-1153: Use DepylerValue keys when return type is bare Dict
fn convert_load_nasa(arg_exprs: &[syn::Expr], ctx: &mut CodeGenContext) -> Result<syn::Expr> {
    if arg_exprs.len() != 1 {
        bail!("json.load() requires exactly 1 argument (file)");
    }
    let _file = &arg_exprs[0];
    ctx.needs_hashmap = true;
    ctx.needs_depyler_value_enum = true; // DEPYLER-1051: Need DepylerValue enum
    // Return empty HashMap as stub
    // DEPYLER-1153: Check if return type expects DepylerValue keys (bare Dict)
    if return_type_needs_depyler_value_keys(ctx) {
        Ok(parse_quote! { std::collections::HashMap::<DepylerValue, DepylerValue>::new() })
    } else {
        // DEPYLER-1051: Use String keys when return type is Dict[str, Any] or similar
        Ok(parse_quote! { std::collections::HashMap::<String, DepylerValue>::new() })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hir::Literal;

    /// Create context with NASA mode disabled for testing serde_json integration
    fn ctx_with_serde_json() -> CodeGenContext<'static> {
        let mut ctx = CodeGenContext::default();
        ctx.type_mapper = Box::leak(Box::new(ctx.type_mapper.clone().with_nasa_mode(false)));
        ctx
    }

    // ============ NASA Mode Tests (Default) ============

    #[test]
    fn test_nasa_mode_json_dumps_returns_format() {
        let mut ctx = CodeGenContext::default(); // Default is NASA mode
        let args = vec![HirExpr::Var("data".to_string())];

        let result = convert_json_method("dumps", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(result.unwrap().is_some());
        assert!(!ctx.needs_serde_json); // NASA mode doesn't need serde_json
    }

    #[test]
    fn test_nasa_mode_json_loads_returns_hashmap() {
        let mut ctx = CodeGenContext::default();
        let args = vec![HirExpr::Var("json_str".to_string())];

        let result = convert_json_method("loads", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(!ctx.needs_serde_json); // NASA mode doesn't need serde_json
        assert!(ctx.needs_hashmap); // But does need HashMap
    }

    // ============ Non-NASA Mode Tests (serde_json) ============

    #[test]
    fn test_convert_json_dumps_single_arg() {
        let mut ctx = ctx_with_serde_json();
        let args = vec![HirExpr::Var("data".to_string())];

        let result = convert_json_method("dumps", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(result.unwrap().is_some());
        assert!(ctx.needs_serde_json);
    }

    #[test]
    fn test_convert_json_dumps_with_indent() {
        let mut ctx = ctx_with_serde_json();
        let args = vec![
            HirExpr::Var("data".to_string()),
            HirExpr::Literal(Literal::Int(2)),
        ];

        let result = convert_json_method("dumps", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(ctx.needs_serde_json);
    }

    #[test]
    fn test_convert_json_dumps_no_args_error() {
        let mut ctx = CodeGenContext::default();
        let args: Vec<HirExpr> = vec![];

        let result = convert_json_method("dumps", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_json_dumps_too_many_args_error() {
        let mut ctx = CodeGenContext::default();
        let args = vec![
            HirExpr::Var("a".to_string()),
            HirExpr::Var("b".to_string()),
            HirExpr::Var("c".to_string()),
        ];

        let result = convert_json_method("dumps", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_json_loads_single_arg() {
        let mut ctx = ctx_with_serde_json();
        let args = vec![HirExpr::Var("json_str".to_string())];

        let result = convert_json_method("loads", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(ctx.needs_serde_json);
    }

    #[test]
    fn test_convert_json_loads_wrong_args_error() {
        let mut ctx = CodeGenContext::default();
        let args: Vec<HirExpr> = vec![];

        let result = convert_json_method("loads", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_json_loads_with_dict_list_union_return() {
        let mut ctx = ctx_with_serde_json();
        ctx.current_return_type = Some(Type::Union(vec![
            Type::Dict(Box::new(Type::String), Box::new(Type::Unknown)),
            Type::List(Box::new(Type::Unknown)),
        ]));
        let args = vec![HirExpr::Var("json_str".to_string())];

        let result = convert_json_method("loads", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(ctx.needs_hashmap);
    }

    #[test]
    fn test_convert_json_loads_with_dict_any_return() {
        let mut ctx = ctx_with_serde_json();
        ctx.current_return_type = Some(Type::Dict(
            Box::new(Type::String),
            Box::new(Type::Unknown),  // Unknown represents Any
        ));
        let args = vec![HirExpr::Var("json_str".to_string())];

        let result = convert_json_method("loads", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(ctx.needs_hashmap);
    }

    #[test]
    fn test_convert_json_dump_correct_args() {
        let mut ctx = ctx_with_serde_json(); // Non-NASA mode
        let args = vec![
            HirExpr::Var("data".to_string()),
            HirExpr::Var("file".to_string()),
        ];

        let result = convert_json_method("dump", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(ctx.needs_serde_json);
    }

    #[test]
    fn test_convert_json_dump_wrong_args_error() {
        let mut ctx = CodeGenContext::default();
        let args = vec![HirExpr::Var("data".to_string())];

        let result = convert_json_method("dump", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_json_load_single_arg() {
        let mut ctx = ctx_with_serde_json(); // Non-NASA mode
        let args = vec![HirExpr::Var("file".to_string())];

        let result = convert_json_method("load", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(ctx.needs_serde_json);
    }

    #[test]
    fn test_convert_json_load_wrong_args_error() {
        let mut ctx = CodeGenContext::default();
        let args: Vec<HirExpr> = vec![];

        let result = convert_json_method("load", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_json_load_with_dict_list_union_return() {
        let mut ctx = CodeGenContext::default();
        ctx.current_return_type = Some(Type::Union(vec![
            Type::Dict(Box::new(Type::String), Box::new(Type::Unknown)),
            Type::List(Box::new(Type::Unknown)),
        ]));
        let args = vec![HirExpr::Var("file".to_string())];

        let result = convert_json_method("load", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(ctx.needs_hashmap);
    }

    #[test]
    fn test_convert_json_unknown_method_error() {
        let mut ctx = CodeGenContext::default();
        let args = vec![HirExpr::Var("data".to_string())];

        let result = convert_json_method("unknown_method", &args, &mut ctx);
        assert!(result.is_err());
    }

    // ============ return_type_is_dict_list_union tests ============

    #[test]
    fn test_return_type_is_dict_list_union_true() {
        let mut ctx = CodeGenContext::default();
        ctx.current_return_type = Some(Type::Union(vec![
            Type::Dict(Box::new(Type::String), Box::new(Type::Int)),
            Type::List(Box::new(Type::Int)),
        ]));

        assert!(return_type_is_dict_list_union(&ctx).is_some());
    }

    #[test]
    fn test_return_type_is_dict_list_union_false_no_dict() {
        let mut ctx = CodeGenContext::default();
        ctx.current_return_type = Some(Type::Union(vec![
            Type::String,
            Type::List(Box::new(Type::Int)),
        ]));

        assert!(return_type_is_dict_list_union(&ctx).is_none());
    }

    #[test]
    fn test_return_type_is_dict_list_union_false_no_list() {
        let mut ctx = CodeGenContext::default();
        ctx.current_return_type = Some(Type::Union(vec![
            Type::Dict(Box::new(Type::String), Box::new(Type::Int)),
            Type::String,
        ]));

        assert!(return_type_is_dict_list_union(&ctx).is_none());
    }

    #[test]
    fn test_return_type_is_dict_list_union_false_too_many_types() {
        let mut ctx = CodeGenContext::default();
        ctx.current_return_type = Some(Type::Union(vec![
            Type::Dict(Box::new(Type::String), Box::new(Type::Int)),
            Type::List(Box::new(Type::Int)),
            Type::String,
        ]));

        assert!(return_type_is_dict_list_union(&ctx).is_none());
    }

    #[test]
    fn test_return_type_is_dict_list_union_false_not_union() {
        let mut ctx = CodeGenContext::default();
        ctx.current_return_type = Some(Type::String);

        assert!(return_type_is_dict_list_union(&ctx).is_none());
    }

    #[test]
    fn test_return_type_is_dict_list_union_false_no_return_type() {
        let ctx = CodeGenContext::default();
        assert!(return_type_is_dict_list_union(&ctx).is_none());
    }

    // ============ return_type_needs_json_dict tests ============

    #[test]
    fn test_return_type_needs_json_dict_true_any() {
        let mut ctx = CodeGenContext::default();
        ctx.current_return_type = Some(Type::Dict(
            Box::new(Type::String),
            Box::new(Type::Custom("Any".to_string())),  // Any as custom type
        ));

        assert!(return_type_needs_json_dict(&ctx));
    }

    #[test]
    fn test_return_type_needs_json_dict_true_unknown() {
        let mut ctx = CodeGenContext::default();
        ctx.current_return_type = Some(Type::Dict(
            Box::new(Type::String),
            Box::new(Type::Unknown),
        ));

        assert!(return_type_needs_json_dict(&ctx));
    }

    #[test]
    fn test_return_type_needs_json_dict_true_custom_hashmap_value() {
        let mut ctx = CodeGenContext::default();
        ctx.current_return_type = Some(Type::Custom("HashMap<String, Value>".to_string()));

        assert!(return_type_needs_json_dict(&ctx));
    }

    #[test]
    fn test_return_type_needs_json_dict_false_int_value() {
        let mut ctx = CodeGenContext::default();
        ctx.current_return_type = Some(Type::Dict(
            Box::new(Type::String),
            Box::new(Type::Int),
        ));

        assert!(!return_type_needs_json_dict(&ctx));
    }

    #[test]
    fn test_return_type_needs_json_dict_false_not_dict() {
        let mut ctx = CodeGenContext::default();
        ctx.current_return_type = Some(Type::String);

        assert!(!return_type_needs_json_dict(&ctx));
    }

    #[test]
    fn test_return_type_needs_json_dict_false_no_return_type() {
        let ctx = CodeGenContext::default();
        assert!(!return_type_needs_json_dict(&ctx));
    }

    #[test]
    fn test_return_type_needs_json_dict_assign_type_any() {
        let mut ctx = CodeGenContext::default();
        ctx.current_assign_type = Some(Type::Dict(
            Box::new(Type::String),
            Box::new(Type::Unknown),  // Unknown represents Any
        ));

        assert!(return_type_needs_json_dict(&ctx));
    }

    #[test]
    fn test_return_type_needs_json_dict_assign_type_custom() {
        let mut ctx = CodeGenContext::default();
        ctx.current_assign_type = Some(Type::Custom("HashMap<String, Value>".to_string()));

        assert!(return_type_needs_json_dict(&ctx));
    }

    // ============ is_json_value_type tests ============

    #[test]
    fn test_is_json_value_type_unknown() {
        assert!(is_json_value_type(&Type::Unknown));
    }

    #[test]
    fn test_is_json_value_type_custom_any() {
        assert!(is_json_value_type(&Type::Custom("Any".to_string())));
    }

    #[test]
    fn test_is_json_value_type_custom_value() {
        assert!(is_json_value_type(&Type::Custom("Value".to_string())));
    }

    #[test]
    fn test_is_json_value_type_int() {
        assert!(!is_json_value_type(&Type::Int));
    }

    #[test]
    fn test_is_json_value_type_string() {
        assert!(!is_json_value_type(&Type::String));
    }
}