Skip to main content

pallas_primitives/
framework.rs

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