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
//! Audit configuration for health checks and dependency audits.
//!
//! **What**: Defines configuration for audit operations, including which audit sections
//! to run, severity thresholds, specific checks for each audit type, and health score weights.
//!
//! **How**: This module provides the `AuditConfig` structure that controls how dependency
//! audits, health checks, and issue detection are performed, along with customizable weights
//! for health score calculation.
//!
//! **Why**: To enable comprehensive project health monitoring with configurable checks,
//! severity levels, and scoring weights that can be tailored to project needs and deployment
//! environments.

use serde::{Deserialize, Serialize};
use sublime_standard_tools::config::{ConfigResult, Configurable};

/// Configuration for audit and health check operations.
///
/// This structure controls all aspects of project auditing, including which
/// sections to audit, minimum severity levels, and specific check configurations.
///
/// # Example
///
/// ```rust
/// use sublime_pkg_tools::config::AuditConfig;
///
/// let config = AuditConfig::default();
/// assert!(config.enabled);
/// assert_eq!(config.min_severity, "warning");
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct AuditConfig {
    /// Whether auditing is enabled.
    ///
    /// # Default: `true`
    pub enabled: bool,

    /// Minimum severity level for reporting issues.
    ///
    /// Valid values: "critical", "warning", "info"
    ///
    /// # Default: `"warning"`
    pub min_severity: String,

    /// Configuration for which audit sections to run.
    pub sections: AuditSectionsConfig,

    /// Configuration for upgrade audits.
    pub upgrades: UpgradeAuditConfig,

    /// Configuration for dependency audits.
    pub dependencies: DependencyAuditConfig,

    /// Configuration for breaking changes audits.
    pub breaking_changes: BreakingChangesAuditConfig,

    /// Configuration for version consistency audits.
    pub version_consistency: VersionConsistencyAuditConfig,

    /// Configuration for health score calculation weights.
    pub health_score_weights: HealthScoreWeightsConfig,
}

/// Configuration for which audit sections to execute.
///
/// Each section can be independently enabled or disabled.
///
/// # Example
///
/// ```rust
/// use sublime_pkg_tools::config::AuditSectionsConfig;
///
/// let config = AuditSectionsConfig::default();
/// assert!(config.upgrades);
/// assert!(config.dependencies);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default)]
pub struct AuditSectionsConfig {
    /// Whether to run upgrade availability audits.
    ///
    /// # Default: `true`
    pub upgrades: bool,

    /// Whether to run dependency health audits.
    ///
    /// # Default: `true`
    pub dependencies: bool,

    /// Whether to check for breaking changes.
    ///
    /// # Default: `true`
    pub breaking_changes: bool,

    /// Whether to categorize dependencies.
    ///
    /// # Default: `true`
    pub categorization: bool,

    /// Whether to check version consistency.
    ///
    /// # Default: `true`
    pub version_consistency: bool,
}

/// Configuration for upgrade audits.
///
/// Controls which types of upgrades to include in audit reports.
///
/// # Example
///
/// ```rust
/// use sublime_pkg_tools::config::UpgradeAuditConfig;
///
/// let config = UpgradeAuditConfig::default();
/// assert!(config.include_major);
/// assert!(config.deprecated_as_critical);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default)]
pub struct UpgradeAuditConfig {
    /// Whether to include patch version upgrades.
    ///
    /// # Default: `true`
    pub include_patch: bool,

    /// Whether to include minor version upgrades.
    ///
    /// # Default: `true`
    pub include_minor: bool,

    /// Whether to include major version upgrades.
    ///
    /// # Default: `true`
    pub include_major: bool,

    /// Whether to treat deprecated packages as critical issues.
    ///
    /// # Default: `true`
    pub deprecated_as_critical: bool,
}

/// Configuration for dependency audits.
///
/// Controls which dependency checks to perform.
///
/// # Example
///
/// ```rust
/// use sublime_pkg_tools::config::DependencyAuditConfig;
///
/// let config = DependencyAuditConfig::default();
/// assert!(config.check_circular);
/// assert!(config.check_version_conflicts);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default)]
pub struct DependencyAuditConfig {
    /// Whether to detect circular dependencies.
    ///
    /// # Default: `true`
    pub check_circular: bool,

    /// Whether to check for missing dependencies.
    ///
    /// # Default: `false`
    pub check_missing: bool,

    /// Whether to check for unused dependencies.
    ///
    /// # Default: `false`
    pub check_unused: bool,

    /// Whether to check for version conflicts.
    ///
    /// # Default: `true`
    pub check_version_conflicts: bool,
}

