codegraph 0.1.1

A fast, reliable, and flexible graph database optimized for storing and querying code relationships
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
//! Convenience helpers for common code entities and relationships.
//!
//! This module provides higher-level abstractions for working with code graphs,
//! reducing boilerplate for common operations like adding files, functions, classes,
//! and tracking relationships between them.

use crate::error::Result;
use crate::graph::{CodeGraph, Direction, EdgeId, EdgeType, NodeId, NodeType, PropertyMap};

/// Metadata for a function with extended properties.
pub struct FunctionMetadata<'a> {
    /// Function name
    pub name: &'a str,
    /// Starting line number
    pub line_start: i64,
    /// Ending line number
    pub line_end: i64,
    /// Visibility modifier (e.g., "public", "private")
    pub visibility: &'a str,
    /// Function signature string
    pub signature: &'a str,
    /// Whether the function is async
    pub is_async: bool,
    /// Whether the function is a test
    pub is_test: bool,
}

/// Add a file node to the graph.
///
/// Creates a CodeFile node with path and language properties.
///
/// # Arguments
///
/// * `graph` - The code graph to add the file to
/// * `path` - File path (e.g., "src/main.rs")
/// * `language` - Programming language (e.g., "rust", "python")
///
/// # Returns
///
/// The ID of the created file node.
pub fn add_file(graph: &mut CodeGraph, path: &str, language: &str) -> Result<NodeId> {
    let props = PropertyMap::new()
        .with("path", path)
        .with("language", language);

    graph.add_node(NodeType::CodeFile, props)
}

/// Add a function node and automatically link it to a file.
///
/// Creates a Function node and a Contains edge from the file to the function.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `file_id` - The ID of the file containing this function
/// * `name` - Function name
/// * `line_start` - Starting line number
/// * `line_end` - Ending line number
///
/// # Returns
///
/// The ID of the created function node.
pub fn add_function(
    graph: &mut CodeGraph,
    file_id: NodeId,
    name: &str,
    line_start: i64,
    line_end: i64,
) -> Result<NodeId> {
    let props = PropertyMap::new()
        .with("name", name)
        .with("line_start", line_start)
        .with("line_end", line_end);

    let func_id = graph.add_node(NodeType::Function, props)?;

    // Auto-create Contains edge
    graph.add_edge(file_id, func_id, EdgeType::Contains, PropertyMap::new())?;

    Ok(func_id)
}

/// Add a function node with extended metadata.
///
/// Creates a Function node with additional properties like visibility, signature, etc.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `file_id` - The ID of the file containing this function
/// * `metadata` - Function metadata including name, lines, visibility, etc.
///
/// # Returns
///
/// The ID of the created function node.
pub fn add_function_with_metadata(
    graph: &mut CodeGraph,
    file_id: NodeId,
    metadata: FunctionMetadata,
) -> Result<NodeId> {
    let props = PropertyMap::new()
        .with("name", metadata.name)
        .with("line_start", metadata.line_start)
        .with("line_end", metadata.line_end)
        .with("visibility", metadata.visibility)
        .with("signature", metadata.signature)
        .with("is_async", metadata.is_async)
        .with("is_test", metadata.is_test);

    let func_id = graph.add_node(NodeType::Function, props)?;

    // Auto-create Contains edge
    graph.add_edge(file_id, func_id, EdgeType::Contains, PropertyMap::new())?;

    Ok(func_id)
}

/// Add a class node and automatically link it to a file.
///
/// Creates a Class node and a Contains edge from the file to the class.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `file_id` - The ID of the file containing this class
/// * `name` - Class name
/// * `line_start` - Starting line number
/// * `line_end` - Ending line number
///
/// # Returns
///
/// The ID of the created class node.
pub fn add_class(
    graph: &mut CodeGraph,
    file_id: NodeId,
    name: &str,
    line_start: i64,
    line_end: i64,
) -> Result<NodeId> {
    let props = PropertyMap::new()
        .with("name", name)
        .with("line_start", line_start)
        .with("line_end", line_end);

    let class_id = graph.add_node(NodeType::Class, props)?;

    // Auto-create Contains edge
    graph.add_edge(file_id, class_id, EdgeType::Contains, PropertyMap::new())?;

    Ok(class_id)
}

