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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//! The conventional commit type and its simple, and typed implementations.

use std::fmt;
use std::ops::Deref;
use std::str::FromStr;

use winnow::error::ContextError;
use winnow::Parser;

use crate::parser::parse;
use crate::{Error, ErrorKind};

const BREAKING_PHRASE: &str = "BREAKING CHANGE";
const BREAKING_ARROW: &str = "BREAKING-CHANGE";

/// A conventional commit.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Commit<'a> {
    ty: Type<'a>,
    scope: Option<Scope<'a>>,
    description: &'a str,
    body: Option<&'a str>,
    breaking: bool,
    #[cfg_attr(feature = "serde", serde(skip))]
    breaking_description: Option<&'a str>,
    footers: Vec<Footer<'a>>,
}

impl<'a> Commit<'a> {
    /// Create a new Conventional Commit based on the provided commit message
    /// string.
    ///
    /// # Errors
    ///
    /// This function returns an error if the commit does not conform to the
    /// Conventional Commit specification.
    pub fn parse(string: &'a str) -> Result<Self, Error> {
        let (ty, scope, breaking, description, body, footers) = parse::<ContextError>
            .parse(string)
            .map_err(|err| Error::with_nom(string, err))?;

        let breaking_description = footers
            .iter()
            .filter_map(|(k, _, v)| (k == &BREAKING_PHRASE || k == &BREAKING_ARROW).then_some(*v))
            .next()
            .or_else(|| breaking.then_some(description));
        let breaking = breaking_description.is_some();
        let footers: Result<Vec<_>, Error> = footers
            .into_iter()
            .map(|(k, s, v)| Ok(Footer::new(FooterToken::new_unchecked(k), s.parse()?, v)))
            .collect();
        let footers = footers?;

        Ok(Self {
            ty: Type::new_unchecked(ty),
            scope: scope.map(Scope::new_unchecked),
            description,
            body,
            breaking,
            breaking_description,
            footers,
        })
    }

    /// The type of the commit.
    pub fn type_(&self) -> Type<'a> {
        self.ty
    }

    /// The optional scope of the commit.
    pub fn scope(&self) -> Option<Scope<'a>> {
        self.scope
    }

    /// The commit description.
    pub fn description(&self) -> &'a str {
        self.description
    }

    /// The commit body, containing a more detailed explanation of the commit
    /// changes.
    pub fn body(&self) -> Option<&'a str> {
        self.body
    }

    /// A flag to signal that the commit contains breaking changes.
    ///
    /// This flag is set either when the commit has an exclamation mark after
    /// the message type and scope, e.g.:
    /// ```text
    /// feat(scope)!: this is a breaking change
    /// ```
    ///
    /// Or when the `BREAKING CHANGE: ` footer is defined:
    /// ```text
    /// feat: my commit description
    ///
    /// BREAKING CHANGE: this is a breaking change
    /// ```
    pub fn breaking(&self) -> bool {
        self.breaking
    }

    /// Explanation for the breaking change.
    ///
    /// Note: if no `BREAKING CHANGE` footer is provided, the `description` is expected to describe
    /// the breaking change.
    pub fn breaking_description(&self) -> Option<&'a str> {
        self.breaking_description
    }

    /// Any footer.
    ///
    /// A footer is similar to a Git trailer, with the exception of not
    /// requiring whitespace before newlines.
    ///
    /// See: <https://git-scm.com/docs/git-interpret-trailers>
    pub fn footers(&self) -> &[Footer<'a>] {
        &self.footers
    }
}

impl fmt::Display for Commit<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.type_().as_str())?;

        if let Some(scope) = &self.scope() {
            f.write_fmt(format_args!("({})", scope))?;
        }

        f.write_fmt(format_args!(": {}", &self.description()))?;

        if let Some(body) = &self.body() {
            f.write_fmt(format_args!("\n\n{}", body))?;
        }

        for footer in self.footers() {
            write!(f, "\n\n{footer}")?;
        }

        Ok(())
    }
}

/// A single footer.
///
/// A footer is similar to a Git trailer, with the exception of not requiring
/// whitespace before newlines.
///
/// See: <https://git-scm.com/docs/git-interpret-trailers>
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Footer<'a> {
    token: FooterToken<'a>,
    sep: FooterSeparator,
    value: &'a str,
}

impl<'a> Footer<'a> {
    /// Piece together a footer.
    pub const fn new(token: FooterToken<'a>, sep: FooterSeparator, value: &'a str) -> Self {
        Self { token, sep, value }
    }

