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}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct Edge {
96    pub src: Option<i64>,
97    pub dst: Option<i64>,
98    pub dst_name: String,
99    pub kind: EdgeKind,
100}
101
102#[derive(Debug, Clone, Serialize, Deserialize)]
103pub struct FileRecord {
104    pub path: String,
105    pub lang: String,
106    pub content_hash: String,
107    pub mtime: i64,
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113
114    #[test]
115    fn symbol_kind_roundtrips_through_str() {
116        for k in [
117            SymbolKind::Function,
118            SymbolKind::Method,
119            SymbolKind::Class,
120            SymbolKind::Struct,
121            SymbolKind::Enum,
122            SymbolKind::Trait,
123            SymbolKind::Interface,
124            SymbolKind::Module,
125        ] {
126            assert_eq!(SymbolKind::from_str(k.as_str()), Some(k));
127        }
128    }
129
130    #[test]
131    fn edge_kind_roundtrips_through_str() {
132        for k in [
133            EdgeKind::Calls,
134            EdgeKind::Imports,
135            EdgeKind::Defines,
136            EdgeKind::References,
137        ] {
138            assert_eq!(EdgeKind::from_str(k.as_str()), Some(k));
139        }
140    }
141}