/// Add a method node and link it to a class.
///
/// Creates a Function node and a Contains edge from the class to the method.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `class_id` - The ID of the class containing this method
/// * `name` - Method name
/// * `line_start` - Starting line number
/// * `line_end` - Ending line number
///
/// # Returns
///
/// The ID of the created method node.
pub fn add_method(
    graph: &mut CodeGraph,
    class_id: NodeId,
    name: &str,
    line_start: i64,
    line_end: i64,
) -> Result<NodeId> {
    let props = PropertyMap::new()
        .with("name", name)
        .with("line_start", line_start)
        .with("line_end", line_end);

    let method_id = graph.add_node(NodeType::Function, props)?;

    // Link to class
    graph.add_edge(class_id, method_id, EdgeType::Contains, PropertyMap::new())?;

    Ok(method_id)
}

/// Add a module node to the graph.
///
/// Creates a Module node with name and path properties.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `name` - Module name
/// * `path` - Module path
///
/// # Returns
///
/// The ID of the created module node.
pub fn add_module(graph: &mut CodeGraph, name: &str, path: &str) -> Result<NodeId> {
    let props = PropertyMap::new().with("name", name).with("path", path);

    graph.add_node(NodeType::Module, props)
}

/// Add a function call relationship with line metadata.
///
/// Creates a Calls edge from caller to callee with the line number where the call occurs.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `caller_id` - The ID of the calling function
/// * `callee_id` - The ID of the called function
/// * `line` - Line number where the call occurs
///
/// # Returns
///
/// The ID of the created Calls edge.
pub fn add_call(
    graph: &mut CodeGraph,
    caller_id: NodeId,
    callee_id: NodeId,
    line: i64,
) -> Result<EdgeId> {
    let props = PropertyMap::new().with("line", line);
    graph.add_edge(caller_id, callee_id, EdgeType::Calls, props)
}

/// Add an import relationship with imported symbols.
///
/// Creates an Imports edge from one file to another with a list of imported symbols.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `from_file_id` - The ID of the file doing the import
/// * `to_file_id` - The ID of the file being imported
/// * `symbols` - List of imported symbol names
///
/// # Returns
///
/// The ID of the created Imports edge.
pub fn add_import(
    graph: &mut CodeGraph,
    from_file_id: NodeId,
    to_file_id: NodeId,
    symbols: Vec<&str>,
) -> Result<EdgeId> {
    let symbol_strings: Vec<String> = symbols.iter().map(|s| s.to_string()).collect();
    let props = PropertyMap::new().with("symbols", symbol_strings);
    graph.add_edge(from_file_id, to_file_id, EdgeType::Imports, props)
}

/// Create a generic Contains edge between two nodes.
///
/// This is useful for linking any entity to a file.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `container_id` - The ID of the containing node (e.g., file)
/// * `contained_id` - The ID of the contained node
///
/// # Returns
///
/// The ID of the created Contains edge.
pub fn link_to_file(
    graph: &mut CodeGraph,
    container_id: NodeId,
    contained_id: NodeId,
) -> Result<EdgeId> {
    graph.add_edge(
        container_id,
        contained_id,
        EdgeType::Contains,
        PropertyMap::new(),
    )
}

/// Get all functions that call the given function.
///
/// Returns the node IDs of all functions with incoming Calls edges.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `function_id` - The ID of the function to find callers for
///
/// # Returns
///
/// Vector of node IDs of functions that call this function.
pub fn get_callers(graph: &CodeGraph, function_id: NodeId) -> Result<Vec<NodeId>> {
    let incoming = graph.get_neighbors(function_id, Direction::Incoming)?;

    let mut callers = Vec::new();
    for neighbor_id in incoming {
        // Check if the edge is a Calls edge
        let edges = graph.get_edges_between(neighbor_id, function_id)?;
        for edge_id in edges {
            let edge = graph.get_edge(edge_id)?;
            if edge.edge_type == EdgeType::Calls {
                callers.push(neighbor_id);
                break;
            }
        }
    }

    Ok(callers)
}

