pub const DEFAULT_TEMPLATE: &str = "{name}.wasm";
pub fn normalize_name(name: &str) -> String {
name.replace('-', "_")
}
const FORBIDDEN_TOKENS: &[&str] = &["{os}", "{arch}", "{ext}"];
pub fn check_asset_template(template: &str) -> Result<(), String> {
for forbidden in FORBIDDEN_TOKENS {
if template.contains(forbidden) {
return Err(format!(
"asset template token '{}' is no longer supported in v0.2.0; \
plugins are distributed as single platform-independent .wasm files. \
Update the plugin's release to ship `<name>.wasm` and remove \
the `asset = \"...\"` line (or set it to `\"{{name}}.wasm\"`).",
forbidden
));
}
}
Ok(())
}
pub fn resolve_template(template: &str, plugin_name: &str) -> String {
template.replace("{name}", &normalize_name(plugin_name))
}
pub fn asset_filename(plugin_name: &str, custom_template: Option<&str>) -> String {
let template = custom_template.unwrap_or(DEFAULT_TEMPLATE);
resolve_template(template, plugin_name)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalize_name_replaces_hyphens() {
assert_eq!(normalize_name("git-status"), "git_status");
}
#[test]
fn normalize_name_no_hyphens() {
assert_eq!(normalize_name("simple"), "simple");
}
#[test]
fn resolve_default_template() {
let result = resolve_template(DEFAULT_TEMPLATE, "git-status");
assert_eq!(result, "git_status.wasm");
}
#[test]
fn resolve_custom_template() {
let result = resolve_template("yosh_{name}.wasm", "auto-env");
assert_eq!(result, "yosh_auto_env.wasm");
}
#[test]
fn asset_filename_uses_default() {
let result = asset_filename("my-plugin", None);
assert_eq!(result, "my_plugin.wasm");
}
#[test]
fn asset_filename_uses_custom() {
let result = asset_filename("my-plugin", Some("custom_{name}.wasm"));
assert_eq!(result, "custom_my_plugin.wasm");
}
#[test]
fn check_template_accepts_default() {
assert!(check_asset_template(DEFAULT_TEMPLATE).is_ok());
}
#[test]
fn check_template_accepts_custom_wasm() {
assert!(check_asset_template("plugin-{name}.wasm").is_ok());
}
#[test]
fn check_template_rejects_os_token() {
let err = check_asset_template("lib{name}-{os}-{arch}.{ext}").unwrap_err();
assert!(err.contains("{os}"));
assert!(err.contains("v0.2.0"));
}
#[test]
fn check_template_rejects_arch_token() {
let err = check_asset_template("plugin-{arch}.wasm").unwrap_err();
assert!(err.contains("{arch}"));
}
#[test]
fn check_template_rejects_ext_token() {
let err = check_asset_template("plugin.{ext}").unwrap_err();
assert!(err.contains("{ext}"));
}
}