willbe 0.34.0

Utility to publish multi-crate and multi-workspace environments and maintain their consistency.
Documentation
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/// Define a private namespace for all its items.
#[ allow( clippy ::std_instead_of_alloc, clippy ::std_instead_of_core ) ]
mod private
{

  use crate :: *;

  use std ::
  {
  fmt,
  str ::FromStr,
 };
  use std ::fmt ::Formatter;
  use toml_edit ::value;
  use semver ::Version as SemVersion;

  use error ::untyped ::Result;
  use crate ::entity ::manifest ::Manifest;
  use crate ::entity ::package ::Package;
  use { error ::untyped ::format_err, iter ::Itertools };

  /// Wrapper for a `SemVer` structure
  #[ derive( Debug, Clone, Eq, PartialEq, Ord, PartialOrd ) ]
  pub struct Version( SemVersion );

  impl FromStr for Version
  {
  type Err =  semver ::Error;

  fn from_str( s: &str ) -> std ::result ::Result< Self, Self ::Err >
  {
   std ::result ::Result ::Ok( Self( SemVersion ::from_str( s )? ) )
 }
 }

  impl TryFrom< &str > for Version
  {
  type Error = semver ::Error;

  fn try_from( value: &str ) -> Result< Self, Self ::Error >
  {
   FromStr ::from_str( value )
 }
 }

  impl TryFrom< &String > for Version
  {
  type Error = semver ::Error;

  fn try_from( value: &String ) -> Result< Self, Self ::Error >
  {
   Self ::try_from( value.as_str() )
 }
 }

  impl fmt ::Display for Version
  {
  fn fmt( &self, f: &mut fmt ::Formatter< '_ > ) -> fmt ::Result
  {
   write!( f, "{}", self.0 )
 }
 }

  impl Version
  {
  /// Bump a version with default strategy
  ///
  /// This function increases first not 0 number
  #[ must_use ]
  pub fn bump( self ) -> Self
  {
   let mut ver = self.0;
   // we shouldn't change the major part of a version yet
   if ver.minor != 0 || ver.major != 0
   {
  ver.minor += 1;
  ver.patch = 0;
 }
   else
   {
  ver.patch += 1;
 }

   Self( ver )
 }
 }

  /// A structure that represents a bump report, which contains information about a version bump.
  #[ derive( Debug, Default, Clone ) ]
  pub struct BumpReport
  {
  /// Pacakge name.
  pub name: Option< String >,
  /// Package old version.
  pub old_version: Option< String >,
  /// Package new version.
  pub new_version: Option< String >,
 }

  impl fmt ::Display for BumpReport
  {
  fn fmt( &self, f: &mut fmt ::Formatter< '_ > ) -> fmt ::Result
  {
   let Self { name, old_version, new_version } = self;
   match ( name, old_version, new_version )
   {
  ( Some( name ), Some( old_version ), Some( new_version ) )
  => f.write_fmt( format_args!( "`{name}` bumped from {old_version} to {new_version}" ) ),
  _ => f.write_fmt( format_args!( "Bump failed" ) )
 }
 }
 }

  // qqq: we have to replace the implementation above with the implementation below, don't we?
  // qqq: for Bohdan: duplication?

  /// `BumpOptions` manages the details necessary for the version bump process for crates.
  /// This includes the directory of the crate whose version is being bumped, the old and new version numbers,
  /// and the set of dependencies of that crate.
  #[ derive( Debug, Clone ) ]
  pub struct BumpOptions
  {
  /// `crate_dir` - The directory of the crate which you want to bump the version of. This value is
  /// represented by `CrateDir` which indicates the directory of the crate.
  pub crate_dir: CrateDir,

  /// `old_version` - The version of the crate before the bump. It's represented by `Version` which
  /// denotes the old version number of the crate.
  pub old_version: Version,

