standard-version 0.11.9

Semantic and calendar version bump calculation
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
//! Semantic version bump calculation from conventional commits.
//!
//! Computes the next version from a list of parsed conventional commits and
//! bump rules.
//!
//! # Main entry points
//!
//! - [`determine_bump`] -- analyse commits and return the bump level
//! - [`apply_bump`] -- apply a bump level to a semver version
//! - [`apply_prerelease`] -- bump with a pre-release tag (e.g. `rc.0`)
//! - [`summarise`] -- count commits by category for display

use standard_commit::ConventionalCommit;

/// The level of version bump to apply.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum BumpLevel {
    /// Bug fix -- increment the patch component.
    Patch,
    /// New feature -- increment the minor component.
    Minor,
    /// Breaking change -- increment the major component.
    Major,
}

/// Analyse a list of conventional commits and return the highest applicable
/// bump level.
///
/// Bump rules follow the [Conventional Commits](https://www.conventionalcommits.org/)
/// specification:
/// - `feat` → [`BumpLevel::Minor`]
/// - `fix` or `perf` → [`BumpLevel::Patch`]
/// - `BREAKING CHANGE` footer or `!` suffix → [`BumpLevel::Major`]
///
/// Returns `None` when no bump-worthy commits exist (e.g. only `chore`,
/// `docs`, `refactor`).
pub fn determine_bump(commits: &[ConventionalCommit]) -> Option<BumpLevel> {
    let mut level: Option<BumpLevel> = None;

    for commit in commits {
        let bump = commit_bump(commit);
        if let Some(b) = bump {
            level = Some(match level {
                Some(current) => current.max(b),
                None => b,
            });
        }
    }

    level
}

/// Determine the bump level for a single commit.
fn commit_bump(commit: &ConventionalCommit) -> Option<BumpLevel> {
    // Breaking change (footer or `!` suffix) always yields Major.
    if commit.is_breaking {
        return Some(BumpLevel::Major);
    }
    for footer in &commit.footers {
        if footer.token == "BREAKING CHANGE" || footer.token == "BREAKING-CHANGE" {
            return Some(BumpLevel::Major);
        }
    }

    match commit.r#type.as_str() {
        "feat" => Some(BumpLevel::Minor),
        "fix" | "perf" | "revert" => Some(BumpLevel::Patch),
        _ => None,
    }
}

/// Apply a bump level to a semver version, returning the new version.
///
/// Resets lower components to zero (e.g. minor bump `1.2.3` → `1.3.0`).
///
/// For versions `< 1.0.0`, bump levels are downshifted following the
/// Rust/Cargo pre-1.0 convention:
/// - `Major` (breaking) → bumps minor (`0.10.2` → `0.11.0`)
/// - `Minor` (feature) → bumps patch (`0.10.2` → `0.10.3`)
/// - `Patch` (fix) → bumps patch (`0.10.2` → `0.10.3`)
///
/// For versions `>= 1.0.0`, behaviour is unchanged.
pub fn apply_bump(current: &semver::Version, level: BumpLevel) -> semver::Version {
    let mut next = current.clone();
    // Clear any pre-release or build metadata.
    next.pre = semver::Prerelease::EMPTY;
    next.build = semver::BuildMetadata::EMPTY;

    // Pre-1.0: downshift bump levels (Major→Minor, Minor→Patch, Patch→Patch).
    let effective = if current.major == 0 {
        match level {
            BumpLevel::Major => BumpLevel::Minor,
            BumpLevel::Minor | BumpLevel::Patch => BumpLevel::Patch,
        }
    } else {
        level
    };

    match effective {
        BumpLevel::Major => {
            next.major += 1;
            next.minor = 0;
            next.patch = 0;
        }
        BumpLevel::Minor => {
            next.minor += 1;
            next.patch = 0;
        }
        BumpLevel::Patch => {
            next.patch += 1;
        }
    }

    next
}

/// Apply a pre-release bump. If the current version already has a pre-release
/// tag matching `tag`, the numeric suffix is incremented. Otherwise, `.0` is
/// appended to the bumped version.
///
/// Example: `1.0.0` + Minor + tag `"rc"` → `1.1.0-rc.0`
/// Example: `1.1.0-rc.0` + tag `"rc"` → `1.1.0-rc.1`
pub fn apply_prerelease(current: &semver::Version, level: BumpLevel, tag: &str) -> semver::Version {
    // If already a pre-release with the same tag prefix, just bump the counter.
    if !current.pre.is_empty() {
        let pre_str = current.pre.as_str();
        if let Some(rest) = pre_str.strip_prefix(tag)
            && let Some(num_str) = rest.strip_prefix('.')
            && let Ok(n) = num_str.parse::<u64>()
        {
            let mut next = current.clone();
            next.pre = semver::Prerelease::new(&format!("{tag}.{}", n + 1)).unwrap_or_default();
            next.build = semver::BuildMetadata::EMPTY;
            return next;
        }
    }

    // Otherwise, bump normally then append the pre-release tag.
    let mut next = apply_bump(current, level);
    next.pre = semver::Prerelease::new(&format!("{tag}.0")).unwrap_or_default();
    next
}

