feat(alonzo): small ergonomic improvements to lib api
This commit is contained in:
parent
b740aaf968
commit
0c46117f84
4 changed files with 38 additions and 5 deletions
|
|
@ -29,7 +29,7 @@ pub fn hash_plutus_data(data: &PlutusData) -> Result<Hash32, Error> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::BlockWrapper;
|
||||
use crate::{BlockWrapper, Fragment};
|
||||
|
||||
use super::hash_transaction;
|
||||
|
||||
|
|
@ -40,7 +40,7 @@ mod tests {
|
|||
let block_str = include_str!("test_data/test1.block");
|
||||
|
||||
let block_bytes = hex::decode(block_str).expect(&format!("bad block file {}", block_idx));
|
||||
let block_model: BlockWrapper = minicbor::decode(&block_bytes[..])
|
||||
let block_model = BlockWrapper::decode_fragment(&block_bytes[..])
|
||||
.expect(&format!("error decoding cbor for file {}", block_idx));
|
||||
|
||||
let valid_hashes = vec![
|
||||
|
|
|
|||
22
pallas-alonzo/src/framework.rs
Normal file
22
pallas-alonzo/src/framework.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
pub type Error = Box<dyn std::error::Error>;
|
||||
|
||||
pub trait Fragment<'a>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
fn encode_fragment(&self) -> Result<Vec<u8>, Error>;
|
||||
fn decode_fragment(bytes: &'a [u8]) -> Result<Self, Error>;
|
||||
}
|
||||
|
||||
impl<'a, T> Fragment<'a> for T
|
||||
where
|
||||
T: minicbor::Encode + minicbor::Decode<'a> + Sized,
|
||||
{
|
||||
fn encode_fragment(&self) -> Result<Vec<u8>, Error> {
|
||||
minicbor::to_vec(self).map_err(|e| e.into())
|
||||
}
|
||||
|
||||
fn decode_fragment(bytes: &'a [u8]) -> Result<Self, Error> {
|
||||
minicbor::decode(bytes).map_err(|e| e.into())
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,7 @@
|
|||
mod model;
|
||||
mod framework;
|
||||
|
||||
pub use framework::*;
|
||||
pub use model::*;
|
||||
|
||||
#[cfg(feature = "crypto")]
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
use log::warn;
|
||||
use minicbor::{bytes::ByteVec, data::Tag};
|
||||
use minicbor_derive::{Decode, Encode};
|
||||
use std::collections::BTreeMap;
|
||||
use std::{collections::BTreeMap, ops::Deref};
|
||||
|
||||
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord)]
|
||||
pub struct SkipCbor<const N: usize> {}
|
||||
|
|
@ -1092,6 +1092,14 @@ impl minicbor::Encode for Metadatum {
|
|||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Metadata(Vec<(Metadatum, Metadatum)>);
|
||||
|
||||
impl Deref for Metadata {
|
||||
type Target = Vec<(Metadatum, Metadatum)>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b> minicbor::decode::Decode<'b> for Metadata {
|
||||
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
|
||||
let items: Result<Vec<_>, _> = d.map_iter::<Metadatum, Metadatum>()?.collect();
|
||||
|
|
@ -1202,7 +1210,7 @@ pub struct BlockWrapper(#[n(0)] pub u16, #[n(1)] pub Block);
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::BlockWrapper;
|
||||
use crate::{BlockWrapper, Fragment};
|
||||
use minicbor::{self, to_vec};
|
||||
|
||||
#[test]
|
||||
|
|
@ -1221,7 +1229,7 @@ mod tests {
|
|||
for (idx, block_str) in test_blocks.iter().enumerate() {
|
||||
//println!("decoding test block {}", idx + 1);
|
||||
let bytes = hex::decode(block_str).expect(&format!("bad block file {}", idx));
|
||||
let block: BlockWrapper = minicbor::decode(&bytes[..])
|
||||
let block = BlockWrapper::decode_fragment(&bytes[..])
|
||||
.expect(&format!("error decoding cbor for file {}", idx));
|
||||
let bytes2 =
|
||||
to_vec(block).expect(&format!("error encoding block cbor for file {}", idx));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue