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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! Result types for upgrade application operations.
//!
//! **What**: Provides types that represent the outcome of applying dependency upgrades,
//! including details of what was changed, files modified, and summary statistics.
//!
//! **How**: The `UpgradeResult` struct contains all information about an upgrade operation,
//! including individual upgrade details (`AppliedUpgrade`), file paths, backup information,
//! and aggregated statistics (`ApplySummary`). Supports both dry-run and actual operations.
//!
//! **Why**: To provide comprehensive feedback about upgrade operations, enabling users to
//! understand exactly what changed, track modifications for rollback, and review statistics
//! for reporting and audit purposes.

use crate::types::DependencyType;
use crate::upgrade::registry::UpgradeType;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Result of applying dependency upgrades.
///
/// Contains comprehensive information about what was changed during an upgrade
/// operation, including individual upgrade details, modified files, backup location,
/// and summary statistics. Supports both dry-run and actual operations.
///
/// # Examples
///
/// ```rust,ignore
/// use sublime_pkg_tools::upgrade::{UpgradeManager, UpgradeSelection};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let manager: UpgradeManager = todo!();
/// // Apply upgrades and inspect result
/// let selection = UpgradeSelection::patch_only();
/// let result = manager.apply_upgrades(selection, false).await?;
///
/// println!("Modified {} packages", result.summary.packages_modified);
/// println!("Upgraded {} dependencies", result.summary.dependencies_upgraded);
/// println!("Applied {} patch upgrades", result.summary.patch_upgrades);
///
/// if let Some(backup) = result.backup_path {
///     println!("Backup saved to: {}", backup.display());
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpgradeResult {
    /// Whether this was a dry-run operation.
    ///
    /// When `true`, no files were actually modified. The result represents
    /// what would have been changed.
    pub dry_run: bool,

    /// List of all applied upgrades.
    ///
    /// Contains detailed information about each dependency that was upgraded,
    /// including the package it belongs to, old and new versions, and upgrade type.
    pub applied: Vec<AppliedUpgrade>,

    /// Paths to all modified package.json files.
    ///
    /// Lists the absolute paths to all package.json files that were modified
    /// (or would be modified in dry-run mode). Empty for dry-run operations.
    pub modified_files: Vec<PathBuf>,

    /// Location of the backup directory.
    ///
    /// When upgrades are applied (not dry-run), all modified package.json files
    /// are backed up to this directory before modification. `None` for dry-run
    /// operations or when backup is disabled.
    pub backup_path: Option<PathBuf>,

    /// ID of automatically created changeset.
    ///
    /// When automatic changeset creation is enabled, contains the ID of the
    /// created changeset. `None` for dry-run operations or when automatic
    /// changeset creation is disabled.
    pub changeset_id: Option<String>,

    /// Summary statistics of the operation.
    ///
    /// Aggregated counts and metrics about the upgrade operation.
    pub summary: ApplySummary,
}

impl UpgradeResult {
    /// Creates a new dry-run result.
    ///
    /// # Arguments
    ///
    /// * `applied` - List of upgrades that would be applied
    /// * `summary` - Summary statistics
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::{UpgradeResult, ApplySummary};
    ///
    /// let summary = ApplySummary::new();
    /// let result = UpgradeResult::dry_run(vec![], summary);
    /// assert!(result.dry_run);
    /// assert!(result.modified_files.is_empty());
    /// assert!(result.backup_path.is_none());
    /// ```
    #[must_use]
    pub fn dry_run(applied: Vec<AppliedUpgrade>, summary: ApplySummary) -> Self {
        Self {
            dry_run: true,
            applied,
            modified_files: Vec::new(),
            backup_path: None,
            changeset_id: None,
            summary,
        }
    }

    /// Creates a new result for an actual operation.
    ///
    /// # Arguments
    ///
    /// * `applied` - List of applied upgrades
    /// * `modified_files` - Paths to modified package.json files
    /// * `backup_path` - Optional backup directory path
    /// * `changeset_id` - Optional automatically created changeset ID
    /// * `summary` - Summary statistics
    #[must_use]
    pub fn applied(
        applied: Vec<AppliedUpgrade>,
        modified_files: Vec<PathBuf>,
        backup_path: Option<PathBuf>,
        changeset_id: Option<String>,
        summary: ApplySummary,
    ) -> Self {
        Self { dry_run: false, applied, modified_files, backup_path, changeset_id, summary }
    }

