@@ -28,39 +28,38 @@ use std::time::{
28
28
} ;
29
29
30
30
/// Changelog generator.
31
- #[ derive( Debug ) ]
31
+ #[ derive( Clone , Debug ) ]
32
32
pub struct Changelog < ' a > {
33
33
/// Releases that the changelog will contain.
34
34
pub releases : Vec < Release < ' a > > ,
35
+ /// Configuration.
36
+ pub config : Config ,
35
37
header_template : Option < Template > ,
36
38
body_template : Template ,
37
39
footer_template : Option < Template > ,
38
- config : & ' a Config ,
39
40
additional_context : HashMap < String , serde_json:: Value > ,
40
41
}
41
42
42
43
impl < ' a > Changelog < ' a > {
43
44
/// Constructs a new instance.
44
- pub fn new ( releases : Vec < Release < ' a > > , config : & ' a Config ) -> Result < Self > {
45
- let mut changelog = Changelog :: build ( releases, config) ?;
46
- changelog. add_remote_data ( ) ?;
47
- changelog. process_commits ( ) ;
48
- changelog. process_releases ( ) ;
49
- Ok ( changelog)
50
- }
51
-
52
- /// Builds a changelog from releases and config.
53
- fn build ( releases : Vec < Release < ' a > > , config : & ' a Config ) -> Result < Self > {
45
+ ///
46
+ /// Processes the commits/releases and fetches the remote data
47
+ /// if `process_data` is set to `true`.
48
+ pub fn new (
49
+ releases : Vec < Release < ' a > > ,
50
+ config : Config ,
51
+ process_data : bool ,
52
+ ) -> Result < Self > {
54
53
let trim = config. changelog . trim . unwrap_or ( true ) ;
55
- Ok ( Self {
54
+ let mut changelog = Self {
56
55
releases,
57
56
header_template : match & config. changelog . header {
58
57
Some ( header) => {
59
58
Some ( Template :: new ( "header" , header. to_string ( ) , trim) ?)
60
59
}
61
60
None => None ,
62
61
} ,
63
- body_template : get_body_template ( config, trim) ?,
62
+ body_template : get_body_template ( & config, trim) ?,
64
63
footer_template : match & config. changelog . footer {
65
64
Some ( footer) => {
66
65
Some ( Template :: new ( "footer" , footer. to_string ( ) , trim) ?)
@@ -69,12 +68,18 @@ impl<'a> Changelog<'a> {
69
68
} ,
70
69
config,
71
70
additional_context : HashMap :: new ( ) ,
72
- } )
71
+ } ;
72
+ if process_data {
73
+ changelog. add_remote_data ( ) ?;
74
+ changelog. process_commits ( ) ;
75
+ changelog. process_releases ( ) ;
76
+ }
77
+ Ok ( changelog)
73
78
}
74
79
75
80
/// Constructs an instance from a serialized context object.
76
- pub fn from_context < R : Read > ( input : & mut R , config : & ' a Config ) -> Result < Self > {
77
- Changelog :: build ( serde_json:: from_reader ( input) ?, config)
81
+ pub fn from_context < R : Read > ( input : & mut R , config : Config ) -> Result < Self > {
82
+ Changelog :: new ( serde_json:: from_reader ( input) ?, config, false )
78
83
}
79
84
80
85
/// Adds a key value pair to the template context.
@@ -431,6 +436,7 @@ impl<'a> Changelog<'a> {
431
436
432
437
/// Adds information about the remote to the template context.
433
438
pub fn add_remote_context ( & mut self ) -> Result < ( ) > {
439
+ debug ! ( "Adding remote context..." ) ;
434
440
self . additional_context . insert (
435
441
"remote" . to_string ( ) ,
436
442
serde_json:: to_value ( self . config . remote . clone ( ) ) ?,
@@ -1024,7 +1030,7 @@ mod test {
1024
1030
#[ test]
1025
1031
fn changelog_generator ( ) -> Result < ( ) > {
1026
1032
let ( config, releases) = get_test_data ( ) ;
1027
- let mut changelog = Changelog :: new ( releases, & config) ?;
1033
+ let mut changelog = Changelog :: new ( releases, config, true ) ?;
1028
1034
changelog. bump_version ( ) ?;
1029
1035
changelog. releases [ 0 ] . timestamp = 0 ;
1030
1036
let mut out = Vec :: new ( ) ;
@@ -1143,7 +1149,7 @@ chore(deps): fix broken deps
1143
1149
" ,
1144
1150
) ,
1145
1151
) ) ;
1146
- let changelog = Changelog :: new ( releases, & config) ?;
1152
+ let changelog = Changelog :: new ( releases, config, true ) ?;
1147
1153
let mut out = Vec :: new ( ) ;
1148
1154
changelog. generate ( & mut out) ?;
1149
1155
assert_eq ! (
@@ -1247,7 +1253,7 @@ chore(deps): fix broken deps
1247
1253
{% endfor %}{% endfor %}"#
1248
1254
. to_string ( ) ,
1249
1255
) ;
1250
- let mut changelog = Changelog :: new ( releases, & config) ?;
1256
+ let mut changelog = Changelog :: new ( releases, config, true ) ?;
1251
1257
changelog. add_context ( "custom_field" , "Hello" ) ?;
1252
1258
let mut out = Vec :: new ( ) ;
1253
1259
changelog. generate ( & mut out) ?;
0 commit comments