ricecoder_hooks/events/
file_operations.rs

1//! File and directory operation event types
2//!
3//! This module defines event types for file system operations including create, modify,
4//! delete, rename, move, and read operations on both files and directories.
5//!
6//! # Examples
7//!
8//! File created event:
9//! ```ignore
10//! use ricecoder_hooks::events::FileOperationEvent;
11//! use std::path::PathBuf;
12//! use std::time::SystemTime;
13//!
14//! let event = FileOperationEvent::Created {
15//!     path: PathBuf::from("/path/to/file.rs"),
16//!     size: 1024,
17//!     timestamp: SystemTime::now(),
18//! };
19//! ```
20
21use serde::{Deserialize, Serialize};
22use std::path::PathBuf;
23use std::time::SystemTime;
24
25/// File operation event types
26///
27/// Represents different types of file system operations that can trigger hooks.
28/// Each variant includes relevant metadata for the operation.
29///
30/// # Variants
31///
32/// * `Created` - File was created
33/// * `Modified` - File was modified
34/// * `Deleted` - File was deleted
35/// * `Renamed` - File was renamed
36/// * `Moved` - File was moved to a different directory
37/// * `Read` - File was read
38#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(tag = "operation_type")]
40pub enum FileOperationEvent {
41    /// File was created
42    ///
43    /// # Fields
44    ///
45    /// * `path` - Path to the created file
46    /// * `size` - Size of the file in bytes
47    /// * `timestamp` - Time when the file was created
48    #[serde(rename = "file_created")]
49    Created {
50        /// Path to the file
51        path: PathBuf,
52
53        /// File size in bytes
54        size: u64,
55
56        /// Timestamp of the operation
57        timestamp: SystemTime,
58    },
59
60    /// File was modified
61    ///
62    /// # Fields
63    ///
64    /// * `path` - Path to the modified file
65    /// * `old_hash` - Hash of the file before modification
66    /// * `new_hash` - Hash of the file after modification
67    /// * `timestamp` - Time when the file was modified
68    #[serde(rename = "file_modified")]
69    Modified {
70        /// Path to the file
71        path: PathBuf,
72
73        /// Hash of the file before modification
74        old_hash: String,
75
76        /// Hash of the file after modification
77        new_hash: String,
78
79        /// Timestamp of the operation
80        timestamp: SystemTime,
81    },
82
83    /// File was deleted
84    ///
85    /// # Fields
86    ///
87    /// * `path` - Path to the deleted file
88    /// * `timestamp` - Time when the file was deleted
89    #[serde(rename = "file_deleted")]
90    Deleted {
91        /// Path to the file
92        path: PathBuf,
93
94        /// Timestamp of the operation
95        timestamp: SystemTime,
96    },
97
98    /// File was renamed
99    ///
100    /// # Fields
101    ///
102    /// * `old_path` - Original path of the file
103    /// * `new_path` - New path of the file
104    /// * `timestamp` - Time when the file was renamed
105    #[serde(rename = "file_renamed")]
106    Renamed {
107        /// Original path
108        old_path: PathBuf,
109
110        /// New path
111        new_path: PathBuf,
112
113        /// Timestamp of the operation
114        timestamp: SystemTime,
115    },
116
117    /// File was moved to a different directory
118    ///
119    /// # Fields
120    ///
121    /// * `old_path` - Original path of the file
122    /// * `new_path` - New path of the file
123    /// * `timestamp` - Time when the file was moved
124    #[serde(rename = "file_moved")]
125    Moved {
126        /// Original path
127        old_path: PathBuf,
128
129        /// New path
130        new_path: PathBuf,
131
132        /// Timestamp of the operation
133        timestamp: SystemTime,
134    },
135
136    /// File was read
137    ///
138    /// # Fields
139    ///
140    /// * `path` - Path to the file that was read
141    /// * `timestamp` - Time when the file was read
142    #[serde(rename = "file_read")]
143    Read {
144        /// Path to the file
145        path: PathBuf,
146
147        /// Timestamp of the operation
148        timestamp: SystemTime,
149    },
150}
151
152/// Directory operation event types
153///
154/// Represents different types of directory operations that can trigger hooks.
155/// Each variant includes relevant metadata for the operation.
156///
157/// # Variants
158///
159/// * `Created` - Directory was created
160/// * `Deleted` - Directory was deleted
161#[derive(Debug, Clone, Serialize, Deserialize)]
162#[serde(tag = "operation_type")]
163pub enum DirectoryOperationEvent {
164    /// Directory was created
165    ///
166    /// # Fields
167    ///
168    /// * `path` - Path to the created directory
169    /// * `timestamp` - Time when the directory was created
170    #[serde(rename = "directory_created")]
171    Created {
172        /// Path to the directory
173        path: PathBuf,
174
175        /// Timestamp of the operation
176        timestamp: SystemTime,
177    },
178
179    /// Directory was deleted
180    ///
181    /// # Fields
182    ///
183    /// * `path` - Path to the deleted directory
184    /// * `timestamp` - Time when the directory was deleted
185    #[serde(rename = "directory_deleted")]
186    Deleted {
187        /// Path to the directory
188        path: PathBuf,
189
190        /// Timestamp of the operation
191        timestamp: SystemTime,
192    },
193}
194
195#[cfg(test)]
196mod tests {
197    use super::*;
198
199    #[test]
200    fn test_file_created_event_serialization() {
201        let event = FileOperationEvent::Created {
202            path: PathBuf::from("/path/to/file.rs"),
203            size: 1024,
204            timestamp: SystemTime::now(),
205        };
206
207        let json = serde_json::to_string(&event).expect("Failed to serialize");
208        assert!(json.contains("file_created"));
209        assert!(json.contains("file.rs"));
210        assert!(json.contains("1024"));
211    }
212
213    #[test]
214    fn test_file_modified_event_serialization() {
215        let event = FileOperationEvent::Modified {
216            path: PathBuf::from("/path/to/file.rs"),
217            old_hash: "abc123".to_string(),
218            new_hash: "def456".to_string(),
219            timestamp: SystemTime::now(),
220        };
221
222        let json = serde_json::to_string(&event).expect("Failed to serialize");
223        assert!(json.contains("file_modified"));
224        assert!(json.contains("abc123"));
225        assert!(json.contains("def456"));
226    }
227
228    #[test]
229    fn test_file_deleted_event_serialization() {
230        let event = FileOperationEvent::Deleted {
231            path: PathBuf::from("/path/to/file.rs"),
232            timestamp: SystemTime::now(),
233        };
234
235        let json = serde_json::to_string(&event).expect("Failed to serialize");
236        assert!(json.contains("file_deleted"));
237        assert!(json.contains("file.rs"));
238    }
239
240    #[test]
241    fn test_file_renamed_event_serialization() {
242        let event = FileOperationEvent::Renamed {
243            old_path: PathBuf::from("/path/to/old.rs"),
244            new_path: PathBuf::from("/path/to/new.rs"),
245            timestamp: SystemTime::now(),
246        };
247
248        let json = serde_json::to_string(&event).expect("Failed to serialize");
249        assert!(json.contains("file_renamed"));
250        assert!(json.contains("old.rs"));
251        assert!(json.contains("new.rs"));
252    }
253
254    #[test]
255    fn test_file_moved_event_serialization() {
256        let event = FileOperationEvent::Moved {
257            old_path: PathBuf::from("/old/path/file.rs"),
258            new_path: PathBuf::from("/new/path/file.rs"),
259            timestamp: SystemTime::now(),
260        };
261
262        let json = serde_json::to_string(&event).expect("Failed to serialize");
263        assert!(json.contains("file_moved"));
264        assert!(json.contains("old/path"));
265        assert!(json.contains("new/path"));
266    }
267
268    #[test]
269    fn test_file_read_event_serialization() {
270        let event = FileOperationEvent::Read {
271            path: PathBuf::from("/path/to/file.rs"),
272            timestamp: SystemTime::now(),
273        };
274
275        let json = serde_json::to_string(&event).expect("Failed to serialize");
276        assert!(json.contains("file_read"));
277        assert!(json.contains("file.rs"));
278    }
279
280    #[test]
281    fn test_directory_created_event_serialization() {
282        let event = DirectoryOperationEvent::Created {
283            path: PathBuf::from("/path/to/dir"),
284            timestamp: SystemTime::now(),
285        };
286
287        let json = serde_json::to_string(&event).expect("Failed to serialize");
288        assert!(json.contains("directory_created"));
289        assert!(json.contains("dir"));
290    }
291
292    #[test]
293    fn test_directory_deleted_event_serialization() {
294        let event = DirectoryOperationEvent::Deleted {
295            path: PathBuf::from("/path/to/dir"),
296            timestamp: SystemTime::now(),
297        };
298
299        let json = serde_json::to_string(&event).expect("Failed to serialize");
300        assert!(json.contains("directory_deleted"));
301        assert!(json.contains("dir"));
302    }
303
304    #[test]
305    fn test_file_operation_event_clone() {
306        let event = FileOperationEvent::Created {
307            path: PathBuf::from("/path/to/file.rs"),
308            size: 1024,
309            timestamp: SystemTime::now(),
310        };
311
312        let cloned = event.clone();
313        match cloned {
314            FileOperationEvent::Created { size, .. } => {
315                assert_eq!(size, 1024);
316            }
317            _ => panic!("Expected Created variant"),
318        }
319    }
320
321    #[test]
322    fn test_directory_operation_event_clone() {
323        let event = DirectoryOperationEvent::Created {
324            path: PathBuf::from("/path/to/dir"),
325            timestamp: SystemTime::now(),
326        };
327
328        let cloned = event.clone();
329        match cloned {
330            DirectoryOperationEvent::Created { .. } => {
331                // Success
332            }
333            _ => panic!("Expected Created variant"),
334        }
335    }
336}