griffin_core/pallas_primitives/
framework.rs

1// pub type Error = Box<dyn std::error::Error>;
2
3use crate::pallas_codec::minicbor::{decode, to_vec, Decode, Encode};
4
5// no std:
6pub type Error = Box<dyn core::error::Error>;
7use alloc::{boxed::Box, string::ToString, vec::Vec};
8
9pub trait Fragment<'a>
10where
11    Self: Sized,
12{
13    fn encode_fragment(&self) -> Result<Vec<u8>, Error>;
14    fn decode_fragment(bytes: &'a [u8]) -> Result<Self, Error>;
15}
16
17impl<'a, T> Fragment<'a> for T
18where
19    T: Encode<()> + Decode<'a, ()> + Sized,
20{
21    fn encode_fragment(&self) -> Result<Vec<u8>, Error> {
22        to_vec(self).map_err(|e| (e.to_string()).into())
23    }
24
25    fn decode_fragment(bytes: &'a [u8]) -> Result<Self, Error> {
26        decode(bytes).map_err(|e| (e.to_string()).into())
27    }
28}
29
30#[cfg(feature = "json")]
31pub trait ToCanonicalJson {
32    fn to_json(&self) -> serde_json::Value;
33}