[][src]Crate conventional

A parser library for the Conventional Commit specification.

Example

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");
}

Re-exports

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

Modules

commit

The conventional commit type and its simple, and typed implementations.

component

Conventional Commit components.

error

All errors related to Conventional Commits.