/// Get all functions called by the given function.
///
/// Returns the node IDs of all functions with outgoing Calls edges.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `function_id` - The ID of the function to find callees for
///
/// # Returns
///
/// Vector of node IDs of functions called by this function.
pub fn get_callees(graph: &CodeGraph, function_id: NodeId) -> Result<Vec<NodeId>> {
    let outgoing = graph.get_neighbors(function_id, Direction::Outgoing)?;

    let mut callees = Vec::new();
    for neighbor_id in outgoing {
        // Check if the edge is a Calls edge
        let edges = graph.get_edges_between(function_id, neighbor_id)?;
        for edge_id in edges {
            let edge = graph.get_edge(edge_id)?;
            if edge.edge_type == EdgeType::Calls {
                callees.push(neighbor_id);
                break;
            }
        }
    }

    Ok(callees)
}

/// Get all functions contained in a file.
///
/// Returns the node IDs of all Function nodes connected to the file via Contains edges.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `file_id` - The ID of the file to find functions in
///
/// # Returns
///
/// Vector of node IDs of functions in this file.
pub fn get_functions_in_file(graph: &CodeGraph, file_id: NodeId) -> Result<Vec<NodeId>> {
    let contained = graph.get_neighbors(file_id, Direction::Outgoing)?;

    let mut functions = Vec::new();
    for node_id in contained {
        let node = graph.get_node(node_id)?;
        // Only include Function nodes
        if node.node_type == NodeType::Function {
            functions.push(node_id);
        }
    }

    Ok(functions)
}

/// Get all files that a file depends on (imports from).
///
/// Returns the node IDs of all files connected via outgoing Imports or ImportsFrom edges.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `file_id` - The ID of the file to find dependencies for
///
/// # Returns
///
/// Vector of node IDs of files that this file imports.
pub fn get_file_dependencies(graph: &CodeGraph, file_id: NodeId) -> Result<Vec<NodeId>> {
    let outgoing = graph.get_neighbors(file_id, Direction::Outgoing)?;

    let mut dependencies = Vec::new();
    for neighbor_id in outgoing {
        // Check if the edge is Imports or ImportsFrom
        let edges = graph.get_edges_between(file_id, neighbor_id)?;
        for edge_id in edges {
            let edge = graph.get_edge(edge_id)?;
            if edge.edge_type == EdgeType::Imports || edge.edge_type == EdgeType::ImportsFrom {
                dependencies.push(neighbor_id);
                break;
            }
        }
    }

    Ok(dependencies)
}

/// Get all files that depend on this file (import this file).
///
/// Returns the node IDs of all files connected via incoming Imports or ImportsFrom edges.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `file_id` - The ID of the file to find dependents for
///
/// # Returns
///
/// Vector of node IDs of files that import this file.
pub fn get_file_dependents(graph: &CodeGraph, file_id: NodeId) -> Result<Vec<NodeId>> {
    let incoming = graph.get_neighbors(file_id, Direction::Incoming)?;

    let mut dependents = Vec::new();
    for neighbor_id in incoming {
        // Check if the edge is Imports or ImportsFrom
        let edges = graph.get_edges_between(neighbor_id, file_id)?;
        for edge_id in edges {
            let edge = graph.get_edge(edge_id)?;
            if edge.edge_type == EdgeType::Imports || edge.edge_type == EdgeType::ImportsFrom {
                dependents.push(neighbor_id);
                break;
            }
        }
    }

    Ok(dependents)
}

// ===== Transitive Dependency Analysis =====

