Skip to main content

codetether_agent/telemetry/tools/file_change/
file_change_ctors.rs

1//! Constructors for [`super::FileChange`]. Split out to keep `file_change.rs`
2//! under the 50-line per-file limit.
3
4use chrono::Utc;
5
6use super::FileChange;
7
8impl FileChange {
9    /// Record a read. `line_range` is optional but recommended when only a
10    /// slice of the file was read.
11    pub fn read(path: &str, line_range: Option<(u32, u32)>) -> Self {
12        Self {
13            path: path.to_string(),
14            operation: "read".to_string(),
15            timestamp: Utc::now(),
16            size_bytes: None,
17            line_range,
18            diff: None,
19        }
20    }
21
22    /// Record file creation. `content` is used only to compute `size_bytes`.
23    pub fn create(path: &str, content: &str) -> Self {
24        Self {
25            path: path.to_string(),
26            operation: "create".to_string(),
27            timestamp: Utc::now(),
28            size_bytes: Some(content.len() as u64),
29            line_range: None,
30            diff: None,
31        }
32    }
33
34    /// Record a modify with a coarse byte-count diff string.
35    pub fn modify(
36        path: &str,
37        old_content: &str,
38        new_content: &str,
39        line_range: Option<(u32, u32)>,
40    ) -> Self {
41        Self {
42            path: path.to_string(),
43            operation: "modify".to_string(),
44            timestamp: Utc::now(),
45            size_bytes: Some(new_content.len() as u64),
46            line_range,
47            diff: Some(format!(
48                "-{} bytes +{} bytes",
49                old_content.len(),
50                new_content.len()
51            )),
52        }
53    }
54
55    /// Record a modify with a caller-provided unified diff.
56    pub fn modify_with_diff(
57        path: &str,
58        diff: &str,
59        new_size: usize,
60        line_range: Option<(u32, u32)>,
61    ) -> Self {
62        Self {
63            path: path.to_string(),
64            operation: "modify".to_string(),
65            timestamp: Utc::now(),
66            size_bytes: Some(new_size as u64),
67            line_range,
68            diff: Some(diff.to_string()),
69        }
70    }
71}