From 52288bdf6c44a03e3c13bdcb327a6f35425d30cd Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Fri, 11 Oct 2024 21:22:56 +0200 Subject: [PATCH] fix blueprint id generation overwriting old blueprints if any were removed before --- src/blueprint.rs | 4 ++++ src/editor.rs | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/blueprint.rs b/src/blueprint.rs index 3c99933..3bdd4b0 100644 --- a/src/blueprint.rs +++ b/src/blueprint.rs @@ -27,6 +27,10 @@ impl Blueprint { } } + pub fn id(&self)->&String{ + &self.id + } + pub fn convert_board(&mut self) -> &Board { if self.tile_board.is_none() { self.tile_board = Some(Board::parse(&self.board)); diff --git a/src/editor.rs b/src/editor.rs index 17de297..8dead71 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -280,7 +280,17 @@ impl Editor { } } board.trim_size(0); - let mut blueprint = Blueprint::new(&board, self.blueprints.len()); + let mut id = 0; + 'outer: loop { + for b in &self.blueprints { + if b.id() == &format!("blueprint_{id}") { + id += 1; + continue 'outer; + } + } + break; + } + let mut blueprint = Blueprint::new(&board, id); if !self.new_blueprint_name.is_empty() { blueprint.name = self.new_blueprint_name.clone(); }