    /// Returns whether any upgrades were applied.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::{UpgradeResult, ApplySummary};
    ///
    /// let result = UpgradeResult::dry_run(vec![], ApplySummary::new());
    /// assert!(!result.has_changes());
    /// ```
    #[must_use]
    pub fn has_changes(&self) -> bool {
        !self.applied.is_empty()
    }

    /// Returns the number of packages that were modified.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::{UpgradeResult, ApplySummary};
    ///
    /// let result = UpgradeResult::dry_run(vec![], ApplySummary::new());
    /// assert_eq!(result.packages_modified(), 0);
    /// ```
    #[must_use]
    pub fn packages_modified(&self) -> usize {
        self.summary.packages_modified
    }

    /// Returns the total number of dependencies that were upgraded.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::{UpgradeResult, ApplySummary};
    ///
    /// let result = UpgradeResult::dry_run(vec![], ApplySummary::new());
    /// assert_eq!(result.dependencies_upgraded(), 0);
    /// ```
    #[must_use]
    pub fn dependencies_upgraded(&self) -> usize {
        self.summary.dependencies_upgraded
    }
}

/// Details of a single applied upgrade.
///
/// Represents a single dependency upgrade within a package, including
/// the package location, dependency information, version change, and
/// upgrade classification.
///
/// # Examples
///
/// ```rust
/// use sublime_pkg_tools::upgrade::{AppliedUpgrade, UpgradeType};
/// use sublime_pkg_tools::types::DependencyType;
/// use std::path::PathBuf;
///
/// let upgrade = AppliedUpgrade {
///     package_path: PathBuf::from("packages/my-package"),
///     dependency_name: "lodash".to_string(),
///     dependency_type: DependencyType::Regular,
///     old_version: "4.17.20".to_string(),
///     new_version: "4.17.21".to_string(),
///     upgrade_type: UpgradeType::Patch,
/// };
///
/// assert!(upgrade.is_patch());
/// assert!(!upgrade.is_breaking());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppliedUpgrade {
    /// Absolute path to the package directory.
    ///
    /// Points to the directory containing the package.json file where
    /// this upgrade was applied.
    pub package_path: PathBuf,

    /// Name of the upgraded dependency.
    ///
    /// The package name as it appears in the dependencies section
    /// (e.g., "lodash", "@types/node", "@myorg/utils").
    pub dependency_name: String,

    /// Type of dependency that was upgraded.
    ///
    /// Indicates which section of package.json this dependency belongs to:
    /// dependencies, devDependencies, peerDependencies, or optionalDependencies.
    pub dependency_type: DependencyType,

    /// Previous version specification.
    ///
    /// The version spec as it appeared in package.json before the upgrade
    /// (e.g., "^4.17.20", "~2.0.0", "1.2.3").
    pub old_version: String,

    /// New version specification.
    ///
    /// The updated version spec written to package.json
    /// (e.g., "^4.17.21", "~2.1.0", "1.2.4").
    pub new_version: String,

    /// Classification of the upgrade.
    ///
    /// Indicates whether this is a major, minor, or patch upgrade based
    /// on semantic versioning.
    pub upgrade_type: UpgradeType,
}

impl AppliedUpgrade {
    /// Returns whether this is a patch upgrade.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::{AppliedUpgrade, UpgradeType};
    /// use sublime_pkg_tools::types::DependencyType;
    /// use std::path::PathBuf;
    ///
    /// let upgrade = AppliedUpgrade {
    ///     package_path: PathBuf::from("."),
    ///     dependency_name: "lodash".to_string(),
    ///     dependency_type: DependencyType::Regular,
    ///     old_version: "4.17.20".to_string(),
    ///     new_version: "4.17.21".to_string(),
    ///     upgrade_type: UpgradeType::Patch,
    /// };
    ///
    /// assert!(upgrade.is_patch());
    /// ```
    #[must_use]
    pub fn is_patch(&self) -> bool {
        self.upgrade_type == UpgradeType::Patch
    }

    /// Returns whether this is a minor upgrade.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::{AppliedUpgrade, UpgradeType};
    /// use sublime_pkg_tools::types::DependencyType;
    /// use std::path::PathBuf;
    ///
    /// let upgrade = AppliedUpgrade {
    ///     package_path: PathBuf::from("."),
    ///     dependency_name: "react".to_string(),
    ///     dependency_type: DependencyType::Regular,
    ///     old_version: "17.0.0".to_string(),
    ///     new_version: "17.1.0".to_string(),
    ///     upgrade_type: UpgradeType::Minor,
    /// };
    ///
    /// assert!(upgrade.is_minor());
    /// ```
    #[must_use]
    pub fn is_minor(&self) -> bool {
        self.upgrade_type == UpgradeType::Minor
    }

    /// Returns whether this is a major upgrade.
    ///
    /// Major upgrades may contain breaking changes.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::{AppliedUpgrade, UpgradeType};
    /// use sublime_pkg_tools::types::DependencyType;
    /// use std::path::PathBuf;
    ///
    /// let upgrade = AppliedUpgrade {
    ///     package_path: PathBuf::from("."),
    ///     dependency_name: "webpack".to_string(),
    ///     dependency_type: DependencyType::Dev,
    ///     old_version: "4.46.0".to_string(),
    ///     new_version: "5.0.0".to_string(),
    ///     upgrade_type: UpgradeType::Major,
    /// };
    ///
    /// assert!(upgrade.is_major());
    /// assert!(upgrade.is_breaking());
    /// ```
    #[must_use]
    pub fn is_major(&self) -> bool {
        self.upgrade_type == UpgradeType::Major
    }

    /// Returns whether this upgrade may contain breaking changes.
    ///
    /// Currently equivalent to `is_major()`, as major version bumps
    /// typically indicate breaking changes per semantic versioning.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::{AppliedUpgrade, UpgradeType};
    /// use sublime_pkg_tools::types::DependencyType;
    /// use std::path::PathBuf;
    ///
    /// let upgrade = AppliedUpgrade {
    ///     package_path: PathBuf::from("."),
    ///     dependency_name: "express".to_string(),
    ///     dependency_type: DependencyType::Regular,
    ///     old_version: "4.18.0".to_string(),
    ///     new_version: "5.0.0".to_string(),
    ///     upgrade_type: UpgradeType::Major,
    /// };
    ///
    /// assert!(upgrade.is_breaking());
    /// ```
    #[must_use]
    pub fn is_breaking(&self) -> bool {
        self.is_major()
    }

    /// Returns a display string for the version change.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::{AppliedUpgrade, UpgradeType};
    /// use sublime_pkg_tools::types::DependencyType;
    /// use std::path::PathBuf;
    ///
    /// let upgrade = AppliedUpgrade {
    ///     package_path: PathBuf::from("."),
    ///     dependency_name: "lodash".to_string(),
    ///     dependency_type: DependencyType::Regular,
    ///     old_version: "4.17.20".to_string(),
    ///     new_version: "4.17.21".to_string(),
    ///     upgrade_type: UpgradeType::Patch,
    /// };
    ///
    /// assert_eq!(upgrade.version_change(), "4.17.20 → 4.17.21");
    /// ```
    #[must_use]
    pub fn version_change(&self) -> String {
        format!("{}{}", self.old_version, self.new_version)
    }
}

