homestar_invocation/ipld/
dag_cbor.rs1use crate::{consts::DAG_CBOR, Error, Unit};
4use libipld::{
5 cbor::DagCborCodec,
6 json::DagJsonCodec,
7 multihash::{Code, MultihashDigest},
8 prelude::{Codec, Decode},
9 Cid, Ipld,
10};
11use std::{
12 fs,
13 io::{Cursor, Write},
14};
15
16pub trait DagCbor
18where
19 Self: Sized,
20 Ipld: From<Self>,
21{
22 fn to_cid(self) -> Result<Cid, Error<Unit>> {
24 let ipld: Ipld = self.into();
25 let bytes = DagCborCodec.encode(&ipld)?;
26 let hash = Code::Sha3_256.digest(&bytes);
27 Ok(Cid::new_v1(DAG_CBOR, hash))
28 }
29
30 fn to_dag_json(self) -> Result<Vec<u8>, Error<Unit>> {
32 let ipld: Ipld = self.into();
33 Ok(DagJsonCodec.encode(&ipld)?)
34 }
35
36 fn to_dagjson_string(self) -> Result<String, Error<Unit>> {
38 let encoded = self.to_dag_json()?;
39 let s = std::str::from_utf8(&encoded)?;
41 Ok(s.to_string())
42 }
43
44 fn to_cbor(self) -> Result<Vec<u8>, Error<Unit>> {
46 let ipld: Ipld = self.into();
47 Ok(DagCborCodec.encode(&ipld)?)
48 }
49
50 fn from_cbor(data: &[u8]) -> Result<Self, Error<Unit>>
52 where
53 Self: TryFrom<Ipld>,
54 {
55 let ipld = Ipld::decode(DagCborCodec, &mut Cursor::new(data))?;
56 let from_ipld = Self::try_from(ipld).map_err(|_err| {
57 Error::<Unit>::UnexpectedIpldType(Ipld::String(
58 "Failed to convert Ipld to expected type".to_string(),
59 ))
60 })?;
61 Ok(from_ipld)
62 }
63
64 fn to_cbor_file(self, filename: String) -> Result<(), Error<Unit>> {
66 Ok(fs::File::create(filename)?.write_all(&self.to_cbor()?)?)
67 }
68}
69
70pub trait DagCborRef
72where
73 Self: Sized,
74 for<'a> Ipld: From<&'a Self>,
75{
76 fn to_cid(&self) -> Result<Cid, Error<Unit>> {
78 let ipld: Ipld = self.into();
79 let bytes = DagCborCodec.encode(&ipld)?;
80 let hash = Code::Sha3_256.digest(&bytes);
81 Ok(Cid::new_v1(DAG_CBOR, hash))
82 }
83}