From 051e9a685115531ace756c67f63db9bc625f70b8 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Sat, 19 Aug 2023 16:37:20 +0200 Subject: [PATCH] Add some utility functions for displaying Term/PlutusData This is useful with the blueprint stuff, where Term are often just plain PlutusData. --- crates/uplc/src/ast.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/uplc/src/ast.rs b/crates/uplc/src/ast.rs index 646ef411..df29c098 100644 --- a/crates/uplc/src/ast.rs +++ b/crates/uplc/src/ast.rs @@ -208,6 +208,20 @@ impl Term { } } +impl TryInto for Term { + type Error = String; + + fn try_into(self) -> Result { + match self { + Term::Constant(rc) => match &*rc { + Constant::Data(data) => Ok(data.to_owned()), + _ => Err("not a data".to_string()), + }, + _ => Err("not a data".to_string()), + } + } +} + impl<'a, T> Display for Term where T: Binder<'a>, @@ -245,6 +259,13 @@ pub struct Data {} // TODO: See about moving these builders upstream to Pallas? impl Data { + pub fn to_hex(data: PlutusData) -> String { + let mut bytes = Vec::new(); + pallas_codec::minicbor::Encoder::new(&mut bytes) + .encode(data) + .expect("failed to encode Plutus Data as cbor?"); + hex::encode(bytes) + } pub fn integer(i: BigInt) -> PlutusData { match i.to_i64() { Some(i) => PlutusData::BigInt(pallas::BigInt::Int(i.into())),