/// Summary statistics of an upgrade operation.
///
/// Provides aggregated counts and metrics about what was upgraded,
/// broken down by package count, dependency count, and upgrade type.
///
/// # Examples
///
/// ```rust
/// use sublime_pkg_tools::upgrade::ApplySummary;
///
/// let summary = ApplySummary {
///     packages_modified: 3,
///     dependencies_upgraded: 5,
///     direct_updates: 4,
///     propagated_updates: 1,
///     dependency_updates: 5,
///     major_upgrades: 1,
///     minor_upgrades: 2,
///     patch_upgrades: 2,
///     applied_at: chrono::Utc::now(),
/// };
///
/// assert_eq!(summary.total_upgrades(), 5);
/// assert!(summary.has_major_upgrades());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApplySummary {
    /// Total number of packages that were modified.
    ///
    /// Counts unique package.json files that had at least one dependency upgraded.
    pub packages_modified: usize,

    /// Total number of dependencies that were upgraded.
    ///
    /// Counts individual dependency upgrades across all packages.
    pub dependencies_upgraded: usize,

    /// Number of direct updates.
    ///
    /// Updates that were explicitly selected for upgrade (not propagated).
    /// Note: This field is for future use with dependency propagation features.
    pub direct_updates: usize,

    /// Number of propagated updates.
    ///
    /// Updates that were applied due to dependency propagation.
    /// Note: This field is for future use with dependency propagation features.
    pub propagated_updates: usize,

    /// Total dependency updates across all sections.
    ///
    /// Includes updates to dependencies, devDependencies, peerDependencies,
    /// and optionalDependencies.
    pub dependency_updates: usize,

    /// Number of major version upgrades.
    ///
    /// Count of dependencies upgraded to a new major version (e.g., 1.x → 2.x).
    pub major_upgrades: usize,

    /// Number of minor version upgrades.
    ///
    /// Count of dependencies upgraded to a new minor version (e.g., 1.2.x → 1.3.x).
    pub minor_upgrades: usize,

    /// Number of patch version upgrades.
    ///
    /// Count of dependencies upgraded to a new patch version (e.g., 1.2.3 → 1.2.4).
    pub patch_upgrades: usize,

    /// Timestamp when the operation was performed.
    ///
    /// For dry-run operations, this is when the preview was generated.
    pub applied_at: DateTime<Utc>,
}

impl ApplySummary {
    /// Creates a new empty summary with the current timestamp.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::ApplySummary;
    ///
    /// let summary = ApplySummary::new();
    /// assert_eq!(summary.packages_modified, 0);
    /// assert_eq!(summary.dependencies_upgraded, 0);
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            packages_modified: 0,
            dependencies_upgraded: 0,
            direct_updates: 0,
            propagated_updates: 0,
            dependency_updates: 0,
            major_upgrades: 0,
            minor_upgrades: 0,
            patch_upgrades: 0,
            applied_at: Utc::now(),
        }
    }

    /// Returns the total number of upgrades performed.
    ///
    /// Sum of major, minor, and patch upgrades.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::ApplySummary;
    ///
    /// let mut summary = ApplySummary::new();
    /// summary.major_upgrades = 1;
    /// summary.minor_upgrades = 2;
    /// summary.patch_upgrades = 3;
    ///
    /// assert_eq!(summary.total_upgrades(), 6);
    /// ```
    #[must_use]
    pub fn total_upgrades(&self) -> usize {
        self.major_upgrades + self.minor_upgrades + self.patch_upgrades
    }

    /// Returns whether any major upgrades were performed.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::ApplySummary;
    ///
    /// let mut summary = ApplySummary::new();
    /// assert!(!summary.has_major_upgrades());
    ///
    /// summary.major_upgrades = 1;
    /// assert!(summary.has_major_upgrades());
    /// ```
    #[must_use]
    pub fn has_major_upgrades(&self) -> bool {
        self.major_upgrades > 0
    }

    /// Returns whether any changes were made.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sublime_pkg_tools::upgrade::ApplySummary;
    ///
    /// let summary = ApplySummary::new();
    /// assert!(!summary.has_changes());
    ///
    /// let mut summary = ApplySummary::new();
    /// summary.patch_upgrades = 1;
    /// summary.dependencies_upgraded = 1;
    /// assert!(summary.has_changes());
    /// ```
    #[must_use]
    pub fn has_changes(&self) -> bool {
        self.dependencies_upgraded > 0
    }
}

impl Default for ApplySummary {
    fn default() -> Self {
        Self::new()
    }
}