  /// `new_version` - The version number to assign to the crate after the bump. It's also represented
  /// by `Version` which denotes the new version number of the crate.
  pub new_version: Version,

  /// `dependencies` - This is a vector containing the directories of all the dependencies of the crate.
  /// Each item in the `dependencies` vector indicates a `CrateDir` directory of a single dependency.
  pub dependencies: Vec< CrateDir >,

  /// `dry` - A boolean indicating whether to do a "dry run". If set to `true`, a simulated run is performed
  /// without making actual changes. If set to `false`, the operations are actually executed. This is
  /// useful for validating the process of bumping up the version or for testing and debugging.
  pub dry: bool,
 }

  /// Report about a changing version.
  #[ derive( Debug, Default, Clone ) ]
  pub struct ExtendedBumpReport
  {
  /// Pacakge name.
  pub name: Option< String >,
  /// Package old version.
  pub old_version: Option< String >,
  /// Package new version.
  pub new_version: Option< String >,
  /// Files that should(already) changed for bump.
  pub changed_files: Vec< ManifestFile >
 }

  impl std ::fmt ::Display for ExtendedBumpReport
  {
  fn fmt( &self, f: &mut Formatter< '_ > ) -> std ::fmt ::Result
  {
   let Self { name, old_version, new_version, changed_files } = self;
   if self.changed_files.is_empty()
   {
  write!( f, "Files were not changed during bumping the version" )?;
  return std ::fmt ::Result ::Ok( () )
 }

   let files = changed_files.iter().map( | f | f.as_ref().display() ).join( ",\n    " );
   match ( name, old_version, new_version )
   {
  ( Some( name ), Some( old_version ), Some( new_version ) )
  => writeln!( f, "`{name}` bumped from {old_version} to {new_version}\n  changed files: \n    {files}" ),
  _ => writeln!( f, "Bump failed" )
 }?;

   std ::fmt ::Result ::Ok( () )
 }
 }


