sublime_pkg_tools 0.0.27

Package and version management toolkit for Node.js projects with changeset support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! File change data structures for tracking individual file modifications.
//!
//! **What**: Provides types for representing individual file changes, including the type
//! of change (added, modified, deleted, renamed, copied), file paths, and associated statistics.
//!
//! **How**: Defines serializable structures that map Git file status changes to domain types,
//! tracking both absolute and package-relative paths, change types, line statistics, and
//! associated commit information.
//!
//! **Why**: To provide detailed, type-safe information about file-level changes that can be
//! aggregated into package-level change reports, enabling accurate tracking of what changed
//! and supporting changelog generation and version bumping decisions.
//!
//! # Examples
//!
//! ## Creating a file change
//!
//! ```rust
//! use sublime_pkg_tools::changes::{FileChange, FileChangeType};
//! use std::path::PathBuf;
//!
//! let change = FileChange {
//!     path: PathBuf::from("packages/core/src/index.ts"),
//!     package_relative_path: PathBuf::from("src/index.ts"),
//!     change_type: FileChangeType::Modified,
//!     lines_added: Some(15),
//!     lines_deleted: Some(3),
//!     commits: vec!["abc123".to_string()],
//! };
//!
//! assert_eq!(change.change_type, FileChangeType::Modified);
//! assert_eq!(change.lines_added, Some(15));
//! ```
//!
//! ## Filtering by change type
//!
//! ```rust
//! use sublime_pkg_tools::changes::{FileChange, FileChangeType};
//! use std::path::PathBuf;
//!
//! let changes = vec![
//!     FileChange {
//!         path: PathBuf::from("file1.ts"),
//!         package_relative_path: PathBuf::from("file1.ts"),
//!         change_type: FileChangeType::Added,
//!         lines_added: Some(100),
//!         lines_deleted: None,
//!         commits: vec![],
//!     },
//!     FileChange {
//!         path: PathBuf::from("file2.ts"),
//!         package_relative_path: PathBuf::from("file2.ts"),
//!         change_type: FileChangeType::Modified,
//!         lines_added: Some(10),
//!         lines_deleted: Some(5),
//!         commits: vec![],
//!     },
//! ];
//!
//! let added: Vec<_> = changes.iter()
//!     .filter(|c| c.change_type == FileChangeType::Added)
//!     .collect();
//! assert_eq!(added.len(), 1);
//! ```

use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use sublime_git_tools::GitFileStatus;

/// Type of change applied to a file.
///
/// Maps directly to Git status indicators and represents the operation
/// performed on a file in the repository.
///
/// # Examples
///
/// ```rust
/// use sublime_pkg_tools::changes::FileChangeType;
///
/// let change_type = FileChangeType::Modified;
/// assert!(change_type.is_modification());
/// assert!(!change_type.is_addition());
///
/// let added = FileChangeType::Added;
/// assert!(added.is_addition());
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FileChangeType {
    /// File was newly added to the repository.
    Added,
    /// File was modified (content changed).
    Modified,
    /// File was deleted from the repository.
    Deleted,
    /// File was renamed or moved.
    Renamed,
    /// File was copied to a new location.
    Copied,
    /// File is untracked (not yet added to Git).
    Untracked,
}

impl FileChangeType {
    /// Returns whether this change type represents an addition.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::FileChangeType;
    ///
    /// assert!(FileChangeType::Added.is_addition());
    /// assert!(FileChangeType::Untracked.is_addition());
    /// assert!(!FileChangeType::Modified.is_addition());
    /// ```
    #[must_use]
    pub fn is_addition(&self) -> bool {
        matches!(self, Self::Added | Self::Untracked | Self::Copied)
    }

    /// Returns whether this change type represents a modification.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::FileChangeType;
    ///
    /// assert!(FileChangeType::Modified.is_modification());
    /// assert!(FileChangeType::Renamed.is_modification());
    /// assert!(!FileChangeType::Added.is_modification());
    /// ```
    #[must_use]
    pub fn is_modification(&self) -> bool {
        matches!(self, Self::Modified | Self::Renamed)
    }

    /// Returns whether this change type represents a deletion.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::FileChangeType;
    ///
    /// assert!(FileChangeType::Deleted.is_deletion());
    /// assert!(!FileChangeType::Modified.is_deletion());
    /// ```
    #[must_use]
    pub fn is_deletion(&self) -> bool {
        matches!(self, Self::Deleted)
    }

