Skip to main content

splice/proof/
data_structures.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use std::path::PathBuf;
4
5/// A complete refactoring proof.
6///
7/// Contains before/after snapshots of the code graph, invariant checks,
8/// and metadata for audit trails.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct RefactoringProof {
11    /// Proof metadata (operation, timestamp, etc.)
12    pub metadata: ProofMetadata,
13    /// Graph state before the refactoring
14    pub before: GraphSnapshot,
15    /// Graph state after the refactoring
16    pub after: GraphSnapshot,
17    /// Invariant validation results
18    pub invariants: Vec<InvariantCheck>,
19    /// Checksums for audit trail (added in 31-04)
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub checksums: Option<ProofChecksums>,
22}
23
24/// Metadata about the proof.
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct ProofMetadata {
27    /// Operation type (e.g., "rename", "delete", "extract")
28    pub operation: String,
29    /// User who performed the operation (if available)
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub user: Option<String>,
32    /// When the operation was performed
33    pub timestamp: i64,
34    /// Git commit hash (if available)
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub git_commit: Option<String>,
37    /// Splice version
38    pub splice_version: String,
39    /// Database path used for the operation
40    pub database_path: PathBuf,
41}
42
43/// A snapshot of the code graph at a point in time.
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct GraphSnapshot {
46    /// Snapshot timestamp
47    pub timestamp: i64,
48    /// All symbols in the graph
49    pub symbols: HashMap<String, SymbolInfo>,
50    /// All edges (caller -> callees)
51    pub edges: HashMap<String, Vec<String>>,
52    /// Entry points (roots of the call graph)
53    pub entry_points: Vec<String>,
54    /// Statistics about the snapshot
55    pub stats: GraphStats,
56}
57
58/// Information about a symbol.
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct SymbolInfo {
61    /// Unique symbol ID
62    pub id: String,
63    /// Symbol name
64    pub name: String,
65    /// File path
66    pub file_path: String,
67    /// Symbol kind (function, struct, etc.)
68    pub kind: String,
69    /// Byte span
70    pub byte_span: (usize, usize),
71    /// Number of incoming references (fan-in)
72    pub fan_in: usize,
73    /// Number of outgoing references (fan-out)
74    pub fan_out: usize,
75}
76
77/// Statistics about a graph snapshot.
78#[derive(Debug, Clone, Serialize, Deserialize)]
79pub struct GraphStats {
80    /// Total number of symbols
81    pub total_symbols: usize,
82    /// Total number of edges
83    pub total_edges: usize,
84    /// Number of entry points
85    pub entry_point_count: usize,
86    /// Maximum cyclomatic complexity (if available)
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub max_complexity: Option<usize>,
89}
90
91/// Result of an invariant check.
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct InvariantCheck {
94    /// Name of the invariant being checked
95    pub invariant_name: String,
96    /// Whether the invariant passed
97    pub passed: bool,
98    /// Violations found (empty if passed)
99    pub violations: Vec<InvariantViolation>,
100}
101
102/// An invariant violation.
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct InvariantViolation {
105    /// Severity of the violation
106    pub severity: ViolationSeverity,
107    /// Symbol or edge where violation occurred
108    pub subject: String,
109    /// Description of the violation
110    pub message: String,
111    /// Suggested fix (if applicable)
112    #[serde(skip_serializing_if = "Option::is_none")]
113    pub suggestion: Option<String>,
114}
115
116/// Severity of an invariant violation.
117#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
118pub enum ViolationSeverity {
119    /// Informational (no impact on correctness)
120    Info,
121    /// Warning (may indicate a problem)
122    Warning,
123    /// Error (definitely breaks invariants)
124    Error,
125    /// Critical (makes the codebase unusable)
126    Critical,
127}
128
129/// Checksums for proof integrity (added in 31-04).
130#[derive(Debug, Clone, Serialize, Deserialize)]
131pub struct ProofChecksums {
132    /// SHA-256 hash of the before snapshot
133    pub before_hash: String,
134    /// SHA-256 hash of the after snapshot
135    pub after_hash: String,
136    /// SHA-256 hash of the combined proof
137    pub proof_hash: String,
138}