rusty-ast 0.0.8

A tool that analyzes Rust code and visualizes its AST
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
use quote::ToTokens;
use serde::Serialize;
use syn::{Expr, File, Item, Lit, Pat, Stmt, visit::Visit};

/// A serializable representation of a Rust AST for JSON output
///
/// # Fields
/// * `items`: Vec<ItemJson> - the items in the AST
#[derive(Serialize, Debug, Default)]
pub struct AstJson {
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub items: Vec<ItemJson>,
}

/// # Fields
/// * `name`: String - the name of the item
/// * `parameters`: Vec<ParameterJson> - the parameters of the item
/// * `return_type`: Option<String> - the return type of the item
/// * `body`: Vec<StmtJson> - the body of the item
#[derive(Serialize, Debug)]
#[serde(tag = "type")]
pub enum ItemJson {
    Function {
        name: String,
        parameters: Vec<ParameterJson>,
        return_type: Option<String>,
        body: Vec<StmtJson>,
    },
    Struct {
        name: String,
        fields: Vec<FieldJson>,
    },
    Enum {
        name: String,
        variants: Vec<VariantJson>,
    },
    Other {
        description: String,
    },
}

/// # Fields
/// * `name`: String - the name of the parameter
/// * `type_info`: String - the type of the parameter
#[derive(Serialize, Debug)]
pub struct ParameterJson {
    pub name: String,
    pub type_info: String,
}

/// # Fields
/// * `name`: Option<String> - the name of the field
/// * `type_info`: String - the type of the field
#[derive(Serialize, Debug)]
pub struct FieldJson {
    pub name: Option<String>,
    pub type_info: String,
}

/// # Fields
/// * `name`: String - the name of the variant
#[derive(Serialize, Debug)]
pub struct VariantJson {
    pub name: String,
}

/// # Fields
/// * `name`: String - the name of the statement
/// * `initializer`: Option<Box<ExprJson>> - the initializer of the statement
#[derive(Serialize, Debug)]
#[serde(tag = "type")]
pub enum StmtJson {
    VariableDeclaration {
        name: String,
        initializer: Option<Box<ExprJson>>,
    },
    Expression {
        expr: Box<ExprJson>,
    },
    Other {
        description: String,
    },
}

/// # Fields
/// * `value`: String - the value of the literal
#[derive(Serialize, Debug)]
#[serde(tag = "type")]
pub enum ExprJson {
    IntLiteral {
        value: String,
    },
    FloatLiteral {
        value: String,
    },
    StringLiteral {
        value: String,
    },
    BoolLiteral {
        value: bool,
    },
    Binary {
        operator: String,
        left: Box<ExprJson>,
        right: Box<ExprJson>,
    },
    FunctionCall {
        function: Box<ExprJson>,
        arguments: Vec<ExprJson>,
    },
    Identifier {
        name: String,
    },
    If {
        condition: Box<ExprJson>,
        then_branch: Vec<StmtJson>,
        else_branch: Option<Box<ExprJson>>,
    },
    Loop {
        body: Vec<StmtJson>,
    },
    While {
        condition: Box<ExprJson>,
        body: Vec<StmtJson>,
    },
    Return {
        value: Option<Box<ExprJson>>,
    },
    Other {
        description: String,
    },
}

/// A visitor that builds a JSON representation of a Rust AST
///
/// # Fields
/// * `ast`: AstJson - the AST to be converted to JSON
pub struct JsonVisitor {
    pub ast: AstJson,
}

/// # Methods
/// * `new()`: creates a new JsonVisitor
/// * `to_json()`: converts the AST to a JSON string
/// * `process_file()`: processes a file and adds its items to the AST
/// * `process_item()`: processes an item and adds it to the AST
impl Default for JsonVisitor {
    fn default() -> Self {
        Self::new()
    }
}

impl JsonVisitor {
    /// new
    ///
    /// # Arguments
    /// * `()`
    ///
    /// # Returns
    /// * `JsonVisitor` - a new JsonVisitor
    pub fn new() -> Self {
        JsonVisitor {
            ast: AstJson::default(),
        }
    }

    /// to_json
    ///
    /// # Arguments
    /// * `self`: &Self - the JsonVisitor
    ///
    /// # Returns
    /// * `String` - the JSON string
    pub fn to_json(&self) -> String {
        match serde_json::to_string_pretty(&self.ast) {
            Ok(json) => json,
            Err(_) => String::from("{}"),
        }
    }

    /// process_file
    ///
    /// # Arguments
    /// * `self`: &mut Self - the JsonVisitor
    /// * `file`: &File - the file to process
    ///
    /// # Returns
    /// * `()`
    pub fn process_file(&mut self, file: &File) {
        for item in &file.items {
            self.process_item(item);
        }
    }