    /// Converts from `sublime_git_tools::GitFileStatus` to `FileChangeType`.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use sublime_pkg_tools::changes::FileChangeType;
    /// use sublime_git_tools::GitFileStatus;
    ///
    /// let change_type = FileChangeType::from_git_status(&GitFileStatus::Modified);
    /// assert_eq!(change_type, FileChangeType::Modified);
    /// ```
    #[must_use]
    pub fn from_git_status(status: &GitFileStatus) -> Self {
        match status {
            GitFileStatus::Added => Self::Added,
            GitFileStatus::Modified => Self::Modified,
            GitFileStatus::Deleted => Self::Deleted,
            GitFileStatus::Untracked => Self::Untracked,
        }
    }
}

/// Details of a single file change.
///
/// Contains comprehensive information about what happened to a file, including
/// paths (both absolute and package-relative), the type of change, line statistics,
/// and associated commit information.
///
/// # Examples
///
/// ## Creating a file change
///
/// ```rust
/// use sublime_pkg_tools::changes::{FileChange, FileChangeType};
/// use std::path::PathBuf;
///
/// let change = FileChange {
///     path: PathBuf::from("packages/core/src/api.ts"),
///     package_relative_path: PathBuf::from("src/api.ts"),
///     change_type: FileChangeType::Added,
///     lines_added: Some(50),
///     lines_deleted: None,
///     commits: vec![],
/// };
///
/// assert!(change.is_addition());
/// assert_eq!(change.net_line_change(), Some(50));
/// ```
///
/// ## Checking for package.json changes
///
/// ```rust
/// use sublime_pkg_tools::changes::{FileChange, FileChangeType};
/// use std::path::PathBuf;
///
/// let change = FileChange {
///     path: PathBuf::from("packages/core/package.json"),
///     package_relative_path: PathBuf::from("package.json"),
///     change_type: FileChangeType::Modified,
///     lines_added: Some(2),
///     lines_deleted: Some(1),
///     commits: vec![],
/// };
///
/// assert!(change.is_package_json());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FileChange {
    /// File path relative to workspace root.
    ///
    /// This is the full path from the workspace root to the file.
    /// Example: `packages/core/src/index.ts`
    pub path: PathBuf,

    /// File path relative to package root.
    ///
    /// This is the path from the package's root directory to the file.
    /// Example: `src/index.ts` (when the package is at `packages/core`)
    pub package_relative_path: PathBuf,

    /// Type of change applied to the file.
    pub change_type: FileChangeType,

    /// Number of lines added (if available).
    ///
    /// This information may not be available for certain operations like
    /// working directory analysis without computing diffs. It will be `None`
    /// for deleted files.
    pub lines_added: Option<usize>,

    /// Number of lines deleted (if available).
    ///
    /// This information may not be available for certain operations like
    /// working directory analysis without computing diffs. It will be `None`
    /// for newly added files.
    pub lines_deleted: Option<usize>,

    /// Commit hashes that modified this file.
    ///
    /// For working directory analysis, this will be empty. For commit range
    /// analysis, this contains all commits in the range that modified this file.
    pub commits: Vec<String>,
}

impl FileChange {
    /// Creates a new `FileChange` with the specified parameters.
    ///
    /// # Arguments
    ///
    /// * `path` - Full path from workspace root
    /// * `package_relative_path` - Path relative to package root
    /// * `change_type` - Type of change
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::{FileChange, FileChangeType};
    /// use std::path::PathBuf;
    ///
    /// let change = FileChange::new(
    ///     PathBuf::from("packages/core/src/index.ts"),
    ///     PathBuf::from("src/index.ts"),
    ///     FileChangeType::Modified,
    /// );
    ///
    /// assert_eq!(change.change_type, FileChangeType::Modified);
    /// assert!(change.commits.is_empty());
    /// ```
    #[must_use]
    pub fn new(path: PathBuf, package_relative_path: PathBuf, change_type: FileChangeType) -> Self {
        Self {
            path,
            package_relative_path,
            change_type,
            lines_added: None,
            lines_deleted: None,
            commits: Vec::new(),
        }
    }

    /// Returns whether this change represents an addition.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::{FileChange, FileChangeType};
    /// use std::path::PathBuf;
    ///
    /// let change = FileChange::new(
    ///     PathBuf::from("file.ts"),
    ///     PathBuf::from("file.ts"),
    ///     FileChangeType::Added,
    /// );
    ///
    /// assert!(change.is_addition());
    /// ```
    #[must_use]
    pub fn is_addition(&self) -> bool {
        self.change_type.is_addition()
    }

