Skip to main content

context_footprint/domain/
node.rs

1/// Unique identifier for a node in the graph
2pub type NodeId = u32;
3
4/// Scope identifier (module/namespace)
5pub type ScopeId = String;
6
7/// Source code span
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct SourceSpan {
10    pub start_line: u32,
11    pub start_column: u32,
12    pub end_line: u32,
13    pub end_column: u32,
14}
15
16/// Shared core attributes for all nodes
17#[derive(Debug, Clone)]
18pub struct NodeCore {
19    pub id: NodeId,
20    pub name: String,
21    pub scope: Option<ScopeId>,
22    pub context_size: u32, // Abstract context size (computed by SizeFunction)
23    pub span: SourceSpan,
24    pub doc_score: f32, // Documentation quality score [0.0, 1.0]
25    pub is_external: bool,
26    pub file_path: String, // Path to source file (relative to project root)
27}
28
29impl NodeCore {
30    #[allow(clippy::too_many_arguments)]
31    pub fn new(
32        id: NodeId,
33        name: String,
34        scope: Option<ScopeId>,
35        context_size: u32,
36        span: SourceSpan,
37        doc_score: f32,
38        is_external: bool,
39        file_path: String,
40    ) -> Self {
41        Self {
42            id,
43            name,
44            scope,
45            context_size,
46            span,
47            doc_score,
48            is_external,
49            file_path,
50        }
51    }
52}
53
54/// Visibility level
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub enum Visibility {
57    Public,
58    Private,
59    Protected,
60    Internal,
61}
62
63/// Function node
64#[derive(Debug, Clone)]
65pub struct FunctionNode {
66    pub core: NodeCore,
67
68    // Signature completeness signals
69    pub param_count: u32,
70    pub typed_param_count: u32,
71    pub has_return_type: bool,
72
73    // Behavioral signals
74    pub is_async: bool,
75    pub is_generator: bool,
76    pub visibility: Visibility,
77}
78
79/// Mutability
80#[derive(Debug, Clone, PartialEq, Eq)]
81pub enum Mutability {
82    Const,     // Compile-time constant
83    Immutable, // Runtime immutable
84    Mutable,   // Mutable (Expansion trigger)
85}
86
87/// Variable kind
88#[derive(Debug, Clone, PartialEq, Eq)]
89pub enum VariableKind {
90    Global,     // Module-level
91    ClassField, // Class/struct field
92}
93
94/// Variable node
95#[derive(Debug, Clone)]
96pub struct VariableNode {
97    pub core: NodeCore,
98
99    // Type annotation
100    pub has_type_annotation: bool,
101
102    // Mutability (critical for Expansion)
103    pub mutability: Mutability,
104
105    // Scope kind
106    pub variable_kind: VariableKind,
107}
108
109/// Type kind
110#[derive(Debug, Clone, PartialEq, Eq)]
111pub enum TypeKind {
112    Class,
113    Interface, // Java, Go, TypeScript
114    Protocol,  // Python, Swift
115    Struct,
116    Enum,
117    TypeAlias,    // type UserId = string
118    FunctionType, // (int, int) -> bool
119    Union,        // A | B
120    Intersection, // A & B
121}
122
123/// Type node
124#[derive(Debug, Clone)]
125pub struct TypeNode {
126    pub core: NodeCore,
127
128    // Type classification
129    pub type_kind: TypeKind,
130
131    // Abstraction signal (Pruning key)
132    pub is_abstract: bool, // interface, protocol, abstract class
133
134    // Generics
135    pub type_param_count: u32, // Generic parameters (e.g., List<T> → 1)
136}
137
138/// Polymorphic node type
139#[derive(Debug, Clone)]
140pub enum Node {
141    Function(FunctionNode),
142    Variable(VariableNode),
143    Type(TypeNode),
144}
145
146impl Node {
147    pub fn core(&self) -> &NodeCore {
148        match self {
149            Node::Function(f) => &f.core,
150            Node::Variable(v) => &v.core,
151            Node::Type(t) => &t.core,
152        }
153    }
154
155    pub fn core_mut(&mut self) -> &mut NodeCore {
156        match self {
157            Node::Function(f) => &mut f.core,
158            Node::Variable(v) => &mut v.core,
159            Node::Type(t) => &mut t.core,
160        }
161    }
162}