arbor-core 3.1.0

AST parsing and code analysis for Arbor
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
658
//! Code node representation.
//!
//! A CodeNode is our abstraction over raw AST nodes. It captures
//! the semantically meaningful parts of code: what it is, where it lives,
//! and enough metadata to be useful for graph construction.

use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};

/// The kind of code entity this node represents.
///
/// We intentionally keep this list focused on the entities that matter
/// for understanding code structure. Helper nodes like expressions
/// or statements are filtered out during extraction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NodeKind {
    /// A standalone function (not attached to a class).
    Function,
    /// A method inside a class or impl block.
    Method,
    /// A class definition.
    Class,
    /// An interface, protocol, or trait.
    Interface,
    /// A struct (Rust, Go).
    Struct,
    /// An enum definition.
    Enum,
    /// A module-level variable.
    Variable,
    /// A constant or static value.
    Constant,
    /// A type alias.
    TypeAlias,
    /// The file/module itself as a container.
    Module,
    /// An import statement.
    Import,
    /// An export declaration.
    Export,
    /// A constructor (Java, TypeScript class constructors).
    Constructor,
    /// A class field.
    Field,
    /// A document section or heading (for Markdown knowledge graphs in Lattice).
    Section,
}

impl std::fmt::Display for NodeKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::Function => "function",
            Self::Method => "method",
            Self::Class => "class",
            Self::Interface => "interface",
            Self::Struct => "struct",
            Self::Enum => "enum",
            Self::Variable => "variable",
            Self::Constant => "constant",
            Self::TypeAlias => "type_alias",
            Self::Module => "module",
            Self::Import => "import",
            Self::Export => "export",
            Self::Constructor => "constructor",
            Self::Field => "field",
            Self::Section => "section",
        };
        write!(f, "{}", s)
    }
}

/// Prefix on [`CodeNode::references`] for a superclass name.
///
/// These are not call targets. The graph builder turns them into `extends`
/// edges and does not feed them to PageRank.
pub const EXTENDS_REF_PREFIX: &str = "extends:";

/// Prefix on [`CodeNode::references`] for an implemented interface or trait.
pub const IMPLEMENTS_REF_PREFIX: &str = "implements:";

/// Prefix for a field's type stored on a struct node: `field:draft:Draft`.
///
/// The type is the one a method call on the field reaches, with references,
/// smart pointers, locks and containers already stripped (`Mutex<Draft>` and
/// `Option<Box<Draft>>` both record `Draft`). The graph builder follows these
/// to resolve `state.draft.run_started()` when `state`'s struct lives in
/// another file.
pub const FIELD_REF_PREFIX: &str = "field:";

/// Encodes a struct field's type as a reference.
pub fn field_ref(field: &str, type_name: &str) -> String {
    format!("{FIELD_REF_PREFIX}{field}:{type_name}")
}

/// Splits a `field:name:Type` reference into the field name and its type.
pub fn field_type_ref(reference: &str) -> Option<(&str, &str)> {
    let (field, type_name) = reference.strip_prefix(FIELD_REF_PREFIX)?.split_once(':')?;
    (!field.is_empty() && !type_name.is_empty()).then_some((field, type_name))
}

/// Prefix for the type a function returns, stored on its node:
/// `returns:PreparedDraft` for `-> Option<mac::PreparedDraft>`.
/// Stripped like a field type, with `Self` already replaced by the impl type.
pub const RETURNS_REF_PREFIX: &str = "returns:";

/// Encodes a function's (stripped) return type as a reference.
pub fn returns_ref(type_name: &str) -> String {
    format!("{RETURNS_REF_PREFIX}{type_name}")
}

/// The return type recorded by a `returns:Type` reference.
pub fn return_type_ref(reference: &str) -> Option<&str> {
    reference
        .strip_prefix(RETURNS_REF_PREFIX)
        .filter(|type_name| !type_name.is_empty())
}