    /// Returns whether this change represents a modification.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::{FileChange, FileChangeType};
    /// use std::path::PathBuf;
    ///
    /// let change = FileChange::new(
    ///     PathBuf::from("file.ts"),
    ///     PathBuf::from("file.ts"),
    ///     FileChangeType::Modified,
    /// );
    ///
    /// assert!(change.is_modification());
    /// ```
    #[must_use]
    pub fn is_modification(&self) -> bool {
        self.change_type.is_modification()
    }

    /// Returns whether this change represents a deletion.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::{FileChange, FileChangeType};
    /// use std::path::PathBuf;
    ///
    /// let change = FileChange::new(
    ///     PathBuf::from("file.ts"),
    ///     PathBuf::from("file.ts"),
    ///     FileChangeType::Deleted,
    /// );
    ///
    /// assert!(change.is_deletion());
    /// ```
    #[must_use]
    pub fn is_deletion(&self) -> bool {
        self.change_type.is_deletion()
    }

    /// Returns whether this file is a package.json file.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::{FileChange, FileChangeType};
    /// use std::path::PathBuf;
    ///
    /// let change = FileChange::new(
    ///     PathBuf::from("packages/core/package.json"),
    ///     PathBuf::from("package.json"),
    ///     FileChangeType::Modified,
    /// );
    ///
    /// assert!(change.is_package_json());
    /// ```
    #[must_use]
    pub fn is_package_json(&self) -> bool {
        self.package_relative_path.file_name().and_then(|name| name.to_str())
            == Some("package.json")
    }

    /// Calculates the net line change (added - deleted).
    ///
    /// Returns `None` if line statistics are not available.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::{FileChange, FileChangeType};
    /// use std::path::PathBuf;
    ///
    /// let mut change = FileChange::new(
    ///     PathBuf::from("file.ts"),
    ///     PathBuf::from("file.ts"),
    ///     FileChangeType::Modified,
    /// );
    /// change.lines_added = Some(20);
    /// change.lines_deleted = Some(5);
    ///
    /// assert_eq!(change.net_line_change(), Some(15));
    /// ```
    #[must_use]
    pub fn net_line_change(&self) -> Option<i64> {
        match (self.lines_added, self.lines_deleted) {
            (Some(added), Some(deleted)) => Some(added as i64 - deleted as i64),
            (Some(added), None) => Some(added as i64),
            (None, Some(deleted)) => Some(-(deleted as i64)),
            (None, None) => None,
        }
    }

    /// Returns the total number of line changes (added + deleted).
    ///
    /// Returns `None` if line statistics are not available.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::{FileChange, FileChangeType};
    /// use std::path::PathBuf;
    ///
    /// let mut change = FileChange::new(
    ///     PathBuf::from("file.ts"),
    ///     PathBuf::from("file.ts"),
    ///     FileChangeType::Modified,
    /// );
    /// change.lines_added = Some(20);
    /// change.lines_deleted = Some(5);
    ///
    /// assert_eq!(change.total_line_changes(), Some(25));
    /// ```
    #[must_use]
    pub fn total_line_changes(&self) -> Option<usize> {
        match (self.lines_added, self.lines_deleted) {
            (Some(added), Some(deleted)) => Some(added + deleted),
            (Some(added), None) => Some(added),
            (None, Some(deleted)) => Some(deleted),
            (None, None) => None,
        }
    }

    /// Returns the file extension if present.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::{FileChange, FileChangeType};
    /// use std::path::PathBuf;
    ///
    /// let change = FileChange::new(
    ///     PathBuf::from("packages/core/src/index.ts"),
    ///     PathBuf::from("src/index.ts"),
    ///     FileChangeType::Modified,
    /// );
    ///
    /// assert_eq!(change.extension(), Some("ts"));
    /// ```
    #[must_use]
    pub fn extension(&self) -> Option<&str> {
        self.path.extension().and_then(|ext| ext.to_str())
    }

    /// Returns the parent directory of the file relative to the package root.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::changes::{FileChange, FileChangeType};
    /// use std::path::{Path, PathBuf};
    ///
    /// let change = FileChange::new(
    ///     PathBuf::from("packages/core/src/api/index.ts"),
    ///     PathBuf::from("src/api/index.ts"),
    ///     FileChangeType::Modified,
    /// );
    ///
    /// assert_eq!(change.package_relative_dir(), Some(Path::new("src/api")));
    /// ```
    #[must_use]
    pub fn package_relative_dir(&self) -> Option<&std::path::Path> {
        self.package_relative_path.parent()
    }
}