/// Configuration for breaking changes audits.
///
/// Controls how breaking changes are detected.
///
/// # Example
///
/// ```rust
/// use sublime_pkg_tools::config::BreakingChangesAuditConfig;
///
/// let config = BreakingChangesAuditConfig::default();
/// assert!(config.check_conventional_commits);
/// assert!(config.check_changelog);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default)]
pub struct BreakingChangesAuditConfig {
    /// Whether to check for breaking changes in conventional commits.
    ///
    /// # Default: `true`
    pub check_conventional_commits: bool,

    /// Whether to check for breaking changes in changelogs.
    ///
    /// # Default: `true`
    pub check_changelog: bool,
}

/// Configuration for version consistency audits.
///
/// Controls how version inconsistencies are handled.
///
/// # Example
///
/// ```rust
/// use sublime_pkg_tools::config::VersionConsistencyAuditConfig;
///
/// let config = VersionConsistencyAuditConfig::default();
/// assert!(!config.fail_on_inconsistency);
/// assert!(config.warn_on_inconsistency);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(default)]
pub struct VersionConsistencyAuditConfig {
    /// Whether to fail when version inconsistencies are detected.
    ///
    /// # Default: `false`
    pub fail_on_inconsistency: bool,

    /// Whether to warn when version inconsistencies are detected.
    ///
    /// # Default: `true`
    pub warn_on_inconsistency: bool,
}

impl Default for AuditConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            min_severity: "warning".to_string(),
            sections: AuditSectionsConfig::default(),
            upgrades: UpgradeAuditConfig::default(),
            dependencies: DependencyAuditConfig::default(),
            breaking_changes: BreakingChangesAuditConfig::default(),
            version_consistency: VersionConsistencyAuditConfig::default(),
            health_score_weights: HealthScoreWeightsConfig::default(),
        }
    }
}

impl Default for AuditSectionsConfig {
    fn default() -> Self {
        Self {
            upgrades: true,
            dependencies: true,
            breaking_changes: true,
            categorization: true,
            version_consistency: true,
        }
    }
}

impl Default for UpgradeAuditConfig {
    fn default() -> Self {
        Self {
            include_patch: true,
            include_minor: true,
            include_major: true,
            deprecated_as_critical: true,
        }
    }
}

impl Default for DependencyAuditConfig {
    fn default() -> Self {
        Self {
            check_circular: true,
            check_missing: false,
            check_unused: false,
            check_version_conflicts: true,
        }
    }
}

impl Default for BreakingChangesAuditConfig {
    fn default() -> Self {
        Self { check_conventional_commits: true, check_changelog: true }
    }
}

impl Default for VersionConsistencyAuditConfig {
    fn default() -> Self {
        Self { fail_on_inconsistency: false, warn_on_inconsistency: true }
    }
}

/// Configuration for health score calculation weights.
///
/// These weights control how much each type of issue affects the overall health score.
/// All weights should be positive numbers where higher values mean more impact.
///
/// # Example
///
/// ```rust
/// use sublime_pkg_tools::config::HealthScoreWeightsConfig;
///
/// let config = HealthScoreWeightsConfig::default();
/// assert_eq!(config.critical_weight, 15.0);
/// assert_eq!(config.security_multiplier, 1.5);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct HealthScoreWeightsConfig {
    /// Points deducted per critical issue.
    ///
    /// # Default: `15.0`
    pub critical_weight: f64,

    /// Points deducted per warning issue.
    ///
    /// # Default: `5.0`
    pub warning_weight: f64,

    /// Points deducted per info issue.
    ///
    /// # Default: `1.0`
    pub info_weight: f64,

    /// Multiplier for security issues.
    ///
    /// # Default: `1.5`
    pub security_multiplier: f64,

    /// Multiplier for breaking changes issues.
    ///
    /// # Default: `1.3`
    pub breaking_changes_multiplier: f64,

    /// Multiplier for dependency issues.
    ///
    /// # Default: `1.2`
    pub dependencies_multiplier: f64,

    /// Multiplier for version consistency issues.
    ///
    /// # Default: `1.0`
    pub version_consistency_multiplier: f64,

    /// Multiplier for upgrade issues.
    ///
    /// # Default: `0.8`
    pub upgrades_multiplier: f64,

    /// Multiplier for other issues.
    ///
    /// # Default: `1.0`
    pub other_multiplier: f64,
}

impl Default for HealthScoreWeightsConfig {
    fn default() -> Self {
        Self {
            critical_weight: 15.0,
            warning_weight: 5.0,
            info_weight: 1.0,
            security_multiplier: 1.5,
            breaking_changes_multiplier: 1.3,
            dependencies_multiplier: 1.2,
            version_consistency_multiplier: 1.0,
            upgrades_multiplier: 0.8,
            other_multiplier: 1.0,
        }
    }
}