    /// process_item
    ///
    /// # Arguments
    /// * `self`: &mut Self - the JsonVisitor
    /// * `item`: &Item - the item to process
    ///
    /// # Returns
    /// * `()`
    fn process_item(&mut self, item: &Item) {
        match item {
            Item::Fn(item_fn) => {
                let mut parameters = Vec::new();
                for param in &item_fn.sig.inputs {
                    match param {
                        syn::FnArg::Typed(pat_type) => {
                            if let Pat::Ident(pat_ident) = &*pat_type.pat {
                                parameters.push(ParameterJson {
                                    name: pat_ident.ident.to_string(),
                                    type_info: format!("{}", (*pat_type.ty).to_token_stream()),
                                });
                            }
                        }
                        syn::FnArg::Receiver(receiver) => {
                            parameters.push(ParameterJson {
                                name: "self".to_string(),
                                type_info: format!("{}", receiver.to_token_stream()),
                            });
                        }
                    }
                }

                let return_type = match &item_fn.sig.output {
                    syn::ReturnType::Default => None,
                    syn::ReturnType::Type(_, return_type) => {
                        Some(format!("{}", return_type.to_token_stream()))
                    }
                };

                let mut statements = Vec::new();
                for stmt in &item_fn.block.stmts {
                    statements.push(self.visit_stmt_json(stmt));
                }

                self.ast.items.push(ItemJson::Function {
                    name: item_fn.sig.ident.to_string(),
                    parameters,
                    return_type,
                    body: statements,
                });
            }
            Item::Struct(item_struct) => {
                let mut fields = Vec::new();
                for field in &item_struct.fields {
                    fields.push(FieldJson {
                        name: field.ident.as_ref().map(|ident| ident.to_string()),
                        type_info: format!("{}", field.ty.to_token_stream()),
                    });
                }

                self.ast.items.push(ItemJson::Struct {
                    name: item_struct.ident.to_string(),
                    fields,
                });
            }
            Item::Enum(item_enum) => {
                let mut variants = Vec::new();
                for variant in &item_enum.variants {
                    variants.push(VariantJson {
                        name: variant.ident.to_string(),
                    });
                }

                self.ast.items.push(ItemJson::Enum {
                    name: item_enum.ident.to_string(),
                    variants,
                });
            }
            _ => {
                self.ast.items.push(ItemJson::Other {
                    description: format!("{}", item.to_token_stream()),
                });
            }
        }
    }
}

/// # Implementations
/// * `Visit<'ast>` - the Visit trait
impl<'ast> Visit<'ast> for JsonVisitor {
    /// visit_file
    ///
    /// # Arguments
    /// * `self`: &mut Self - the JsonVisitor
    /// * `file`: &File - the file to process
    ///
    /// # Returns
    /// * `()`
    fn visit_file(&mut self, file: &'ast File) {
        self.process_file(file);
    }
}

/// # Implementations
/// * `Visit<'ast>` - the Visit trait
impl JsonVisitor {
    /// visit_stmt_json
    ///
    /// # Arguments
    /// * `self`: &mut Self - the JsonVisitor
    /// * `stmt`: &Stmt - the statement to process
    ///
    /// # Returns
    fn visit_stmt_json(&mut self, stmt: &Stmt) -> StmtJson {
        match stmt {
            Stmt::Local(local) => {
                let name = if let Pat::Ident(pat_ident) = &local.pat {
                    pat_ident.ident.to_string()
                } else {
                    "unknown".to_string()
                };

                let initializer = local.init.as_ref().map(|init| Box::new(self.visit_expr_json(&init.expr)));

                StmtJson::VariableDeclaration { name, initializer }
            }
            Stmt::Expr(expr, _) => StmtJson::Expression {
                expr: Box::new(self.visit_expr_json(expr)),
            },
            _ => StmtJson::Other {
                description: format!("{}", stmt.to_token_stream()),
            },
        }
    }