/// Encodes a method call whose receiver's type is known from the code:
/// `.run_started@Draft`, or `.run_started@AppState.draft` for a call on a
/// field of a value of type `AppState`.
///
/// The type path starts at a type name or at a call whose return type
/// decides it (`prepare()`, `mac::Draft::capture()`), then reads fields
/// (`.draft`) and method results (`.load()`) in order.
///
/// A plain `.name` reference means the receiver's type is unknown.
pub fn typed_receiver_ref(method: &str, receiver_type: &str) -> String {
    format!(".{method}@{receiver_type}")
}

/// Splits a typed receiver reference into the method name and the receiver's
/// type path (`Type` or `Type.field.field`).
pub fn typed_receiver(reference: &str) -> Option<(&str, &str)> {
    let (method, receiver) = reference.strip_prefix('.')?.split_once('@')?;
    (!method.is_empty() && !receiver.is_empty()).then_some((method, receiver))
}

/// A type relationship stored on a node until the graph builder resolves it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeRelationKind {
    /// `class Middle(Base)` — subclass depends on the base.
    Extends,
    /// `class Foo implements Bar` — implementor depends on the interface.
    Implements,
}

/// Splits an `extends:` / `implements:` reference into its kind and type name.
///
/// Returns `None` for ordinary call references.
pub fn type_relation_ref(reference: &str) -> Option<(TypeRelationKind, &str)> {
    let reference = reference.trim();
    if let Some(name) = reference.strip_prefix(EXTENDS_REF_PREFIX) {
        let name = name.trim();
        if !name.is_empty() {
            return Some((TypeRelationKind::Extends, name));
        }
    }
    if let Some(name) = reference.strip_prefix(IMPLEMENTS_REF_PREFIX) {
        let name = name.trim();
        if !name.is_empty() {
            return Some((TypeRelationKind::Implements, name));
        }
    }
    None
}

/// Strips pointers, references, and generic arguments from a type name.
///
/// `*Base`, `Foo<T>`, and `pkg.Base` become `Base`, `Foo`, and `pkg.Base`.
/// Keywords and anything that is not a type path become empty.
pub fn clean_type_name(raw: &str) -> String {
    let mut raw = raw.trim();
    loop {
        if let Some(rest) = raw.strip_prefix('*').or_else(|| raw.strip_prefix('&')) {
            raw = rest.trim_start();
            continue;
        }
        if let Some(rest) = raw.strip_prefix("mut ") {
            raw = rest.trim_start();
            continue;
        }
        break;
    }
    let raw = raw.split(['<', '[']).next().unwrap_or(raw).trim();
    let raw = raw.trim_end_matches('?').trim();
    if raw.is_empty() || is_type_keyword(raw) || !is_type_path(raw) {
        return String::new();
    }
    raw.to_string()
}

fn is_type_path(name: &str) -> bool {
    let mut chars = name.chars();
    let Some(first) = chars.next() else {
        return false;
    };
    if !(first.is_alphabetic() || first == '_') {
        return false;
    }
    name.chars()
        .all(|c| c.is_alphanumeric() || c == '_' || c == '.' || c == ':')
}

fn is_type_keyword(name: &str) -> bool {
    matches!(
        name,
        "public"
            | "private"
            | "protected"
            | "virtual"
            | "override"
            | "abstract"
            | "static"
            | "final"
            | "sealed"
            | "partial"
            | "readonly"
            | "const"
            | "class"
            | "struct"
            | "interface"
            | "enum"
            | "extends"
            | "implements"
            | "with"
            | "where"
            | "new"
            | "base"
            | "this"
            | "super"
            | "mut"
            | "pub"
            | "crate"
            | "object"
    )
}

fn push_type_relation(references: &mut Vec<String>, kind: TypeRelationKind, name: &str) {
    let name = clean_type_name(name);
    if name.is_empty() {
        return;
    }
    let prefix = match kind {
        TypeRelationKind::Extends => EXTENDS_REF_PREFIX,
        TypeRelationKind::Implements => IMPLEMENTS_REF_PREFIX,
    };
    let encoded = format!("{prefix}{name}");
    if !references.iter().any(|existing| existing == &encoded) {
        references.push(encoded);
    }
}

/// Visibility of a code entity.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum Visibility {
    #[default]
    Private,
    Public,
    Protected,
    /// Rust's pub(crate) or similar restricted visibility.
    Internal,
}