    /// The token of the footer.
    pub const fn token(&self) -> FooterToken<'a> {
        self.token
    }

    /// The separator between the footer token and its value.
    pub const fn separator(&self) -> FooterSeparator {
        self.sep
    }

    /// The value of the footer.
    pub const fn value(&self) -> &'a str {
        self.value
    }

    /// A flag to signal that the footer describes a breaking change.
    pub fn breaking(&self) -> bool {
        self.token.breaking()
    }
}

impl<'a> fmt::Display for Footer<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self { token, sep, value } = self;
        write!(f, "{token}{sep}{value}")
    }
}

/// The type of separator between the footer token and value.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum FooterSeparator {
    /// ":"
    Value,

    /// " #"
    Ref,
}

impl FooterSeparator {
    /// Access `str` representation of FooterSeparator
    pub fn as_str(self) -> &'static str {
        match self {
            FooterSeparator::Value => ":",
            FooterSeparator::Ref => " #",
        }
    }
}

impl Deref for FooterSeparator {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl PartialEq<&'_ str> for FooterSeparator {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

impl fmt::Display for FooterSeparator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self)
    }
}

impl FromStr for FooterSeparator {
    type Err = Error;

    fn from_str(sep: &str) -> Result<Self, Self::Err> {
        match sep {
            ":" => Ok(FooterSeparator::Value),
            " #" => Ok(FooterSeparator::Ref),
            _ => {
                Err(Error::new(ErrorKind::InvalidFooter)
                    .set_context(Box::new(format!("{:?}", sep))))
            }
        }
    }
}

macro_rules! unicase_components {
    ($($ty:ident),+) => (
        $(
            /// A component of the conventional commit.
            #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
            pub struct $ty<'a>(unicase::UniCase<&'a str>);

            impl<'a> $ty<'a> {
                /// See `parse` for ensuring the data is valid.
                pub const fn new_unchecked(value: &'a str) -> Self {
                    $ty(unicase::UniCase::unicode(value))
                }

                /// Access `str` representation
                pub fn as_str(&self) -> &'a str {
                    &self.0.into_inner()
                }
            }

            impl Deref for $ty<'_> {
                type Target = str;

                fn deref(&self) -> &Self::Target {
                    self.as_str()
                }
            }

            impl PartialEq<&'_ str> for $ty<'_> {
                fn eq(&self, other: &&str) -> bool {
                    *self == $ty::new_unchecked(*other)
                }
            }

            impl fmt::Display for $ty<'_> {
                fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                    self.0.fmt(f)
                }
            }

            #[cfg(feature = "serde")]
            impl serde::Serialize for $ty<'_> {
                fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
                where
                    S: serde::Serializer,
                {
                    serializer.serialize_str(self)
                }
            }
        )+
    )
}

unicase_components![Type, Scope, FooterToken];

impl<'a> Type<'a> {
    /// Parse a `str` into a `Type`.
    pub fn parse(sep: &'a str) -> Result<Self, Error> {
        let t = crate::parser::type_::<ContextError>
            .parse(sep)
            .map_err(|err| Error::with_nom(sep, err))?;
        Ok(Type::new_unchecked(t))
    }
}

/// Common commit types
impl Type<'static> {
    /// Commit type when introducing new features (correlates with `minor` in semver)
    pub const FEAT: Type<'static> = Type::new_unchecked("feat");
    /// Commit type when patching a bug (correlates with `patch` in semver)
    pub const FIX: Type<'static> = Type::new_unchecked("fix");
    /// Possible commit type when reverting changes.
    pub const REVERT: Type<'static> = Type::new_unchecked("revert");
    /// Possible commit type for changing documentation.
    pub const DOCS: Type<'static> = Type::new_unchecked("docs");
    /// Possible commit type for changing code style.
    pub const STYLE: Type<'static> = Type::new_unchecked("style");
    /// Possible commit type for refactoring code structure.
    pub const REFACTOR: Type<'static> = Type::new_unchecked("refactor");
    /// Possible commit type for performance optimizations.
    pub const PERF: Type<'static> = Type::new_unchecked("perf");
    /// Possible commit type for addressing tests.
    pub const TEST: Type<'static> = Type::new_unchecked("test");
    /// Possible commit type for other things.
    pub const CHORE: Type<'static> = Type::new_unchecked("chore");
}