    /// visit_expr_json
    ///
    /// # Arguments
    /// * `self`: &mut Self - the JsonVisitor
    /// * `expr`: &Expr - the expression to process
    ///
    /// # Returns
    /// * `ExprJson` - the JSON representation of the expression
    fn visit_expr_json(&mut self, expr: &Expr) -> ExprJson {
        match expr {
            Expr::Lit(expr_lit) => match &expr_lit.lit {
                Lit::Int(lit_int) => ExprJson::IntLiteral {
                    value: lit_int.base10_digits().to_string(),
                },
                Lit::Float(lit_float) => ExprJson::FloatLiteral {
                    value: lit_float.base10_digits().to_string(),
                },
                Lit::Str(lit_str) => ExprJson::StringLiteral {
                    value: lit_str.value(),
                },
                Lit::Bool(lit_bool) => ExprJson::BoolLiteral {
                    value: lit_bool.value,
                },
                _ => ExprJson::Other {
                    description: format!("{}", expr_lit.to_token_stream()),
                },
            },
            Expr::Binary(expr_bin) => {
                let op = match expr_bin.op {
                    syn::BinOp::Add(_) => "+",
                    syn::BinOp::Sub(_) => "-",
                    syn::BinOp::Mul(_) => "*",
                    syn::BinOp::Div(_) => "/",
                    syn::BinOp::Eq(_) => "==",
                    syn::BinOp::Lt(_) => "<",
                    syn::BinOp::Le(_) => "<=",
                    syn::BinOp::Ne(_) => "!=",
                    syn::BinOp::Ge(_) => ">=",
                    syn::BinOp::Gt(_) => ">",
                    _ => "other_operator",
                };

                ExprJson::Binary {
                    operator: op.to_string(),
                    left: Box::new(self.visit_expr_json(&expr_bin.left)),
                    right: Box::new(self.visit_expr_json(&expr_bin.right)),
                }
            }
            Expr::Call(expr_call) => ExprJson::FunctionCall {
                function: Box::new(self.visit_expr_json(&expr_call.func)),
                arguments: expr_call
                    .args
                    .iter()
                    .map(|arg| self.visit_expr_json(arg))
                    .collect(),
            },
            Expr::Path(expr_path) => ExprJson::Identifier {
                name: format!("{}", expr_path.to_token_stream()),
            },
            Expr::If(expr_if) => {
                let mut then_stmts = Vec::new();
                for stmt in &expr_if.then_branch.stmts {
                    then_stmts.push(self.visit_stmt_json(stmt));
                }

                let else_branch = if let Some((_, else_expr)) = &expr_if.else_branch {
                    Some(Box::new(self.visit_expr_json(else_expr)))
                } else {
                    None
                };

                ExprJson::If {
                    condition: Box::new(self.visit_expr_json(&expr_if.cond)),
                    then_branch: then_stmts,
                    else_branch,
                }
            }
            Expr::Loop(expr_loop) => {
                let mut stmts = Vec::new();
                for stmt in &expr_loop.body.stmts {
                    stmts.push(self.visit_stmt_json(stmt));
                }

                ExprJson::Loop { body: stmts }
            }
            Expr::While(expr_while) => {
                let mut stmts = Vec::new();
                for stmt in &expr_while.body.stmts {
                    stmts.push(self.visit_stmt_json(stmt));
                }

                ExprJson::While {
                    condition: Box::new(self.visit_expr_json(&expr_while.cond)),
                    body: stmts,
                }
            }
            Expr::Return(expr_return) => ExprJson::Return {
                value: expr_return
                    .expr
                    .as_ref()
                    .map(|e| Box::new(self.visit_expr_json(e))),
            },
            _ => ExprJson::Other {
                description: format!("{}", expr.to_token_stream()),
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse_rust_source;
    use serde_json::Value;

    #[test]
    fn test_json_serialization_function() {
        let source = r#"
            fn add(a: i32, b: i32) -> i32 {
                a + b
            }
        "#;

        let file = parse_rust_source(source).unwrap();
        let mut visitor = JsonVisitor::new();
        visitor.process_file(&file);

        let json = visitor.to_json();

        // JSONをパースして構造を確認
        let parsed: Value = serde_json::from_str(&json).expect("JSONのパースに失敗");

        // 構造を確認 - itemsは配列
        assert!(parsed["items"].is_array());

        // 最初の要素を取得
        let first_item = &parsed["items"][0];

        // 型と名前を確認
        assert_eq!(first_item["type"], "Function");
        assert_eq!(first_item["name"], "add");

        // パラメータを確認
        assert!(first_item["parameters"].is_array());
        assert_eq!(first_item["parameters"][0]["name"], "a");
        assert_eq!(first_item["parameters"][1]["name"], "b");

        // 戻り値を確認
        assert_eq!(first_item["return_type"], "i32");
    }

    #[test]
    fn test_json_serialization_struct() {
        let source = r#"
            struct Point {
                x: f64,
                y: f64,
            }
        "#;

        let file = parse_rust_source(source).unwrap();
        let mut visitor = JsonVisitor::new();
        visitor.process_file(&file);

        let json = visitor.to_json();

        // JSONをパースして構造を確認
        let parsed: Value = serde_json::from_str(&json).expect("JSONのパースに失敗");

        // 構造を確認
        assert!(parsed["items"].is_array());

        // 最初の要素を取得
        let first_item = &parsed["items"][0];

        // 型と名前を確認
        assert_eq!(first_item["type"], "Struct");
        assert_eq!(first_item["name"], "Point");

        // フィールドを確認
        assert!(first_item["fields"].is_array());
        assert_eq!(first_item["fields"][0]["name"], "x");
        assert_eq!(first_item["fields"][1]["name"], "y");
    }

    #[test]
    fn test_json_serialization_enum() {
        let source = r#"
            enum Direction {
                North,
                East,
                South,
                West,
            }
        "#;

        let file = parse_rust_source(source).unwrap();
        let mut visitor = JsonVisitor::new();
        visitor.process_file(&file);

        let json = visitor.to_json();

        // JSONをパースして構造を確認
        let parsed: Value = serde_json::from_str(&json).expect("JSONのパースに失敗");

        // 構造を確認
        assert!(parsed["items"].is_array());

        // 最初の要素を取得
        let first_item = &parsed["items"][0];

        // 型と名前を確認
        assert_eq!(first_item["type"], "Enum");
        assert_eq!(first_item["name"], "Direction");

        // バリアントを確認
        assert!(first_item["variants"].is_array());
        assert_eq!(first_item["variants"][0]["name"], "North");
        assert_eq!(first_item["variants"][1]["name"], "East");
        assert_eq!(first_item["variants"][2]["name"], "South");
        assert_eq!(first_item["variants"][3]["name"], "West");
    }

    #[test]
    fn test_json_serialization_complex() {
        let source = r#"
            fn complex_expr() {
                let result = (10 + 20) * 30 / (5 - 2);
                if result > 100 {
                    println!("Large result: {}", result);
                } else {
                    println!("Small result: {}", result);
                }
            }
        "#;

        let file = parse_rust_source(source).unwrap();
        let mut visitor = JsonVisitor::new();
        visitor.process_file(&file);

        let json = visitor.to_json();

        // JSONをパースして構造を確認
        let parsed: Value = serde_json::from_str(&json).expect("JSONのパースに失敗");

        // 構造を確認
        assert!(parsed["items"].is_array());

        // 最初の要素を取得
        let first_item = &parsed["items"][0];

        // 型と名前を確認
        assert_eq!(first_item["type"], "Function");
        assert_eq!(first_item["name"], "complex_expr");

        // 関数本体の文を確認
        assert!(first_item["body"].is_array());
        assert_eq!(first_item["body"][0]["type"], "VariableDeclaration");
        assert_eq!(first_item["body"][0]["name"], "result");

        // if文を確認
        assert_eq!(first_item["body"][1]["type"], "Expression");
        assert_eq!(first_item["body"][1]["expr"]["type"], "If");
    }

    // 追加のデバッグテスト
    #[test]
    fn test_debug_json_output() {
        let source = r#"
            fn test_func() {
                println!("Hello");
            }
        "#;

        let file = parse_rust_source(source).unwrap();
        let mut visitor = JsonVisitor::new();
        visitor.process_file(&file);

        // JSON化
        let json = visitor.to_json();

        // 検証 - 項目が空でないこと
        assert!(!visitor.ast.items.is_empty(), "AST項目が空です!");

        // JSONをパースして構造を確認
        let parsed: Value = serde_json::from_str(&json).expect("JSONのパースに失敗");

        // 構造を確認
        assert!(parsed["items"].is_array());

        // 最初の要素を取得
        let first_item = &parsed["items"][0];

        // 型と名前を確認
        assert_eq!(first_item["type"], "Function");
        assert_eq!(first_item["name"], "test_func");
    }

    // 基本的なシリアライズのテスト
    #[test]
    fn test_basic_serialization() {
        // 手動でAstJson構造を作成
        let mut ast = AstJson::default();

        // 関数アイテムを追加
        ast.items.push(ItemJson::Function {
            name: "manual_func".to_string(),
            parameters: vec![],
            return_type: Some("i32".to_string()),
            body: vec![],
        });

        // JSONシリアライズ
        let json = serde_json::to_string_pretty(&ast).unwrap();

        // JSONをパースして構造を確認
        let parsed: Value = serde_json::from_str(&json).expect("JSONのパースに失敗");

        // 構造を確認
        assert!(parsed["items"].is_array());

        // 最初の要素を取得
        let first_item = &parsed["items"][0];

        // 型と名前を確認
        assert_eq!(first_item["type"], "Function");
        assert_eq!(first_item["name"], "manual_func");
    }
}