  /// Bumps the version of a package and its dependencies.
  ///
  /// # Arguments
  ///
  /// * `o` - The options for version bumping.
  ///
  /// # Returns
  ///
  /// Returns a result containing the extended bump report if successful.
  ///
  ///
  /// # Errors
  /// qqq: doc
  // qqq: should be typed error, apply err_with
  // qqq: don't use 1-prameter Result
  pub fn bump( o: BumpOptions ) -> Result< ExtendedBumpReport >
  {
  let mut report = ExtendedBumpReport ::default();
  // let manifest_file = o.crate_dir.inner().join( "Cargo.toml" );
  let manifest_file = o.crate_dir.manifest_file();
  let package = Package ::try_from( manifest_file.clone() ).map_err( | e | format_err!( "{report:?}\n{e:#?}" ) )?;
  let name = package.name().map_err( | e | format_err!( "{report:?}\n{e:#?}" ) )?;
  report.name = Some( name.into() );
  let package_version = package.version().map_err( | e | format_err!( "{report:?}\n{e:#?}" ) )?;
  let current_version = version ::Version ::try_from( package_version.as_str() ).map_err( | e | format_err!( "{report:?}\n{e:#?}" ) )?;
  if current_version > o.new_version
  {
   return Err( format_err!
   (
  "{report:?}\nThe current version of the package is higher than need to be set\n\tpackage: {name}\n\tcurrent_version: {current_version}\n\tnew_version: {}",
  o.new_version
 ));
 }
  report.old_version = Some( o.old_version.to_string() );
  report.new_version = Some( o.new_version.to_string() );

  let mut package_manifest = package.manifest().map_err( | e | format_err!( "{report:?}\n{e:#?}" ) )?;
  if !o.dry
  {
   // let data = package_manifest.data.as_mut().unwrap();
   let data = &mut package_manifest.data;
   data[ "package" ][ "version" ] = value( o.new_version.to_string() );
   package_manifest.store()?;
 }
  report.changed_files = vec![ manifest_file ];
  let new_version = &o.new_version.to_string();
  for dep in &o.dependencies
  {
   // let manifest_file = dep.absolute_path().join( "Cargo.toml" );
   let manifest_file = dep.clone().manifest_file();
   let mut manifest = Manifest ::try_from( manifest_file.clone() ).map_err( | e | format_err!( "{report:?}\n{e:#?}" ) )?;
   // let data = manifest.data.as_mut().unwrap();
   let data = &mut manifest.data;
   let item = if let Some( item ) = data.get_mut( "package" ) { item }
   else if let Some( item ) = data.get_mut( "workspace" ) { item }
   else
   { return Err( format_err!( "{report:?}\nThe manifest nor the package and nor the workspace" ) ); };
   // Fix(issue-001): Check all 3 dependency sections, not just [dependencies]
   // Root cause: Original code missed dev-dependencies and build-dependencies
   // Pitfall: Cargo has 3 dependency sections - all must be updated for consistency

   for section in [ "dependencies", "dev-dependencies", "build-dependencies" ]
   {
  if let Some( dependency ) = item.get_mut( section ).and_then( | ds | ds.get_mut( name ) )
  {
   if let Some( previous_version ) = dependency.get( "version" ).and_then( | v | v.as_str() ).map( std ::string ::ToString ::to_string )
   {
    // Preserve version operator prefix (~, ^, =)
    let (operator, _) = if let Some( stripped ) = previous_version.strip_prefix('~')
    {
   ("~", stripped)
  }
    else if let Some( stripped ) = previous_version.strip_prefix('^')
    {
   ("^", stripped)
  }
    else if let Some( stripped ) = previous_version.strip_prefix('=')
    {
   ("=", stripped)
  }
    else
    {
   ("", previous_version.as_str())
  };

    dependency[ "version" ] = value( format!( "{operator}{new_version}" ) );
  }
 }
 }
   if !o.dry 
   { manifest.store().map_err( | e | format_err!( "{report:?}\n{e:#?}" ) )?; }
   report.changed_files.push( manifest_file );
 }

  Ok( report )
 }

  /// Reverts the version of a package in the provided `ExtendedBumpReport`.
  ///
  /// # Arguments
  ///
  /// * `report` - The `ExtendedBumpReport` containing the bump information.
  ///
  /// # Returns
  ///
  /// Returns `Ok(())` if the version is reverted successfully. Returns `Err` with an error message if there is any issue with reverting the version.
  ///
  /// # Errors
  /// qqq: doc
  ///
  /// # Panics
  /// qqq: doc
  // qqq: don't use 1-prameter Result
  pub fn revert( report: &ExtendedBumpReport ) -> error ::untyped ::Result< () > // qqq: use typed error
  {
  let Some( name ) = report.name.as_ref() else { return Ok( () ) };
  let Some( old_version ) = report.old_version.as_ref() else { return Ok( () ) };
  let Some( new_version ) = report.new_version.as_ref() else { return Ok( () ) };

  // Fix(issue-001): Check all 3 dependency sections to match bump() behavior
  // Root cause: Original code only reverted [dependencies], missing dev/build-dependencies
  // Pitfall: Revert must mirror bump operations exactly to maintain consistency
  let dependencies = | item_maybe_with_dependencies: &mut toml_edit ::Item |
  {
   for section in [ "dependencies", "dev-dependencies", "build-dependencies" ]
   {
  if let Some( dependency ) = item_maybe_with_dependencies.get_mut( section ).and_then( | ds | ds.get_mut( name ) )
  {
   if let Some( current_version ) = dependency.get( "version" ).and_then( | v | v.as_str() ).map( std ::string ::ToString ::to_string )
   {
    let version = &mut dependency[ "version" ];

    // Preserve version operator prefix (~, ^, =) to match bump() behavior
    let (operator, version_without_prefix) = if let Some( v ) = current_version.strip_prefix( '~' )
    {
   ("~", v)
  }
    else if let Some( v ) = current_version.strip_prefix( '^' )
    {
   ("^", v)
  }
    else if let Some( v ) = current_version.strip_prefix( '=' )
    {
   ("=", v)
  }
    else
    {
   ("", current_version.as_str())
  };

    if version_without_prefix != new_version
    {
   return Err( format_err!
   (
    "The current version of the package does not match the expected one. Expected: `{new_version}` Current: `{}`",
    version.as_str().unwrap_or_default()
  ));
  }

    *version = value( format!( "{operator}{old_version}" ) );
  }
 }
 }

   Ok( () )
 };

  for path in &report.changed_files
  {
   let mut manifest = Manifest ::try_from( path.clone() )?;
   let data = manifest.data();
   if let Some( workspace ) = data.get_mut( "workspace" )
   {
  dependencies( workspace )?;
 }
   if let Some( package ) = data.get_mut( "package" )
   {
  if package.get_mut( "name" ).unwrap().as_str().unwrap() == name
  {
   let version = &mut package[ "version" ];
   if version.as_str().unwrap() != new_version
   {
  return Err( format_err!
  (
   "The current version of the package does not match the expected one. Expected: `{new_version}` Current: `{}`",
   version.as_str().unwrap_or_default()
 ));
 }
   *version = value( old_version.clone() );
 }
  else
  {
   dependencies( package )?;
 }
 }
   manifest.store()?;
 }

  Ok( () )
 }

