mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-29 01:34:37 +01:00
refactor(dev): drop unused export naming
This commit is contained in:
1 parent
777ccdcc24
commit
e0d3f16477
2 files changed
+25
-40
No files matched your search
@@ -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<ExitCode> {
|
||||
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::<String>();
|
||||
// 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<String>,
|
||||
@@ -304,9 +313,20 @@ pub fn export(file: &str, out: &str, t: &Target) -> CliResult<ExitCode> {
|
||||
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<String, usize> = 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}");
|
||||
|
||||
@@ -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::<String>();
|
||||
// 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<String>,
|
||||
next_suffix: std::collections::HashMap<String, usize>,
|
||||
}
|
||||
|
||||
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<Value> {
|
||||
let name = string(p, "name").unwrap_or_else(|| if kind == FileKind::Board { "Untitled board" } else { "Untitled" }.into());
|
||||
|
||||
Reference in new issue
Block a user