use super::{DidTemplate, TemplateError};
pub const BUILTIN_NAMES: &[&str] = &[
"did-host-didcomm",
"did-host-http",
"did-host-http-didcomm",
"didcomm-mediator",
"push-gateway",
"vta-admin",
"vtc-host",
];
const LEGACY_ALIASES: &[(&str, &str)] = &[
("webvh-control", "did-host-http-didcomm"),
("webvh-daemon", "did-host-http"),
("webvh-server", "did-host-didcomm"),
("did-hosting-control", "did-host-http-didcomm"),
("did-hosting-daemon", "did-host-http"),
("did-hosting-server", "did-host-didcomm"),
];
const DIDCOMM_MEDIATOR: &str = include_str!("../../templates/didcomm-mediator.json");
const PUSH_GATEWAY: &str = include_str!("../../templates/push-gateway.json");
const VTA_ADMIN: &str = include_str!("../../templates/vta-admin.json");
const VTC_HOST: &str = include_str!("../../templates/vtc-host.json");
const DID_HOST_HTTP_DIDCOMM: &str = include_str!("../../templates/did-host-http-didcomm.json");
const DID_HOST_HTTP: &str = include_str!("../../templates/did-host-http.json");
const DID_HOST_DIDCOMM: &str = include_str!("../../templates/did-host-didcomm.json");
fn resolve_alias(name: &str) -> &str {
for (old, new) in LEGACY_ALIASES {
if name == *old {
return new;
}
}
name
}
pub fn load_embedded(name: &str) -> Result<DidTemplate, TemplateError> {
let canonical = resolve_alias(name);
let raw = match canonical {
"didcomm-mediator" => DIDCOMM_MEDIATOR,
"push-gateway" => PUSH_GATEWAY,
"vta-admin" => VTA_ADMIN,
"vtc-host" => VTC_HOST,
"did-host-http-didcomm" => DID_HOST_HTTP_DIDCOMM,
"did-host-http" => DID_HOST_HTTP,
"did-host-didcomm" => DID_HOST_DIDCOMM,
_ => return Err(TemplateError::BuiltinNotFound(name.to_string())),
};
let value: serde_json::Value = serde_json::from_str(raw)?;
DidTemplate::from_json(value)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_builtin_parses_and_validates() {
for name in BUILTIN_NAMES {
let tpl = load_embedded(name)
.unwrap_or_else(|e| panic!("builtin '{name}' failed to load: {e}"));
assert_eq!(tpl.name, *name, "builtin name field must match lookup key");
}
}
#[test]
fn unknown_builtin_errors() {
let err = load_embedded("does-not-exist").unwrap_err();
assert!(matches!(err, TemplateError::BuiltinNotFound(_)));
}
#[test]
fn legacy_aliases_resolve_to_did_host_canonical() {
for (old, new) in [
("webvh-control", "did-host-http-didcomm"),
("webvh-daemon", "did-host-http"),
("webvh-server", "did-host-didcomm"),
("did-hosting-control", "did-host-http-didcomm"),
("did-hosting-daemon", "did-host-http"),
("did-hosting-server", "did-host-didcomm"),
] {
let tpl = load_embedded(old)
.unwrap_or_else(|e| panic!("legacy alias '{old}' failed to resolve: {e}"));
assert_eq!(
tpl.name, new,
"legacy alias '{old}' must surface the canonical name '{new}'"
);
}
}
}