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
//! A parser library for the [Conventional Commit] specification.
//!
//! [conventional commit]: https://www.conventionalcommits.org
//!
//! # Example
//!
//! ```rust
//! use indoc::indoc;
//! use conventional::{Commit, Error, Simple as _};
//! use std::str::FromStr;
//!
//! fn main() -> Result<(), Error> {
//!     let message = indoc!("
//!         docs(example)!: add tested usage example
//!
//!         This example is tested using Rust's doctest capabilities. Having this
//!         example helps people understand how to use the parser.
//!
//!         BREAKING CHANGE: Going from nothing to something, meaning anyone doing
//!         nothing before suddenly has something to do. That sounds like a change
//!         in your break.
//!
//!         Co-Authored-By: Lisa Simpson <lisa@simpsons.fam>
//!         Closes #12
//!     ");
//!
//!     let commit = Commit::new(message)?;
//!
//!     // You can access all components of the header.
//!     assert_eq!(commit.type_(), "docs");
//!     assert_eq!(commit.scope(), Some("example"));
//!     assert_eq!(commit.description(), "add tested usage example");
//!
//!     // And the free-form commit body.
//!     assert!(commit.body().unwrap().contains("helps people understand"));
//!
//!     // If a commit is marked with a bang (`!`) OR has a trailer with the key
//!     // "BREAKING CHANGE", it is considered a "breaking" commit.
//!     assert!(commit.breaking());
//!
//!     // You can access each trailer individually.
//!     assert!(commit.trailers().get(0).unwrap().value().contains("That sounds like a change"));
//!
//!     // Trailers provide access to their key and value.
//!     assert_eq!(commit.trailers().get(1).unwrap().key(), "Co-Authored-By");
//!     assert_eq!(commit.trailers().get(1).unwrap().value(), "Lisa Simpson <lisa@simpsons.fam>");
//!
//!     // Two types of separators are supported, regular ": ", and " #":
//!     assert_eq!(commit.trailers().get(2).unwrap().separator(), " #");
//!     assert_eq!(commit.trailers().get(2).unwrap().value(), "12");
//!     # Ok(())
//! }
//! ```

#![deny(
    clippy::all,
    clippy::cargo,
    clippy::clone_on_ref_ptr,
    clippy::dbg_macro,
    clippy::indexing_slicing,
    clippy::mem_forget,
    clippy::multiple_inherent_impl,
    clippy::nursery,
    clippy::option_unwrap_used,
    clippy::pedantic,
    clippy::print_stdout,
    clippy::result_unwrap_used,
    clippy::unimplemented,
    clippy::use_debug,
    clippy::wildcard_enum_match_arm,
    clippy::wrong_pub_self_convention,
    deprecated_in_future,
    future_incompatible,
    missing_copy_implementations,
    missing_debug_implementations,
    missing_docs,
    nonstandard_style,
    rust_2018_idioms,
    rustdoc,
    trivial_casts,
    trivial_numeric_casts,
    unreachable_pub,
    unsafe_code,
    unused_import_braces,
    unused_lifetimes,
    unused_qualifications,
    unused_results,
    variant_size_differences,
    warnings
)]
#![doc(html_root_url = "https://docs.rs/conventional")]

pub mod commit;
pub mod component;
pub mod error;
mod parser;

pub use commit::{simple::Simple, typed::Typed, Commit};
pub use component::{
    Body, Description, Scope, SimpleTrailer, Trailer, TrailerKey, TrailerSeparator, TrailerValue,
    Type,
};
pub use error::{Error, Kind as ErrorKind};

#[cfg(test)]
#[allow(clippy::result_unwrap_used)]
mod tests {
    use super::{Commit, ErrorKind, Simple as _};
    use indoc::indoc;

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

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

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

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

            BREAKING CHANGE: breaking change"
        ))
        .unwrap();

        assert_eq!("feat", commit.type_());
        assert_eq!(
            "breaking change",
            &*commit.trailers().get(0).unwrap().value()
        );
        assert!(commit.breaking());
    }

    #[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::new(commit).unwrap();

        assert_eq!("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.trailers().get(0).unwrap().value());
    }

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

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

    mod typed {
        use crate::{component::*, Commit, Typed as _};

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

            assert_eq!(Type("my type"), commit.type_());
            assert_eq!(Some(Scope("my scope")), commit.scope());
            assert_eq!(Description("hello world"), commit.description());
        }
    }
}