use crate::*;
use ts_rs::TS;
pub struct HoloHashTs;
impl TS for HoloHashTs {
type WithoutGenerics = Self;
type OptionInnerType = Self;
fn name(_: &ts_rs::Config) -> String {
"HoloHash".into()
}
fn inline(cfg: &ts_rs::Config) -> String {
Self::name(cfg)
}
fn decl(_: &ts_rs::Config) -> String {
"type HoloHash = Uint8Array;".into()
}
fn decl_concrete(cfg: &ts_rs::Config) -> String {
Self::decl(cfg)
}
fn output_path() -> Option<std::path::PathBuf> {
Some("types.ts".into())
}
}
macro_rules! holo_hash_ts {
($hash_type:ty, $name:literal) => {
impl TS for HoloHash<$hash_type> {
type WithoutGenerics = Self;
type OptionInnerType = Self;
fn name(_: &ts_rs::Config) -> String {
$name.into()
}
fn inline(cfg: &ts_rs::Config) -> String {
Self::name(cfg)
}
fn decl(_: &ts_rs::Config) -> String {
concat!("type ", $name, " = HoloHash;").into()
}
fn decl_concrete(cfg: &ts_rs::Config) -> String {
Self::decl(cfg)
}
fn visit_dependencies(v: &mut impl ts_rs::TypeVisitor)
where
Self: 'static,
{
v.visit::<HoloHashTs>();
}
fn output_path() -> Option<std::path::PathBuf> {
Some("types.ts".into())
}
}
};
}
holo_hash_ts!(hash_type::Agent, "AgentPubKey");
holo_hash_ts!(hash_type::Dna, "DnaHash");
holo_hash_ts!(hash_type::Wasm, "WasmHash");
holo_hash_ts!(hash_type::Entry, "EntryHash");
holo_hash_ts!(hash_type::Action, "ActionHash");
holo_hash_ts!(hash_type::External, "ExternalHash");
holo_hash_ts!(hash_type::DhtOp, "DhtOpHash");
holo_hash_ts!(hash_type::Warrant, "WarrantHash");
holo_hash_ts!(hash_type::AnyDht, "AnyDhtHash");
holo_hash_ts!(hash_type::AnyLinkable, "AnyLinkableHash");
holo_hash_ts!(hash_type::Inline, "InlineHash");
#[macro_export]
macro_rules! ts_alias {
($marker:ident, $name:literal, $rhs:literal, $file:literal, deps: [$($dep:ty),* $(,)?]) => {
#[doc(hidden)]
pub struct $marker;
impl ts_rs::TS for $marker {
type WithoutGenerics = Self;
type OptionInnerType = Self;
fn name(_: &ts_rs::Config) -> String {
$name.into()
}
fn inline(cfg: &ts_rs::Config) -> String {
<Self as ts_rs::TS>::name(cfg)
}
fn decl(_: &ts_rs::Config) -> String {
concat!("type ", $name, " = ", $rhs, ";").into()
}
fn decl_concrete(cfg: &ts_rs::Config) -> String {
<Self as ts_rs::TS>::decl(cfg)
}
#[allow(
unused_variables,
reason = "`v` goes unused when a `ts_alias!` invocation lists no deps; \
cross-crate callers are exempt from this lint automatically, \
but same-crate callers (e.g. within holo_hash itself) are not"
)]
fn visit_dependencies(v: &mut impl ts_rs::TypeVisitor)
where
Self: 'static,
{
$(v.visit::<$dep>();)*
}
fn output_path() -> Option<std::path::PathBuf> {
Some($file.into())
}
}
};
}
pub fn export_ts_bindings(cfg: &ts_rs::Config) -> Result<(), ts_rs::ExportError> {
HoloHashTs::export_all(cfg)?;
crate::AgentPubKey::export_all(cfg)?;
crate::DnaHash::export_all(cfg)?;
crate::WasmHash::export_all(cfg)?;
crate::EntryHash::export_all(cfg)?;
crate::ActionHash::export_all(cfg)?;
crate::ExternalHash::export_all(cfg)?;
crate::DhtOpHash::export_all(cfg)?;
crate::WarrantHash::export_all(cfg)?;
crate::AnyDhtHash::export_all(cfg)?;
crate::AnyLinkableHash::export_all(cfg)?;
crate::InlineHash::export_all(cfg)?;
#[cfg(feature = "encoding")]
{
crate::DnaHashB64Ts::export_all(cfg)?;
crate::WasmHashB64Ts::export_all(cfg)?;
}
Ok(())
}
#[cfg(all(test, feature = "ts_rs"))]
mod ts_export {
use super::*;
#[test]
fn hash_decls_have_expected_shape() {
let cfg = ts_rs::Config::from_env();
assert_eq!(HoloHashTs::decl(&cfg), "type HoloHash = Uint8Array;");
assert_eq!(
crate::AgentPubKey::decl(&cfg),
"type AgentPubKey = HoloHash;"
);
}
}