Skip to main content

ryo_app/graph/
types.rs

1//! Core types for Graph module
2
3use ryo_analysis::{SymbolId, SymbolKind};
4use std::path::PathBuf;
5
6/// Graph statistics
7#[derive(Debug, Clone)]
8pub struct GraphStats {
9    pub files: usize,
10    pub nodes: usize,
11    pub functions: usize,
12    pub structs: usize,
13    pub enums: usize,
14    pub traits: usize,
15    pub impls: usize,
16    pub edges: usize,
17}
18
19/// A code node returned from queries
20#[derive(Debug, Clone)]
21pub struct CodeNode {
22    pub name: String,
23    pub file: PathBuf,
24    pub kind: NodeKind,
25    pub is_public: bool,
26    pub is_async: bool,
27    pub symbol_id: SymbolId,
28}
29
30/// Kind of code node
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum NodeKind {
33    Function,
34    Struct,
35    Enum,
36    Trait,
37    Impl,
38    Mod,
39    TypeAlias,
40    Const,
41    Static,
42}
43
44impl NodeKind {
45    pub fn as_str(&self) -> &'static str {
46        match self {
47            NodeKind::Function => "fn",
48            NodeKind::Struct => "struct",
49            NodeKind::Enum => "enum",
50            NodeKind::Trait => "trait",
51            NodeKind::Impl => "impl",
52            NodeKind::Mod => "mod",
53            NodeKind::TypeAlias => "type",
54            NodeKind::Const => "const",
55            NodeKind::Static => "static",
56        }
57    }
58}
59
60impl From<SymbolKind> for NodeKind {
61    fn from(kind: SymbolKind) -> Self {
62        match kind {
63            SymbolKind::Function | SymbolKind::Method => NodeKind::Function,
64            SymbolKind::Struct => NodeKind::Struct,
65            SymbolKind::Enum => NodeKind::Enum,
66            SymbolKind::Trait => NodeKind::Trait,
67            SymbolKind::Impl => NodeKind::Impl,
68            SymbolKind::Mod => NodeKind::Mod,
69            SymbolKind::TypeAlias => NodeKind::TypeAlias,
70            SymbolKind::Const => NodeKind::Const,
71            SymbolKind::Static => NodeKind::Static,
72            _ => NodeKind::Function,
73        }
74    }
75}
76
77/// Graph API error
78#[derive(Debug, thiserror::Error)]
79pub enum GraphError {
80    #[error("Failed to load project: {0}")]
81    LoadFailed(String),
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87    use ryo_analysis::SymbolKind;
88
89    #[test]
90    fn test_node_kind_as_str() {
91        assert_eq!(NodeKind::Function.as_str(), "fn");
92        assert_eq!(NodeKind::Struct.as_str(), "struct");
93        assert_eq!(NodeKind::Enum.as_str(), "enum");
94        assert_eq!(NodeKind::Trait.as_str(), "trait");
95        assert_eq!(NodeKind::Impl.as_str(), "impl");
96        assert_eq!(NodeKind::Mod.as_str(), "mod");
97        assert_eq!(NodeKind::TypeAlias.as_str(), "type");
98        assert_eq!(NodeKind::Const.as_str(), "const");
99        assert_eq!(NodeKind::Static.as_str(), "static");
100    }
101
102    #[test]
103    fn test_node_kind_from_symbol_kind() {
104        assert_eq!(NodeKind::from(SymbolKind::Function), NodeKind::Function);
105        assert_eq!(NodeKind::from(SymbolKind::Struct), NodeKind::Struct);
106        assert_eq!(NodeKind::from(SymbolKind::Enum), NodeKind::Enum);
107        assert_eq!(NodeKind::from(SymbolKind::Trait), NodeKind::Trait);
108        assert_eq!(NodeKind::from(SymbolKind::Impl), NodeKind::Impl);
109        assert_eq!(NodeKind::from(SymbolKind::Mod), NodeKind::Mod);
110    }
111
112    #[test]
113    fn test_graph_error_display() {
114        let err = GraphError::LoadFailed("file not found".to_string());
115        assert!(err.to_string().contains("Failed to load project"));
116        assert!(err.to_string().contains("file not found"));
117    }
118}