From e0d3f16477a06736bb80f29f7e17a415f736a232 Mon Sep 17 00:00:00 2001 From: ATOM Date: Wed, 25 Nov 2026 09:39:40 -0600 Subject: [PATCH] refactor(dev): drop unused export naming --- apps/figcraft-cli/src/files.rs | 30 ++++++++++++++++++++++++----- crates/engine/src/cmd/file.rs | 35 ---------------------------------- 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/apps/figcraft-cli/src/files.rs b/apps/figcraft-cli/src/files.rs index a916e4ecb..5f4a006f4 100644 --- a/apps/figcraft-cli/src/files.rs +++ b/apps/figcraft-cli/src/files.rs @@ -5,9 +5,6 @@ use std::path::Path; use std::process::ExitCode; use figcraft_doc::{Document, ExportFormat, ExportSetting, NodeId, NodeKind, PrintSettings, VdCompat}; -use figcraft_engine::cmd::file::ExportNames; -#[cfg(test)] -use figcraft_engine::cmd::file::export_file_stem as sanitize; use figcraft_engine::{BatchScope, BatchStep, Session}; use serde_json::{Value, json}; @@ -239,6 +236,18 @@ pub fn info(path: &str, as_json: bool) -> CliResult { Ok(ExitCode::SUCCESS) } +/// The file name a layer exports to: the app's rule ([`figcraft_engine::cmd::file::safe_file_name`], +/// so `/` becomes `_` too), without leading or trailing spaces and dots, at most 100 characters +/// and 200 bytes (file systems allow 255, and a number and the extension still fit). +fn sanitize(name: &str) -> String { + let s = figcraft_engine::cmd::file::safe_file_name(name); + let s = s.trim_matches(|c: char| c == '.' || c.is_whitespace()); + let cap = |s: &str| s.char_indices().take(100).take_while(|(i, c)| i + c.len_utf8() <= 200).map(|(_, c)| c).collect::(); + // Truncation can expose a device name; its escape must still fit the limits. + let s = figcraft_engine::file_access::escape_device_names(&cap(s)); + if s.is_empty() { "layer".into() } else { cap(&s) } +} + /// What `export` exports and how. pub struct Target { pub format: Option, @@ -304,9 +313,20 @@ pub fn export(file: &str, out: &str, t: &Target) -> CliResult { if ids.is_empty() { return Err(CliError::Failed("the page is empty: nothing to export".into())); } - let mut names = ExportNames::default(); + let mut used = std::collections::BTreeSet::new(); + let mut next_suffix: std::collections::HashMap = std::collections::HashMap::new(); for id in ids { - let name = names.push(doc.node(id).map(|n| n.name.as_str()).unwrap_or("layer")); + let base = sanitize(doc.node(id).map(|n| n.name.as_str()).unwrap_or("layer")); + let base_lower = base.to_lowercase(); + let next = next_suffix.entry(base_lower).or_insert(1); + let name = loop { + let k = *next; + *next += 1; + let candidate = if k == 1 { base.clone() } else { format!("{base} {k}") }; + if used.insert(candidate.to_lowercase()) { + break candidate; + } + }; let path = Path::new(out).join(format!("{name}.{}", fmt.ext())).to_string_lossy().into_owned(); write(&path, &bytes_of(&[id])?)?; println!("{path}"); diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index fe562359a..0c84f3458 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -47,41 +47,6 @@ pub use version::MAX_AUTOSAVES; pub(crate) use version::{add_version, keep_in_history, read_version, unique_id}; use version::{autosave, restore_version, save_version, version_index, version_json, version_list}; -/// The file base a layer exports to, shared by filesystem export and generated asset references. -/// Preserves case and ordinary spaces, strips leading/trailing dots and whitespace, and caps at -/// 100 characters and 200 bytes so a number and extension still fit common filesystems. -pub fn export_file_stem(name: &str) -> String { - let s = safe_file_name(name); - let s = s.trim_matches(|c: char| c == '.' || c.is_whitespace()); - let cap = |s: &str| s.char_indices().take(100).take_while(|(i, c)| i + c.len_utf8() <= 200).map(|(_, c)| c).collect::(); - // Truncation can expose a device name; its escape must still fit the limits. - let s = crate::file_access::escape_device_names(&cap(s)); - if s.is_empty() { "layer".into() } else { cap(&s) } -} - -/// Case-insensitively unique export file bases, using the exporter's `" 2"`, `" 3"` suffixes. -#[derive(Default)] -pub struct ExportNames { - used: std::collections::BTreeSet, - next_suffix: std::collections::HashMap, -} - -impl ExportNames { - /// Resolve the next layer name exactly as the filesystem exporter does. - pub fn push(&mut self, layer_name: &str) -> String { - let base = export_file_stem(layer_name); - let next = self.next_suffix.entry(base.to_lowercase()).or_insert(1); - loop { - let k = *next; - *next += 1; - let candidate = if k == 1 { base.clone() } else { format!("{base} {k}") }; - if self.used.insert(candidate.to_lowercase()) { - return candidate; - } - } - } -} - /// A new file of `kind` in a tab of its own. fn new_file(s: &mut Session, p: &Value, kind: FileKind) -> Result { let name = string(p, "name").unwrap_or_else(|| if kind == FileKind::Board { "Untitled board" } else { "Untitled" }.into());