refactor(embedded): Decouple package name sanitization

This commit is contained in:
Ed Page 2023-06-13 08:54:15 -05:00
parent 0af1d21bc4
commit 1df0ce5583

View File

@ -73,12 +73,20 @@ fn write(
) -> CargoResult<std::path::PathBuf> { ) -> CargoResult<std::path::PathBuf> {
let hash = hash(script).to_string(); let hash = hash(script).to_string();
assert_eq!(hash.len(), 64); assert_eq!(hash.len(), 64);
let file_name = script
.path
.file_stem()
.ok_or_else(|| anyhow::format_err!("no file name"))?
.to_string_lossy();
let name = sanitize_package_name(file_name.as_ref());
let mut workspace_root = target_dir.to_owned(); let mut workspace_root = target_dir.to_owned();
workspace_root.push("eval"); workspace_root.push("eval");
workspace_root.push(&hash[0..2]); workspace_root.push(&hash[0..2]);
workspace_root.push(&hash[2..4]); workspace_root.push(&hash[2..4]);
workspace_root.push(&hash[4..]); workspace_root.push(&hash[4..]);
workspace_root.push(package_name(script)?); workspace_root.push(name);
std::fs::create_dir_all(&workspace_root).with_context(|| { std::fs::create_dir_all(&workspace_root).with_context(|| {
format!( format!(
"failed to create temporary workspace at {}", "failed to create temporary workspace at {}",
@ -126,7 +134,12 @@ fn expand_manifest_(script: &RawScript, config: &Config) -> CargoResult<toml::Ta
anyhow::bail!("`package.{key}` is not allowed in embedded manifests") anyhow::bail!("`package.{key}` is not allowed in embedded manifests")
} }
} }
let name = package_name(script)?; let file_name = script
.path
.file_stem()
.ok_or_else(|| anyhow::format_err!("no file name"))?
.to_string_lossy();
let name = sanitize_package_name(file_name.as_ref());
let hash = hash(script); let hash = hash(script);
let bin_name = format!("{name}_{hash}"); let bin_name = format!("{name}_{hash}");
package package
@ -179,12 +192,7 @@ fn expand_manifest_(script: &RawScript, config: &Config) -> CargoResult<toml::Ta
Ok(manifest) Ok(manifest)
} }
fn package_name(script: &RawScript) -> CargoResult<String> { fn sanitize_package_name(name: &str) -> String {
let name = script
.path
.file_stem()
.ok_or_else(|| anyhow::format_err!("no file name"))?
.to_string_lossy();
let mut slug = String::new(); let mut slug = String::new();
for (i, c) in name.chars().enumerate() { for (i, c) in name.chars().enumerate() {
match (i, c) { match (i, c) {
@ -204,7 +212,7 @@ fn package_name(script: &RawScript) -> CargoResult<String> {
} }
} }
} }
Ok(slug) slug
} }
fn hash(script: &RawScript) -> blake3::Hash { fn hash(script: &RawScript) -> blake3::Hash {