From fa3c88a31e507777ca6e48c681a256599771d55a Mon Sep 17 00:00:00 2001 From: rvcas Date: Thu, 4 Aug 2022 12:48:09 -0400 Subject: [PATCH] feat: add from_cbor and from_hex --- crates/uplc/src/flat.rs | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/crates/uplc/src/flat.rs b/crates/uplc/src/flat.rs index 4ee52881..534e6051 100644 --- a/crates/uplc/src/flat.rs +++ b/crates/uplc/src/flat.rs @@ -30,10 +30,29 @@ impl<'b, T> Program where T: Binder<'b> + Debug, { - // convenient so that people don't need to depend on the flat crate - // directly to call programs flat function - pub fn to_flat(&self) -> Result, en::Error> { - self.flat() + pub fn from_cbor(bytes: &'b [u8], buffer: &'b mut Vec) -> Result { + let flat_bytes: Vec = + minicbor::decode(bytes).map_err(|err| de::Error::Message(err.to_string()))?; + + buffer.extend(flat_bytes); + + Self::unflat(buffer) + } + + pub fn from_flat(bytes: &'b [u8]) -> Result { + Self::unflat(bytes) + } + + pub fn from_hex( + hex_str: &str, + cbor_buffer: &'b mut Vec, + flat_buffer: &'b mut Vec, + ) -> Result { + let cbor_bytes = hex::decode(hex_str).map_err(|err| de::Error::Message(err.to_string()))?; + + cbor_buffer.extend(cbor_bytes); + + Self::from_cbor(cbor_buffer, flat_buffer) } pub fn to_cbor(&self) -> Result, en::Error> { @@ -42,6 +61,12 @@ where minicbor::to_vec(&flat_bytes).map_err(|err| en::Error::Message(err.to_string())) } + // convenient so that people don't need to depend on the flat crate + // directly to call programs flat function + pub fn to_flat(&self) -> Result, en::Error> { + self.flat() + } + pub fn to_hex(&self) -> Result { let bytes = self.to_cbor()?; @@ -49,10 +74,6 @@ where Ok(hex) } - - pub fn from_flat(bytes: &'b [u8]) -> Result { - Self::unflat(bytes) - } } impl<'b, T> Encode for Program