impl<'a> Scope<'a> {
    /// Parse a `str` into a `Scope`.
    pub fn parse(sep: &'a str) -> Result<Self, Error> {
        let t = crate::parser::scope::<ContextError>
            .parse(sep)
            .map_err(|err| Error::with_nom(sep, err))?;
        Ok(Scope::new_unchecked(t))
    }
}

impl<'a> FooterToken<'a> {
    /// Parse a `str` into a `FooterToken`.
    pub fn parse(sep: &'a str) -> Result<Self, Error> {
        let t = crate::parser::token::<ContextError>
            .parse(sep)
            .map_err(|err| Error::with_nom(sep, err))?;
        Ok(FooterToken::new_unchecked(t))
    }

    /// A flag to signal that the footer describes a breaking change.
    pub fn breaking(&self) -> bool {
        self == &BREAKING_PHRASE || self == &BREAKING_ARROW
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::ErrorKind;
    use indoc::indoc;
    #[cfg(feature = "serde")]
    use serde_test::Token;

    #[test]
    fn test_valid_simple_commit() {
        let commit = Commit::parse("type(my scope): hello world").unwrap();

        assert_eq!(commit.type_(), "type");
        assert_eq!(commit.scope().unwrap(), "my scope");
        assert_eq!(commit.description(), "hello world");
    }

    #[test]
    fn test_trailing_whitespace_without_body() {
        let commit = Commit::parse("type(my scope): hello world\n\n\n").unwrap();

        assert_eq!(commit.type_(), "type");
        assert_eq!(commit.scope().unwrap(), "my scope");
        assert_eq!(commit.description(), "hello world");
    }

    #[test]
    fn test_trailing_1_nl() {
        let commit = Commit::parse("type: hello world\n").unwrap();

        assert_eq!(commit.type_(), "type");
        assert_eq!(commit.scope(), None);
        assert_eq!(commit.description(), "hello world");
    }

    #[test]
    fn test_trailing_2_nl() {
        let commit = Commit::parse("type: hello world\n\n").unwrap();

        assert_eq!(commit.type_(), "type");
        assert_eq!(commit.scope(), None);
        assert_eq!(commit.description(), "hello world");
    }

    #[test]
    fn test_trailing_3_nl() {
        let commit = Commit::parse("type: hello world\n\n\n").unwrap();

        assert_eq!(commit.type_(), "type");
        assert_eq!(commit.scope(), None);
        assert_eq!(commit.description(), "hello world");
    }

    #[test]
    fn test_parenthetical_statement() {
        let commit = Commit::parse("type: hello world (#1)").unwrap();

        assert_eq!(commit.type_(), "type");
        assert_eq!(commit.scope(), None);
        assert_eq!(commit.description(), "hello world (#1)");
    }

    #[test]
    fn test_multiline_description() {
        let err = Commit::parse(
            "chore: Automate fastlane when a file in the fastlane directory is\nchanged (hopefully)",
        ).unwrap_err();

        assert_eq!(ErrorKind::InvalidBody, err.kind());
    }

    #[test]
    fn test_issue_12_case_1() {
        // Looks like it was test_trailing_2_nl that triggered this to fail originally
        let commit = Commit::parse("chore: add .hello.txt (#1)\n\n").unwrap();

        assert_eq!(commit.type_(), "chore");
        assert_eq!(commit.scope(), None);
        assert_eq!(commit.description(), "add .hello.txt (#1)");
    }

    #[test]
    fn test_issue_12_case_2() {
        // Looks like it was test_trailing_2_nl that triggered this to fail originally
        let commit = Commit::parse("refactor: use fewer lines (#3)\n\n").unwrap();

        assert_eq!(commit.type_(), "refactor");
        assert_eq!(commit.scope(), None);
        assert_eq!(commit.description(), "use fewer lines (#3)");
    }

    #[test]
    fn test_breaking_change() {
        let commit = Commit::parse("feat!: this is a breaking change").unwrap();
        assert_eq!(Type::FEAT, commit.type_());
        assert!(commit.breaking());
        assert_eq!(
            commit.breaking_description(),
            Some("this is a breaking change")
        );

        let commit = Commit::parse(indoc!(
            "feat: message

            BREAKING CHANGE: breaking change"
        ))
        .unwrap();
        assert_eq!(Type::FEAT, commit.type_());
        assert_eq!("breaking change", commit.footers().get(0).unwrap().value());
        assert!(commit.breaking());
        assert_eq!(commit.breaking_description(), Some("breaking change"));

        let commit = Commit::parse(indoc!(
            "fix: message

            BREAKING-CHANGE: it's broken"
        ))
        .unwrap();
        assert_eq!(Type::FIX, commit.type_());
        assert_eq!("it's broken", commit.footers().get(0).unwrap().value());
        assert!(commit.breaking());
        assert_eq!(commit.breaking_description(), Some("it's broken"));
    }

    #[test]
    fn test_conjoined_footer() {
        let commit = Commit::parse(
            "fix(example): fix keepachangelog config example

Fixes: #123, #124, #125",
        )
        .unwrap();
        assert_eq!(Type::FIX, commit.type_());
        assert_eq!(commit.body(), None);
        assert_eq!(
            commit.footers(),
            [Footer::new(
                FooterToken("Fixes".into()),
                FooterSeparator::Value,
                "#123, #124, #125"
            ),]
        );
    }

    #[test]
    fn test_windows_line_endings() {
        let commit =
            Commit::parse("feat: thing\r\n\r\nbody\r\n\r\ncloses #1234\r\n\r\n\r\nBREAKING CHANGE: something broke\r\n\r\n")
                .unwrap();
        assert_eq!(commit.body(), Some("body"));
        assert_eq!(
            commit.footers(),
            [
                Footer::new(FooterToken("closes".into()), FooterSeparator::Ref, "1234"),
                Footer::new(
                    FooterToken("BREAKING CHANGE".into()),
                    FooterSeparator::Value,
                    "something broke"
                ),
            ]
        );
        assert_eq!(commit.breaking_description(), Some("something broke"));
    }

    #[test]
    fn test_extra_line_endings() {
        let commit =
            Commit::parse("feat: thing\n\n\n\n\nbody\n\n\n\n\ncloses #1234\n\n\n\n\n\nBREAKING CHANGE: something broke\n\n\n\n")
                .unwrap();
        assert_eq!(commit.body(), Some("body"));
        assert_eq!(
            commit.footers(),
            [
                Footer::new(FooterToken("closes".into()), FooterSeparator::Ref, "1234"),
                Footer::new(
                    FooterToken("BREAKING CHANGE".into()),
                    FooterSeparator::Value,
                    "something broke"
                ),
            ]
        );
        assert_eq!(commit.breaking_description(), Some("something broke"));
    }

    #[test]
    fn test_fake_footer() {
        let commit = indoc! {"
            fix: something something

            First line of the body
            IMPORTANT: Please see something else for details.
            Another line here.
        "};

        let commit = Commit::parse(commit).unwrap();

        assert_eq!(Type::FIX, commit.type_());
        assert_eq!(None, commit.scope());
        assert_eq!("something something", commit.description());
        assert_eq!(
            Some(indoc!(
                "
                First line of the body
                IMPORTANT: Please see something else for details.
                Another line here."
            )),
            commit.body()
        );
        let empty_footer: &[Footer<'_>] = &[];
        assert_eq!(empty_footer, commit.footers());
    }

    #[test]
    fn test_valid_complex_commit() {
        let commit = indoc! {"
            chore: improve changelog readability

            Change date notation from YYYY-MM-DD to YYYY.MM.DD to make it a tiny bit
            easier to parse while reading.

            BREAKING CHANGE: Just kidding!
        "};

        let commit = Commit::parse(commit).unwrap();

        assert_eq!(Type::CHORE, commit.type_());
        assert_eq!(None, commit.scope());
        assert_eq!("improve changelog readability", commit.description());
        assert_eq!(
            Some(indoc!(
                "Change date notation from YYYY-MM-DD to YYYY.MM.DD to make it a tiny bit
                 easier to parse while reading."
            )),
            commit.body()
        );
        assert_eq!("Just kidding!", commit.footers().get(0).unwrap().value());
    }

    #[test]
    fn test_missing_type() {
        let err = Commit::parse("").unwrap_err();

        assert_eq!(ErrorKind::MissingType, err.kind());
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_commit_serialize() {
        let commit = Commit::parse("type(my scope): hello world").unwrap();
        serde_test::assert_ser_tokens(
            &commit,
            &[
                Token::Struct {
                    name: "Commit",
                    len: 6,
                },
                Token::Str("ty"),
                Token::Str("type"),
                Token::Str("scope"),
                Token::Some,
                Token::Str("my scope"),
                Token::Str("description"),
                Token::Str("hello world"),
                Token::Str("body"),
                Token::None,
                Token::Str("breaking"),
                Token::Bool(false),
                Token::Str("footers"),
                Token::Seq { len: Some(0) },
                Token::SeqEnd,
                Token::StructEnd,
            ],
        );
    }
}