Add some utility functions for displaying Term/PlutusData
This is useful with the blueprint stuff, where Term are often just plain PlutusData.
This commit is contained in:
parent
690e41261e
commit
051e9a6851
|
@ -208,6 +208,20 @@ impl<T> Term<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> TryInto<PlutusData> for Term<T> {
|
||||||
|
type Error = String;
|
||||||
|
|
||||||
|
fn try_into(self) -> Result<PlutusData, String> {
|
||||||
|
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<T>
|
impl<'a, T> Display for Term<T>
|
||||||
where
|
where
|
||||||
T: Binder<'a>,
|
T: Binder<'a>,
|
||||||
|
@ -245,6 +259,13 @@ pub struct Data {}
|
||||||
|
|
||||||
// TODO: See about moving these builders upstream to Pallas?
|
// TODO: See about moving these builders upstream to Pallas?
|
||||||
impl Data {
|
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 {
|
pub fn integer(i: BigInt) -> PlutusData {
|
||||||
match i.to_i64() {
|
match i.to_i64() {
|
||||||
Some(i) => PlutusData::BigInt(pallas::BigInt::Int(i.into())),
|
Some(i) => PlutusData::BigInt(pallas::BigInt::Int(i.into())),
|
||||||
|
|
Loading…
Reference in New Issue