/// A code entity extracted from source.
///
/// This is the core data type that flows through Arbor. It's designed
/// to be language-agnostic while still capturing the structure we need.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeNode {
    /// Unique identifier, derived from file path + qualified name + kind.
    pub id: String,

    /// The simple name (e.g., "validate_user").
    pub name: String,

    /// Fully qualified name including parent scope (e.g., "UserService.validate_user").
    pub qualified_name: String,

    /// What kind of entity this is.
    pub kind: NodeKind,

    /// Path to the source file, relative to project root.
    pub file: String,

    /// Starting line (1-indexed, like editors show).
    pub line_start: u32,

    /// Ending line (inclusive).
    pub line_end: u32,

    /// Column of the name identifier.
    pub column: u32,

    /// Function/method signature if applicable.
    pub signature: Option<String>,

    /// Visibility modifier.
    pub visibility: Visibility,

    /// Whether this is async.
    pub is_async: bool,

    /// Whether this is static/class-level.
    pub is_static: bool,

    /// Whether this is exported (TS/ES modules).
    pub is_exported: bool,

    /// Docstring or leading comment.
    pub docstring: Option<String>,

    /// Byte offset range in source for incremental updates.
    pub byte_start: u32,
    pub byte_end: u32,

    /// Entities this node references (call targets, type refs, etc).
    /// These are names, not IDs - resolution happens in the graph crate.
    pub references: Vec<String>,
}

impl CodeNode {
    /// Creates a deterministic ID for this node.
    ///
    /// The ID is a hash of (file, qualified_name, kind) so the same
    /// entity always gets the same ID across parses.
    pub fn compute_id(file: &str, qualified_name: &str, kind: NodeKind) -> String {
        use std::collections::hash_map::DefaultHasher;

        let mut hasher = DefaultHasher::new();
        file.hash(&mut hasher);
        qualified_name.hash(&mut hasher);
        kind.hash(&mut hasher);

        format!("{:016x}", hasher.finish())
    }

    /// Creates a new node and automatically computes its ID.
    pub fn new(
        name: impl Into<String>,
        qualified_name: impl Into<String>,
        kind: NodeKind,
        file: impl Into<String>,
    ) -> Self {
        let name = name.into();
        let qualified_name = qualified_name.into();
        let file = file.into();
        let id = Self::compute_id(&file, &qualified_name, kind);

        Self {
            id,
            name,
            qualified_name,
            kind,
            file,
            line_start: 0,
            line_end: 0,
            column: 0,
            signature: None,
            visibility: Visibility::default(),
            is_async: false,
            is_static: false,
            is_exported: false,
            docstring: None,
            byte_start: 0,
            byte_end: 0,
            references: Vec::new(),
        }
    }

    /// Builder pattern: set line range.
    pub fn with_lines(mut self, start: u32, end: u32) -> Self {
        self.line_start = start;
        self.line_end = end;
        self
    }

    /// Builder pattern: set byte range.
    pub fn with_bytes(mut self, start: u32, end: u32) -> Self {
        self.byte_start = start;
        self.byte_end = end;
        self
    }

    /// Builder pattern: set column.
    pub fn with_column(mut self, column: u32) -> Self {
        self.column = column;
        self
    }

    /// Builder pattern: set signature.
    pub fn with_signature(mut self, sig: impl Into<String>) -> Self {
        self.signature = Some(sig.into());
        self
    }

    /// Builder pattern: set visibility.
    pub fn with_visibility(mut self, vis: Visibility) -> Self {
        self.visibility = vis;
        self
    }

    /// Builder pattern: mark as async.
    pub fn as_async(mut self) -> Self {
        self.is_async = true;
        self
    }

    /// Builder pattern: mark as static.
    pub fn as_static(mut self) -> Self {
        self.is_static = true;
        self
    }

    /// Builder pattern: mark as exported.
    pub fn as_exported(mut self) -> Self {
        self.is_exported = true;
        self
    }

    /// Builder pattern: add references.
    pub fn with_references(mut self, refs: Vec<String>) -> Self {
        self.references = refs;
        self
    }

    /// Records a superclass. Appends to [`references`](Self::references); it
    /// does not replace call references already stored there.
    pub fn add_type_relation(&mut self, kind: TypeRelationKind, name: &str) {
        push_type_relation(&mut self.references, kind, name);
    }

