@@ -28,39 +28,38 @@ use std::time::{
2828} ;
2929
3030/// Changelog generator.
31- #[ derive( Debug ) ]
31+ #[ derive( Clone , Debug ) ]
3232pub struct Changelog < ' a > {
3333 /// Releases that the changelog will contain.
3434 pub releases : Vec < Release < ' a > > ,
35+ /// Configuration.
36+ pub config : Config ,
3537 header_template : Option < Template > ,
3638 body_template : Template ,
3739 footer_template : Option < Template > ,
38- config : & ' a Config ,
3940 additional_context : HashMap < String , serde_json:: Value > ,
4041}
4142
4243impl < ' a > Changelog < ' a > {
4344 /// 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 > {
5453 let trim = config. changelog . trim . unwrap_or ( true ) ;
55- Ok ( Self {
54+ let mut changelog = Self {
5655 releases,
5756 header_template : match & config. changelog . header {
5857 Some ( header) => {
5958 Some ( Template :: new ( "header" , header. to_string ( ) , trim) ?)
6059 }
6160 None => None ,
6261 } ,
63- body_template : get_body_template ( config, trim) ?,
62+ body_template : get_body_template ( & config, trim) ?,
6463 footer_template : match & config. changelog . footer {
6564 Some ( footer) => {
6665 Some ( Template :: new ( "footer" , footer. to_string ( ) , trim) ?)
@@ -69,12 +68,18 @@ impl<'a> Changelog<'a> {
6968 } ,
7069 config,
7170 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)
7378 }
7479
7580 /// 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 )
7883 }
7984
8085 /// Adds a key value pair to the template context.
@@ -431,6 +436,7 @@ impl<'a> Changelog<'a> {
431436
432437 /// Adds information about the remote to the template context.
433438 pub fn add_remote_context ( & mut self ) -> Result < ( ) > {
439+ debug ! ( "Adding remote context..." ) ;
434440 self . additional_context . insert (
435441 "remote" . to_string ( ) ,
436442 serde_json:: to_value ( self . config . remote . clone ( ) ) ?,
@@ -1024,7 +1030,7 @@ mod test {
10241030 #[ test]
10251031 fn changelog_generator ( ) -> Result < ( ) > {
10261032 let ( config, releases) = get_test_data ( ) ;
1027- let mut changelog = Changelog :: new ( releases, & config) ?;
1033+ let mut changelog = Changelog :: new ( releases, config, true ) ?;
10281034 changelog. bump_version ( ) ?;
10291035 changelog. releases [ 0 ] . timestamp = 0 ;
10301036 let mut out = Vec :: new ( ) ;
@@ -1143,7 +1149,7 @@ chore(deps): fix broken deps
11431149" ,
11441150 ) ,
11451151 ) ) ;
1146- let changelog = Changelog :: new ( releases, & config) ?;
1152+ let changelog = Changelog :: new ( releases, config, true ) ?;
11471153 let mut out = Vec :: new ( ) ;
11481154 changelog. generate ( & mut out) ?;
11491155 assert_eq ! (
@@ -1247,7 +1253,7 @@ chore(deps): fix broken deps
12471253 {% endfor %}{% endfor %}"#
12481254 . to_string ( ) ,
12491255 ) ;
1250- let mut changelog = Changelog :: new ( releases, & config) ?;
1256+ let mut changelog = Changelog :: new ( releases, config, true ) ?;
12511257 changelog. add_context ( "custom_field" , "Hello" ) ?;
12521258 let mut out = Vec :: new ( ) ;
12531259 changelog. generate ( & mut out) ?;
0 commit comments