/// Summary of analysed commits for display purposes.
///
/// Counts commits by category. A single commit may increment both
/// `breaking_count` and its type count (e.g. a breaking `feat` increments
/// both `feat_count` and `breaking_count`).
#[derive(Debug, Default)]
pub struct BumpSummary {
    /// Count of `feat` commits.
    pub feat_count: usize,
    /// Count of `fix` commits.
    pub fix_count: usize,
    /// Count of commits with breaking changes.
    pub breaking_count: usize,
    /// Count of other conventional commits (perf, refactor, etc.).
    pub other_count: usize,
}

/// Summarise a list of conventional commits for display purposes.
pub fn summarise(commits: &[ConventionalCommit]) -> BumpSummary {
    let mut summary = BumpSummary::default();
    for commit in commits {
        let is_breaking = commit.is_breaking
            || commit
                .footers
                .iter()
                .any(|f| f.token == "BREAKING CHANGE" || f.token == "BREAKING-CHANGE");
        if is_breaking {
            summary.breaking_count += 1;
        }
        match commit.r#type.as_str() {
            "feat" => summary.feat_count += 1,
            "fix" => summary.fix_count += 1,
            _ => summary.other_count += 1,
        }
    }
    summary
}

#[cfg(test)]
mod tests {
    use super::*;
    use standard_commit::Footer;

    fn commit(typ: &str, breaking: bool) -> ConventionalCommit {
        ConventionalCommit {
            r#type: typ.to_string(),
            scope: None,
            description: "test".to_string(),
            body: None,
            footers: vec![],
            is_breaking: breaking,
        }
    }

    fn commit_with_footer(typ: &str, footer_token: &str) -> ConventionalCommit {
        ConventionalCommit {
            r#type: typ.to_string(),
            scope: None,
            description: "test".to_string(),
            body: None,
            footers: vec![Footer {
                token: footer_token.to_string(),
                value: "some breaking change".to_string(),
            }],
            is_breaking: false,
        }
    }

    #[test]
    fn no_commits_returns_none() {
        assert_eq!(determine_bump(&[]), None);
    }

    #[test]
    fn non_bump_commits_return_none() {
        let commits = vec![commit("chore", false), commit("docs", false)];
        assert_eq!(determine_bump(&commits), None);
    }

    #[test]
    fn fix_yields_patch() {
        let commits = vec![commit("fix", false)];
        assert_eq!(determine_bump(&commits), Some(BumpLevel::Patch));
    }

    #[test]
    fn perf_yields_patch() {
        let commits = vec![commit("perf", false)];
        assert_eq!(determine_bump(&commits), Some(BumpLevel::Patch));
    }

    #[test]
    fn feat_yields_minor() {
        let commits = vec![commit("feat", false)];
        assert_eq!(determine_bump(&commits), Some(BumpLevel::Minor));
    }

    #[test]
    fn breaking_bang_yields_major() {
        let commits = vec![commit("feat", true)];
        assert_eq!(determine_bump(&commits), Some(BumpLevel::Major));
    }

    #[test]
    fn breaking_footer_yields_major() {
        let commits = vec![commit_with_footer("fix", "BREAKING CHANGE")];
        assert_eq!(determine_bump(&commits), Some(BumpLevel::Major));
    }

    #[test]
    fn breaking_change_hyphenated_footer() {
        let commits = vec![commit_with_footer("fix", "BREAKING-CHANGE")];
        assert_eq!(determine_bump(&commits), Some(BumpLevel::Major));
    }

    #[test]
    fn highest_bump_wins() {
        let commits = vec![commit("fix", false), commit("feat", false)];
        assert_eq!(determine_bump(&commits), Some(BumpLevel::Minor));
    }

    #[test]
    fn breaking_beats_all() {
        let commits = vec![
            commit("fix", false),
            commit("feat", false),
            commit("chore", true),
        ];
        assert_eq!(determine_bump(&commits), Some(BumpLevel::Major));
    }

    #[test]
    fn apply_bump_patch() {
        let v = semver::Version::new(1, 2, 3);
        assert_eq!(
            apply_bump(&v, BumpLevel::Patch),
            semver::Version::new(1, 2, 4)
        );
    }