  // qqq: for Bohdan: not used? why is it needed?
  /// Bump version by manifest.
  /// It takes data from the manifest and increments the version number according to the semantic versioning scheme.
  /// It then writes the updated manifest file back to the same path, unless the flag is set to true, in which case it only returns the new version number as a string.
  ///
  /// # Args :
  /// - `manifest` - a manifest mutable reference
  /// - `dry` - a flag that indicates whether to apply the changes or not
  /// - `true` - does not modify the manifest file, but only returns the new version;
  /// - `false` - overwrites the manifest file with the new version.
  ///
  /// # Returns :
  /// - `Ok` - the new version number as a string;
  /// - `Err` - if the manifest file cannot be read, written, parsed.
  ///
  /// # Errors
  /// qqq: doc
  ///
  /// # Panics
  /// qqq: doc
  pub fn manifest_bump( manifest: &mut Manifest, dry: bool ) -> Result< BumpReport, manifest ::ManifestError >
  {
  let mut report = BumpReport ::default();

  let version=
  {
   let data = &manifest.data;
   if !manifest.package_is()
   {
  return Err( manifest ::ManifestError ::NotAPackage );
 }
   let package = data.get( "package" ).unwrap();

   let version = package.get( "version" );
   if version.is_none()
   {
  return Err( manifest ::ManifestError ::CannotFindValue( "version".into() ) );
 }
   let version = version.unwrap().as_str().unwrap();
   report.name = Some( package[ "name" ].as_str().unwrap().to_string() );
   report.old_version = Some( version.to_string() );

   Version ::from_str( version ).map_err( | e | manifest ::ManifestError ::InvalidValue( e.to_string() ) )?
 };

  let new_version = version.bump().to_string();
  report.new_version = Some( new_version.clone() );

  if !dry
  {
   let data = &mut manifest.data;
   data[ "package" ][ "version" ] = value( &new_version );
   manifest.store()?;
 }

  Result ::Ok( report )
 }
}

//

crate ::mod_interface!
{
  /// Version entity.
  exposed use Version;

  /// Report for bump operation.
  own use BumpReport;

  /// Options for version bumping.
  own use BumpOptions;
  /// Report about a changing version with list of files that was changed.
  own use ExtendedBumpReport;

  /// Bumps the version of a package and its dependencies.
  own use manifest_bump;
  /// Bump version.
  own use bump;

  /// Reverts the version of a package.
  own use revert;
}