impl Configurable for AuditConfig {
    fn validate(&self) -> ConfigResult<()> {
        // Validate min_severity
        match self.min_severity.as_str() {
            "critical" | "warning" | "info" => {}
            _ => {
                return Err(sublime_standard_tools::config::ConfigError::ValidationError {
                    message: format!(
                        "audit.min_severity: Invalid severity '{}'. Must be one of: critical, warning, info",
                        self.min_severity
                    ),
                });
            }
        }

        self.sections.validate()?;
        self.upgrades.validate()?;
        self.dependencies.validate()?;
        self.breaking_changes.validate()?;
        self.version_consistency.validate()?;
        self.health_score_weights.validate()?;

        Ok(())
    }

    fn merge_with(&mut self, other: Self) -> ConfigResult<()> {
        self.enabled = other.enabled;
        self.min_severity = other.min_severity;
        self.sections.merge_with(other.sections)?;
        self.upgrades.merge_with(other.upgrades)?;
        self.dependencies.merge_with(other.dependencies)?;
        self.breaking_changes.merge_with(other.breaking_changes)?;
        self.version_consistency.merge_with(other.version_consistency)?;
        self.health_score_weights.merge_with(other.health_score_weights)?;
        Ok(())
    }
}

impl Configurable for AuditSectionsConfig {
    fn validate(&self) -> ConfigResult<()> {
        Ok(())
    }

    fn merge_with(&mut self, other: Self) -> ConfigResult<()> {
        self.upgrades = other.upgrades;
        self.dependencies = other.dependencies;
        self.breaking_changes = other.breaking_changes;
        self.categorization = other.categorization;
        self.version_consistency = other.version_consistency;
        Ok(())
    }
}

impl Configurable for UpgradeAuditConfig {
    fn validate(&self) -> ConfigResult<()> {
        Ok(())
    }

    fn merge_with(&mut self, other: Self) -> ConfigResult<()> {
        self.include_patch = other.include_patch;
        self.include_minor = other.include_minor;
        self.include_major = other.include_major;
        self.deprecated_as_critical = other.deprecated_as_critical;
        Ok(())
    }
}

impl Configurable for DependencyAuditConfig {
    fn validate(&self) -> ConfigResult<()> {
        Ok(())
    }

    fn merge_with(&mut self, other: Self) -> ConfigResult<()> {
        self.check_circular = other.check_circular;
        self.check_missing = other.check_missing;
        self.check_unused = other.check_unused;
        self.check_version_conflicts = other.check_version_conflicts;
        Ok(())
    }
}

impl Configurable for BreakingChangesAuditConfig {
    fn validate(&self) -> ConfigResult<()> {
        Ok(())
    }

    fn merge_with(&mut self, other: Self) -> ConfigResult<()> {
        self.check_conventional_commits = other.check_conventional_commits;
        self.check_changelog = other.check_changelog;
        Ok(())
    }
}

impl Configurable for VersionConsistencyAuditConfig {
    fn validate(&self) -> ConfigResult<()> {
        Ok(())
    }

    fn merge_with(&mut self, other: Self) -> ConfigResult<()> {
        self.fail_on_inconsistency = other.fail_on_inconsistency;
        self.warn_on_inconsistency = other.warn_on_inconsistency;
        Ok(())
    }
}

impl Configurable for HealthScoreWeightsConfig {
    fn validate(&self) -> ConfigResult<()> {
        // Validate that all weights are positive
        let weights = [
            ("critical_weight", self.critical_weight),
            ("warning_weight", self.warning_weight),
            ("info_weight", self.info_weight),
            ("security_multiplier", self.security_multiplier),
            ("breaking_changes_multiplier", self.breaking_changes_multiplier),
            ("dependencies_multiplier", self.dependencies_multiplier),
            ("version_consistency_multiplier", self.version_consistency_multiplier),
            ("upgrades_multiplier", self.upgrades_multiplier),
            ("other_multiplier", self.other_multiplier),
        ];

        for (name, value) in &weights {
            if *value < 0.0 {
                return Err(sublime_standard_tools::config::ConfigError::ValidationError {
                    message: format!(
                        "audit.health_score_weights.{}: Must be non-negative, got {}",
                        name, value
                    ),
                });
            }
        }

        Ok(())
    }

    fn merge_with(&mut self, other: Self) -> ConfigResult<()> {
        self.critical_weight = other.critical_weight;
        self.warning_weight = other.warning_weight;
        self.info_weight = other.info_weight;
        self.security_multiplier = other.security_multiplier;
        self.breaking_changes_multiplier = other.breaking_changes_multiplier;
        self.dependencies_multiplier = other.dependencies_multiplier;
        self.version_consistency_multiplier = other.version_consistency_multiplier;
        self.upgrades_multiplier = other.upgrades_multiplier;
        self.other_multiplier = other.other_multiplier;
        Ok(())
    }
}