    /// Records superclasses this type extends.
    pub fn with_extends<I, S>(mut self, bases: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        for name in bases {
            self.add_type_relation(TypeRelationKind::Extends, name.as_ref());
        }
        self
    }

    /// Records interfaces or traits this type implements.
    pub fn with_implements<I, S>(mut self, interfaces: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        for name in interfaces {
            self.add_type_relation(TypeRelationKind::Implements, name.as_ref());
        }
        self
    }
}

impl PartialEq for CodeNode {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for CodeNode {}

impl Hash for CodeNode {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;

    #[test]
    fn test_node_kind_display_all_variants() {
        // Exhaustive check of Display for every NodeKind variant
        assert_eq!(NodeKind::Function.to_string(), "function");
        assert_eq!(NodeKind::Method.to_string(), "method");
        assert_eq!(NodeKind::Class.to_string(), "class");
        assert_eq!(NodeKind::Struct.to_string(), "struct");
        assert_eq!(NodeKind::Interface.to_string(), "interface");
        assert_eq!(NodeKind::Enum.to_string(), "enum");
        assert_eq!(NodeKind::Module.to_string(), "module");
        assert_eq!(NodeKind::Field.to_string(), "field");
        assert_eq!(NodeKind::Constant.to_string(), "constant");
        assert_eq!(NodeKind::Constructor.to_string(), "constructor");
        assert_eq!(NodeKind::Import.to_string(), "import");
        assert_eq!(NodeKind::Export.to_string(), "export");
        assert_eq!(NodeKind::TypeAlias.to_string(), "type_alias");
        assert_eq!(NodeKind::Variable.to_string(), "variable");
    }

    #[test]
    fn test_visibility_default_is_private() {
        let vis = Visibility::default();
        assert!(matches!(vis, Visibility::Private));
    }

    #[test]
    fn test_builder_pattern_chain() {
        // Verify that all builder methods compose correctly
        let node = CodeNode::new("foo", "pkg.foo", NodeKind::Function, "main.rs")
            .with_lines(10, 20)
            .with_bytes(100, 300)
            .with_column(4)
            .with_signature("fn foo(x: i32) -> bool")
            .with_visibility(Visibility::Public)
            .as_async()
            .as_static()
            .as_exported()
            .with_references(vec!["bar".to_string(), "baz".to_string()]);

        assert_eq!(node.name, "foo");
        assert_eq!(node.qualified_name, "pkg.foo");
        assert_eq!(node.file, "main.rs");
        assert_eq!(node.line_start, 10);
        assert_eq!(node.line_end, 20);
        assert_eq!(node.byte_start, 100);
        assert_eq!(node.byte_end, 300);
        assert_eq!(node.column, 4);
        assert_eq!(node.signature.as_deref(), Some("fn foo(x: i32) -> bool"));
        assert!(matches!(node.visibility, Visibility::Public));
        assert!(node.is_async);
        assert!(node.is_static);
        assert!(node.is_exported);
        assert_eq!(node.references.len(), 2);
    }

    #[test]
    fn test_code_node_equality_by_id() {
        // PartialEq compares by ID only, not by other fields
        let node1 = CodeNode::new("foo", "foo", NodeKind::Function, "a.rs");
        let node2 = CodeNode::new("foo", "foo", NodeKind::Function, "a.rs");
        // Same inputs → same ID → equal
        assert_eq!(node1, node2);

        // Different kind → different ID → not equal
        let node3 = CodeNode::new("foo", "foo", NodeKind::Method, "a.rs");
        assert_ne!(node1, node3);
    }

    #[test]
    fn test_code_node_hash_consistency() {
        // Same node should hash identically, and be usable in HashSet
        let node1 = CodeNode::new("foo", "foo", NodeKind::Function, "main.rs");
        let node2 = CodeNode::new("foo", "foo", NodeKind::Function, "main.rs");

        let mut set = HashSet::new();
        set.insert(node1.clone());
        assert!(set.contains(&node2));
        // Inserting duplicate should not increase size
        set.insert(node2);
        assert_eq!(set.len(), 1);
    }

