diff --git a/CHANGELOG.md b/CHANGELOG.md index bd48932..81bfd9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Text in SVG `` elements now rendered in PNG sprites (see [#102]) - Support loading SVG files compressed with gzip (`*.svgz`; see [#107]) - Propagate errors in `get_svg_input_paths` (see [#108]) +- Use consuming builder pattern for `SpritesheetBuilder` (see [#109]) - Update Oxipng dependency from v9 to v10 (see [#106]) - Update thiserror dependency from v1 to v2 (see [#106]) @@ -14,6 +15,7 @@ The minimum supported version of Rust is now 1.85.1 (released March 2025). [#106]: https://github.com/flother/spreet/pull/106 [#107]: https://github.com/flother/spreet/pull/107 [#108]: https://github.com/flother/spreet/pull/108 +[#109]: https://github.com/flother/spreet/pull/109 ## v0.13.1 (2025-12-24) diff --git a/src/bin/spreet/main.rs b/src/bin/spreet/main.rs index 8de2687..e8c7dc6 100644 --- a/src/bin/spreet/main.rs +++ b/src/bin/spreet/main.rs @@ -48,15 +48,13 @@ fn main() { std::process::exit(exitcode::NOINPUT); } - let mut spritesheet_builder = Spritesheet::build(); - spritesheet_builder.sprites(sprites); - spritesheet_builder.spacing(args.spacing); + let mut spritesheet_builder = Spritesheet::build().sprites(sprites).spacing(args.spacing); if args.unique { - spritesheet_builder.make_unique(); - }; + spritesheet_builder = spritesheet_builder.make_unique(); + } if args.sdf { - spritesheet_builder.make_sdf(); - }; + spritesheet_builder = spritesheet_builder.make_sdf(); + } // Generate sprite sheet let Some(spritesheet) = spritesheet_builder.generate() else { diff --git a/src/sprite/mod.rs b/src/sprite/mod.rs index c7f0922..6e4a8c3 100644 --- a/src/sprite/mod.rs +++ b/src/sprite/mod.rs @@ -311,6 +311,7 @@ impl SpriteDescription { /// Builder pattern for `Spritesheet`: construct a `Spritesheet` object using calls to a builder /// helper. #[derive(Default, Clone)] +#[must_use = "builder does nothing unless you call .generate()"] pub struct SpritesheetBuilder { sprites: Option>, references: Option>, @@ -328,20 +329,20 @@ impl SpritesheetBuilder { } } - pub fn sprites(&mut self, sprites: BTreeMap) -> &mut Self { + pub fn sprites(mut self, sprites: BTreeMap) -> Self { self.sprites = Some(sprites); self } /// Set the spacing (in pixels) to add to the right and bottom of each sprite. - pub fn spacing(&mut self, spacing: u8) -> &mut Self { + pub fn spacing(mut self, spacing: u8) -> Self { self.spacing = spacing; self } // Remove any duplicate sprites from the spritesheet's sprites. This is used to let spritesheets // include only unique sprites, with multiple references to the same sprite in the index file. - pub fn make_unique(&mut self) -> &mut Self { + pub fn make_unique(mut self) -> Self { match self.sprites.take() { Some(sprites) => { let mut unique_sprites = BTreeMap::new(); @@ -373,7 +374,7 @@ impl SpritesheetBuilder { /// /// You have to ensure that the sprites are created as an SDF file beforehand. See /// [`Sprite::new_sdf`] for further context. - pub fn make_sdf(&mut self) -> &mut Self { + pub fn make_sdf(mut self) -> Self { self.sdf = true; self }