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
pub mod simple;
pub mod typed;
use crate::component::{Body, Description, Scope, Trailer, Type};
use crate::error::Error;
use crate::parser::parse;
use nom::error::VerboseError;
use std::fmt;
#[derive(Clone, Debug)]
pub struct Commit<'a> {
ty: Type<'a>,
scope: Option<Scope<'a>>,
description: Description<'a>,
body: Option<Body<'a>>,
breaking: bool,
trailers: Vec<Trailer<'a>>,
}
impl<'a> Commit<'a> {
pub fn new(string: &'a str) -> Result<Self, Error> {
let (ty, scope, breaking, description, body, trailers) =
parse::<VerboseError<&'a str>>(string).map_err(|err| (string, err))?;
Ok(Self {
ty: ty.into(),
scope: scope.map(Into::into),
description: description.into(),
body: body.map(Into::into),
breaking: breaking.is_some()
|| trailers.iter().any(|(k, _, _)| k == &"BREAKING CHANGE"),
trailers: trailers.into_iter().map(Into::into).collect(),
})
}
}
impl fmt::Display for Commit<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use crate::Simple;
f.write_str(self.type_())?;
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 t in self.trailers() {
write!(f, "\n\n{}{}{}", t.key(), t.separator(), t.value())?;
}
Ok(())
}
}