    #[test]
    fn apply_bump_minor() {
        let v = semver::Version::new(1, 2, 3);
        assert_eq!(
            apply_bump(&v, BumpLevel::Minor),
            semver::Version::new(1, 3, 0)
        );
    }

    #[test]
    fn apply_bump_major() {
        let v = semver::Version::new(1, 2, 3);
        assert_eq!(
            apply_bump(&v, BumpLevel::Major),
            semver::Version::new(2, 0, 0)
        );
    }

    #[test]
    fn apply_bump_clears_prerelease() {
        let v = semver::Version::parse("1.2.3-rc.1").unwrap();
        assert_eq!(
            apply_bump(&v, BumpLevel::Patch),
            semver::Version::new(1, 2, 4)
        );
    }

    #[test]
    fn apply_prerelease_new() {
        let v = semver::Version::new(1, 0, 0);
        let next = apply_prerelease(&v, BumpLevel::Minor, "rc");
        assert_eq!(next, semver::Version::parse("1.1.0-rc.0").unwrap());
    }

    #[test]
    fn apply_prerelease_increment() {
        let v = semver::Version::parse("1.1.0-rc.0").unwrap();
        let next = apply_prerelease(&v, BumpLevel::Minor, "rc");
        assert_eq!(next, semver::Version::parse("1.1.0-rc.1").unwrap());
    }

    #[test]
    fn apply_prerelease_different_tag() {
        let v = semver::Version::parse("1.1.0-alpha.2").unwrap();
        let next = apply_prerelease(&v, BumpLevel::Minor, "rc");
        // Different tag → bump normally and start at 0.
        assert_eq!(next, semver::Version::parse("1.2.0-rc.0").unwrap());
    }

    #[test]
    fn summarise_counts() {
        let commits = vec![
            commit("feat", false),
            commit("feat", false),
            commit("fix", false),
            commit("chore", true),
            commit("refactor", false),
        ];
        let s = summarise(&commits);
        assert_eq!(s.feat_count, 2);
        assert_eq!(s.fix_count, 1);
        assert_eq!(s.breaking_count, 1);
        assert_eq!(s.other_count, 2); // chore + refactor
    }

    #[test]
    fn bump_level_ordering() {
        assert!(BumpLevel::Major > BumpLevel::Minor);
        assert!(BumpLevel::Minor > BumpLevel::Patch);
    }

    // ── Pre-1.0 semver convention ──────────────────────────────────

    #[test]
    fn pre1_breaking_bumps_minor() {
        let v = semver::Version::new(0, 10, 2);
        assert_eq!(
            apply_bump(&v, BumpLevel::Major),
            semver::Version::new(0, 11, 0)
        );
    }

    #[test]
    fn pre1_feat_bumps_patch() {
        let v = semver::Version::new(0, 10, 2);
        assert_eq!(
            apply_bump(&v, BumpLevel::Minor),
            semver::Version::new(0, 10, 3)
        );
    }

    #[test]
    fn pre1_fix_bumps_patch() {
        let v = semver::Version::new(0, 10, 2);
        assert_eq!(
            apply_bump(&v, BumpLevel::Patch),
            semver::Version::new(0, 10, 3)
        );
    }

    #[test]
    fn pre1_zero_minor_breaking_bumps_minor() {
        let v = semver::Version::new(0, 0, 5);
        assert_eq!(
            apply_bump(&v, BumpLevel::Major),
            semver::Version::new(0, 1, 0)
        );
    }

    #[test]
    fn pre1_zero_minor_feat_bumps_patch() {
        let v = semver::Version::new(0, 0, 5);
        assert_eq!(
            apply_bump(&v, BumpLevel::Minor),
            semver::Version::new(0, 0, 6)
        );
    }

    #[test]
    fn post1_major_unchanged() {
        let v = semver::Version::new(1, 2, 3);
        assert_eq!(
            apply_bump(&v, BumpLevel::Major),
            semver::Version::new(2, 0, 0)
        );
    }

    #[test]
    fn pre1_clears_prerelease_metadata() {
        let v = semver::Version::parse("0.3.0-rc.2").unwrap();
        assert_eq!(
            apply_bump(&v, BumpLevel::Major),
            semver::Version::new(0, 4, 0)
        );
    }

    #[test]
    fn pre1_prerelease_breaking() {
        let v = semver::Version::new(0, 5, 0);
        let next = apply_prerelease(&v, BumpLevel::Major, "rc");
        assert_eq!(next, semver::Version::parse("0.6.0-rc.0").unwrap());
    }

    #[test]
    fn pre1_prerelease_feat() {
        let v = semver::Version::new(0, 5, 0);
        let next = apply_prerelease(&v, BumpLevel::Minor, "rc");
        assert_eq!(next, semver::Version::parse("0.5.1-rc.0").unwrap());
    }
}