griffin-core 0.3.0

UTXO framework for Substrate and Polkadot.
Documentation
// pub type Error = Box<dyn std::error::Error>;

use crate::pallas_codec::minicbor::{decode, to_vec, Decode, Encode};

// no std:
pub type Error = Box<dyn core::error::Error>;
use alloc::{boxed::Box, string::ToString, vec::Vec};

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: Encode<()> + Decode<'a, ()> + Sized,
{
    fn encode_fragment(&self) -> Result<Vec<u8>, Error> {
        to_vec(self).map_err(|e| (e.to_string()).into())
    }

    fn decode_fragment(bytes: &'a [u8]) -> Result<Self, Error> {
        decode(bytes).map_err(|e| (e.to_string()).into())
    }
}

#[cfg(feature = "json")]
pub trait ToCanonicalJson {
    fn to_json(&self) -> serde_json::Value;
}