/// Find all transitive dependencies of a file (what it imports, directly or indirectly).
///
/// Uses BFS to follow Imports/ImportsFrom edges to find all files that this file
/// depends on, directly or transitively. Handles cycles gracefully.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `file_id` - The starting file node ID
/// * `max_depth` - Optional maximum depth to traverse (None for unlimited)
///
/// # Returns
///
/// Vector of node IDs of all files this file depends on (transitively).
pub fn transitive_dependencies(
    graph: &CodeGraph,
    file_id: NodeId,
    max_depth: Option<usize>,
) -> Result<Vec<NodeId>> {
    use std::collections::{HashSet, VecDeque};

    let mut visited = HashSet::new();
    let mut queue = VecDeque::new();
    let mut result = Vec::new();

    visited.insert(file_id);
    queue.push_back((file_id, 0));

    while let Some((current, depth)) = queue.pop_front() {
        // Check depth limit
        if let Some(max) = max_depth {
            if depth >= max {
                continue;
            }
        }

        // Get direct dependencies
        let deps = get_file_dependencies(graph, current)?;

        for dep_id in deps {
            if !visited.contains(&dep_id) {
                visited.insert(dep_id);
                result.push(dep_id);
                queue.push_back((dep_id, depth + 1));
            }
        }
    }

    Ok(result)
}

/// Find all transitive dependents of a file (what imports it, directly or indirectly).
///
/// Uses reverse BFS to follow incoming Imports/ImportsFrom edges to find all files
/// that depend on this file, directly or transitively. Handles cycles gracefully.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `file_id` - The starting file node ID
/// * `max_depth` - Optional maximum depth to traverse (None for unlimited)
///
/// # Returns
///
/// Vector of node IDs of all files that depend on this file (transitively).
pub fn transitive_dependents(
    graph: &CodeGraph,
    file_id: NodeId,
    max_depth: Option<usize>,
) -> Result<Vec<NodeId>> {
    use std::collections::{HashSet, VecDeque};

    let mut visited = HashSet::new();
    let mut queue = VecDeque::new();
    let mut result = Vec::new();

    visited.insert(file_id);
    queue.push_back((file_id, 0));

    while let Some((current, depth)) = queue.pop_front() {
        // Check depth limit
        if let Some(max) = max_depth {
            if depth >= max {
                continue;
            }
        }

        // Get direct dependents
        let dependents = get_file_dependents(graph, current)?;

        for dependent_id in dependents {
            if !visited.contains(&dependent_id) {
                visited.insert(dependent_id);
                result.push(dependent_id);
                queue.push_back((dependent_id, depth + 1));
            }
        }
    }

    Ok(result)
}

/// Find all call chains (paths) between two functions.
///
/// Uses path finding to discover all possible ways one function can reach another
/// through intermediate function calls.
///
/// # Arguments
///
/// * `graph` - The code graph
/// * `from_func` - Starting function node ID
/// * `to_func` - Target function node ID
/// * `max_depth` - Maximum path length (recommended to prevent infinite search)
///
/// # Returns
///
/// Vector of call chains, where each chain is a Vec of node IDs from start to end.
pub fn call_chain(
    graph: &CodeGraph,
    from_func: NodeId,
    to_func: NodeId,
    max_depth: Option<usize>,
) -> Result<Vec<Vec<NodeId>>> {
    graph.find_all_paths(from_func, to_func, max_depth)
}

/// Detect circular dependencies in file imports.
///
/// Uses Tarjan's strongly connected components algorithm to find groups of files
/// that form circular import chains.
///
/// # Arguments
///
/// * `graph` - The code graph
///
/// # Returns
///
/// Vector of circular dependency groups, where each group is a Vec of file node IDs
/// that form a cycle.
pub fn circular_deps(graph: &CodeGraph) -> Result<Vec<Vec<NodeId>>> {
    // Find all SCCs in the graph
    let sccs = graph.find_strongly_connected_components()?;

    // Filter to only include SCCs that contain CodeFile nodes
    let mut file_cycles = Vec::new();

    for scc in sccs {
        // Check if this SCC contains file nodes
        let mut file_nodes = Vec::new();
        for node_id in &scc {
            if let Ok(node) = graph.get_node(*node_id) {
                if node.node_type == NodeType::CodeFile {
                    file_nodes.push(*node_id);
                }
            }
        }

        // If we found file nodes in this SCC, it's a circular dependency
        if file_nodes.len() > 1 {
            file_cycles.push(file_nodes);
        }
    }

    Ok(file_cycles)
}