ricecoder_files/models.rs
1//! Data models for file management operations
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6use uuid::Uuid;
7
8/// Represents a single file operation
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct FileOperation {
11 /// Path to the file being operated on
12 pub path: PathBuf,
13 /// Type of operation (create, update, delete, rename)
14 pub operation: OperationType,
15 /// Content of the file (if applicable)
16 pub content: Option<String>,
17 /// Path to the backup file (if created)
18 pub backup_path: Option<PathBuf>,
19 /// SHA-256 hash of the content for verification
20 pub content_hash: Option<String>,
21}
22
23/// Types of file operations
24#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
25pub enum OperationType {
26 /// Create a new file
27 Create,
28 /// Update an existing file
29 Update,
30 /// Delete a file
31 Delete,
32 /// Rename a file
33 Rename {
34 /// New path for the file
35 to: PathBuf,
36 },
37}
38
39/// Information about a detected conflict
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct ConflictInfo {
42 /// Path where the conflict was detected
43 pub path: PathBuf,
44 /// Content of the existing file
45 pub existing_content: String,
46 /// Content of the new file
47 pub new_content: String,
48}
49
50/// Strategy for resolving conflicts
51#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
52pub enum ConflictResolution {
53 /// Skip the write operation, leave existing file unchanged
54 Skip,
55 /// Overwrite the existing file with new content
56 Overwrite,
57 /// Merge the changes (combine both versions)
58 Merge,
59}
60
61/// Represents a transaction containing multiple file operations
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct FileTransaction {
64 /// Unique identifier for the transaction
65 pub id: Uuid,
66 /// List of operations in the transaction
67 pub operations: Vec<FileOperation>,
68 /// Current status of the transaction
69 pub status: TransactionStatus,
70 /// When the transaction was created
71 pub created_at: DateTime<Utc>,
72 /// When the transaction was completed (if applicable)
73 pub completed_at: Option<DateTime<Utc>>,
74}
75
76/// Status of a transaction
77#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
78pub enum TransactionStatus {
79 /// Transaction is pending execution
80 Pending,
81 /// Transaction has been committed
82 Committed,
83 /// Transaction has been rolled back
84 RolledBack,
85}
86
87/// Entry in the audit trail
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct AuditEntry {
90 /// When the operation occurred
91 pub timestamp: DateTime<Utc>,
92 /// Path of the file affected
93 pub path: PathBuf,
94 /// Type of operation
95 pub operation_type: OperationType,
96 /// SHA-256 hash of the content
97 pub content_hash: String,
98 /// Transaction ID if part of a transaction
99 pub transaction_id: Option<Uuid>,
100}
101
102/// Metadata about a backup
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct BackupMetadata {
105 /// Original path of the file
106 pub original_path: PathBuf,
107 /// Path where the backup is stored
108 pub backup_path: PathBuf,
109 /// When the backup was created
110 pub timestamp: DateTime<Utc>,
111 /// SHA-256 hash of the backup content for integrity verification
112 pub content_hash: String,
113}
114
115/// Represents a diff between two file versions
116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct FileDiff {
118 /// Path of the file
119 pub path: PathBuf,
120 /// List of hunks (sections of changes)
121 pub hunks: Vec<DiffHunk>,
122 /// Statistics about the diff
123 pub stats: DiffStats,
124}
125
126/// A hunk is a section of changes in a diff
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct DiffHunk {
129 /// Starting line number in the old file
130 pub old_start: usize,
131 /// Number of lines in the old file
132 pub old_count: usize,
133 /// Starting line number in the new file
134 pub new_start: usize,
135 /// Number of lines in the new file
136 pub new_count: usize,
137 /// Lines in this hunk
138 pub lines: Vec<DiffLine>,
139}
140
141/// A single line in a diff
142#[derive(Debug, Clone, Serialize, Deserialize)]
143pub enum DiffLine {
144 /// Context line (unchanged)
145 Context(String),
146 /// Added line
147 Added(String),
148 /// Removed line
149 Removed(String),
150}
151
152/// Statistics about a diff
153#[derive(Debug, Clone, Serialize, Deserialize)]
154pub struct DiffStats {
155 /// Number of lines added
156 pub additions: usize,
157 /// Number of lines deleted
158 pub deletions: usize,
159 /// Number of files changed
160 pub files_changed: usize,
161}
162
163/// Git repository status
164#[derive(Debug, Clone, Serialize, Deserialize)]
165pub struct GitStatus {
166 /// Current branch name
167 pub branch: String,
168 /// Modified files
169 pub modified: Vec<PathBuf>,
170 /// Staged files
171 pub staged: Vec<PathBuf>,
172 /// Untracked files
173 pub untracked: Vec<PathBuf>,
174}