    #[test]
    fn test_compute_id_deterministic() {
        // Same inputs must always produce the same ID
        let id1 = CodeNode::compute_id("test.rs", "main", NodeKind::Function);
        let id2 = CodeNode::compute_id("test.rs", "main", NodeKind::Function);
        assert_eq!(id1, id2);
    }

    #[test]
    fn test_compute_id_different_kinds_differ() {
        // A function and a struct with the same name should have different IDs
        let id_fn = CodeNode::compute_id("test.rs", "Foo", NodeKind::Function);
        let id_struct = CodeNode::compute_id("test.rs", "Foo", NodeKind::Struct);
        assert_ne!(id_fn, id_struct);
    }

    #[test]
    fn test_compute_id_different_files_differ() {
        let id1 = CodeNode::compute_id("a.rs", "main", NodeKind::Function);
        let id2 = CodeNode::compute_id("b.rs", "main", NodeKind::Function);
        assert_ne!(id1, id2);
    }

    #[test]
    fn field_and_typed_receiver_refs_round_trip() {
        assert_eq!(
            field_type_ref(&field_ref("draft", "Draft")),
            Some(("draft", "Draft"))
        );
        assert_eq!(field_type_ref("field:draft"), None);
        assert_eq!(field_type_ref("draft"), None);
        assert_eq!(
            typed_receiver(&typed_receiver_ref("run_started", "AppState.draft")),
            Some(("run_started", "AppState.draft"))
        );
        // A plain unknown-receiver call and a `self` call are not typed.
        assert_eq!(typed_receiver(".run_started"), None);
        assert_eq!(typed_receiver("self.run_started"), None);
        assert_eq!(type_relation_ref(&field_ref("draft", "Draft")), None);
        assert_eq!(return_type_ref(&returns_ref("Draft")), Some("Draft"));
        assert_eq!(return_type_ref("returns:"), None);
        assert_eq!(
            typed_receiver(".run_started@mac::Draft::capture().load()"),
            Some(("run_started", "mac::Draft::capture().load()"))
        );
    }

    #[test]
    fn type_relation_refs_round_trip() {
        assert_eq!(
            type_relation_ref("extends:Base"),
            Some((TypeRelationKind::Extends, "Base"))
        );
        assert_eq!(
            type_relation_ref("implements:Reader"),
            Some((TypeRelationKind::Implements, "Reader"))
        );
        assert!(type_relation_ref("helper").is_none());
        assert!(type_relation_ref("extends:").is_none());
    }

    #[test]
    fn clean_type_name_strips_generics_and_pointers() {
        assert_eq!(clean_type_name("*Base"), "Base");
        assert_eq!(clean_type_name("Foo<T>"), "Foo");
        assert_eq!(clean_type_name("pkg.Base"), "pkg.Base");
        assert_eq!(clean_type_name("std::fmt::Display"), "std::fmt::Display");
        assert_eq!(clean_type_name("object"), "");
        assert_eq!(clean_type_name("public"), "");
    }

    #[test]
    fn with_extends_appends_without_dropping_calls() {
        let node = CodeNode::new("Middle", "Middle", NodeKind::Class, "a.py")
            .with_references(vec!["helper".to_string()])
            .with_extends(["Base"])
            .with_implements(["Proto"]);
        assert!(node.references.iter().any(|r| r == "helper"));
        assert!(node.references.iter().any(|r| r == "extends:Base"));
        assert!(node.references.iter().any(|r| r == "implements:Proto"));
    }

    #[test]
    fn test_node_default_values() {
        // Verify sensible defaults for a freshly created node
        let node = CodeNode::new("f", "f", NodeKind::Function, "x.rs");
        assert_eq!(node.line_start, 0);
        assert_eq!(node.line_end, 0);
        assert_eq!(node.byte_start, 0);
        assert_eq!(node.byte_end, 0);
        assert_eq!(node.column, 0);
        assert!(node.signature.is_none());
        assert!(!node.is_async);
        assert!(!node.is_static);
        assert!(!node.is_exported);
        assert!(node.references.is_empty());
        assert!(matches!(node.visibility, Visibility::Private));
    }
}