codegraph_python/relationships/
calls.rs

1use serde::{Deserialize, Serialize};
2
3/// Represents a function call from one function to another
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5pub struct CallRelation {
6    /// Name of the calling function
7    pub caller: String,
8
9    /// Name of the called function
10    pub callee: String,
11
12    /// Line number where the call occurs
13    pub line: usize,
14
15    /// Is this a method call (obj.method()) vs function call?
16    pub is_method_call: bool,
17}
18
19impl CallRelation {
20    /// Create a new function call relation
21    pub fn new(caller: impl Into<String>, callee: impl Into<String>, line: usize) -> Self {
22        Self {
23            caller: caller.into(),
24            callee: callee.into(),
25            line,
26            is_method_call: false,
27        }
28    }
29
30    /// Create a method call relation
31    pub fn method_call(caller: impl Into<String>, callee: impl Into<String>, line: usize) -> Self {
32        Self {
33            caller: caller.into(),
34            callee: callee.into(),
35            line,
36            is_method_call: true,
37        }
38    }
39
40    /// Set whether this is a method call
41    pub fn set_method_call(mut self, is_method_call: bool) -> Self {
42        self.is_method_call = is_method_call;
43        self
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_call_relation_new() {
53        let call = CallRelation::new("foo", "bar", 10);
54        assert_eq!(call.caller, "foo");
55        assert_eq!(call.callee, "bar");
56        assert_eq!(call.line, 10);
57        assert!(!call.is_method_call);
58    }
59
60    #[test]
61    fn test_method_call() {
62        let call = CallRelation::method_call("foo", "bar", 20);
63        assert_eq!(call.caller, "foo");
64        assert_eq!(call.callee, "bar");
65        assert_eq!(call.line, 20);
66        assert!(call.is_method_call);
67    }
68}