Skip to main content

rinne_graph/
model.rs

1//! Plain data types for the code graph: symbols, edges, files, neighborhoods.
2
3use serde::{Deserialize, Serialize};
4
5// SymbolRef and Neighborhood now live in rinne-types so downstream crates can
6// use the CodeGraph trait without depending on rinne-graph.
7pub use rinne_types::graph::{Neighborhood, SymbolRef};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "lowercase")]
11pub enum SymbolKind {
12    Function,
13    Method,
14    Class,
15    Struct,
16    Enum,
17    Trait,
18    Interface,
19    Module,
20}
21
22impl SymbolKind {
23    pub fn as_str(self) -> &'static str {
24        match self {
25            SymbolKind::Function => "function",
26            SymbolKind::Method => "method",
27            SymbolKind::Class => "class",
28            SymbolKind::Struct => "struct",
29            SymbolKind::Enum => "enum",
30            SymbolKind::Trait => "trait",
31            SymbolKind::Interface => "interface",
32            SymbolKind::Module => "module",
33        }
34    }
35
36    #[allow(clippy::should_implement_trait)]
37    pub fn from_str(s: &str) -> Option<Self> {
38        Some(match s {
39            "function" => SymbolKind::Function,
40            "method" => SymbolKind::Method,
41            "class" => SymbolKind::Class,
42            "struct" => SymbolKind::Struct,
43            "enum" => SymbolKind::Enum,
44            "trait" => SymbolKind::Trait,
45            "interface" => SymbolKind::Interface,
46            "module" => SymbolKind::Module,
47            _ => return None,
48        })
49    }
50}
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
53#[serde(rename_all = "lowercase")]
54pub enum EdgeKind {
55    Calls,
56    Imports,
57    Defines,
58    References,
59}
60
61impl EdgeKind {
62    pub fn as_str(self) -> &'static str {
63        match self {
64            EdgeKind::Calls => "calls",
65            EdgeKind::Imports => "imports",
66            EdgeKind::Defines => "defines",
67            EdgeKind::References => "references",
68        }
69    }
70
71    #[allow(clippy::should_implement_trait)]
72    pub fn from_str(s: &str) -> Option<Self> {
73        Some(match s {
74            "calls" => EdgeKind::Calls,
75            "imports" => EdgeKind::Imports,
76            "defines" => EdgeKind::Defines,
77            "references" => EdgeKind::References,
78            _ => return None,
79        })
80    }
81}
82
83#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct Symbol {
85    pub id: i64,
86    pub file: String,
87    pub name: String,
88    pub kind: SymbolKind,
89    pub start_line: u32,
90    pub end_line: u32,
91    pub signature: Option<String>,
92    /// Enclosing type/class/trait for a method; `None` for free functions.
93    pub container: Option<String>,
94}
95
96#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct Edge {
98    pub src: Option<i64>,
99    pub dst: Option<i64>,
100    pub dst_name: String,
101    pub kind: EdgeKind,
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
105pub struct FileRecord {
106    pub path: String,
107    pub lang: String,
108    pub content_hash: String,
109    pub mtime: i64,
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn symbol_kind_roundtrips_through_str() {
118        for k in [
119            SymbolKind::Function,
120            SymbolKind::Method,
121            SymbolKind::Class,
122            SymbolKind::Struct,
123            SymbolKind::Enum,
124            SymbolKind::Trait,
125            SymbolKind::Interface,
126            SymbolKind::Module,
127        ] {
128            assert_eq!(SymbolKind::from_str(k.as_str()), Some(k));
129        }
130    }
131
132    #[test]
133    fn edge_kind_roundtrips_through_str() {
134        for k in [
135            EdgeKind::Calls,
136            EdgeKind::Imports,
137            EdgeKind::Defines,
138            EdgeKind::References,
139        ] {
140            assert_eq!(EdgeKind::from_str(k.as_str()), Some(k));
141        }
142    }
143}