ipld_dagpb/
lib.rs

1//! Protobuf codec.
2#![deny(missing_docs)]
3
4use std::io::{BufRead, Write};
5
6mod codec;
7mod error;
8
9use bytes::Bytes;
10use ipld_core::{
11    cid::Cid,
12    codec::{Codec, Links},
13    ipld::Ipld,
14};
15
16use crate::codec::PbNodeRef;
17pub use crate::{
18    codec::{PbLink, PbNode},
19    error::Error,
20};
21
22/// Convert from [`ipld_core::ipld::Ipld`] into serialized DAG-PB.
23pub fn from_ipld(ipld: &Ipld) -> Result<Vec<u8>, Error> {
24    let node: PbNodeRef = ipld.try_into()?;
25    Ok(node.into_bytes())
26}
27
28/// Convert from serialized DAG-PB to [`ipld_core::ipld::Ipld`].
29pub fn to_ipld(bytes: &[u8]) -> Result<Ipld, Error> {
30    let node = PbNode::from_bytes(Bytes::copy_from_slice(bytes))?;
31    Ok(node.into())
32}
33
34/// Extract all the links from a serialize DAG-PB object.
35pub fn links(bytes: &[u8], links: &mut impl Extend<Cid>) -> Result<(), Error> {
36    let node = PbNode::from_bytes(Bytes::copy_from_slice(bytes))?;
37    for link in node.links {
38        links.extend(Some(link.cid));
39    }
40    Ok(())
41}
42
43/// DAG-PB implementation of ipld-core's `Codec` trait.
44#[derive(Copy, Clone, Debug, PartialEq, Eq)]
45pub struct DagPbCodec;
46
47impl Codec<Ipld> for DagPbCodec {
48    const CODE: u64 = 0x70;
49    type Error = Error;
50
51    fn decode<R: BufRead>(mut reader: R) -> Result<Ipld, Self::Error> {
52        let mut bytes = Vec::new();
53        reader.read_to_end(&mut bytes)?;
54        crate::to_ipld(&bytes)
55    }
56
57    fn encode<W: Write>(mut writer: W, data: &Ipld) -> Result<(), Self::Error> {
58        let bytes = crate::from_ipld(data)?;
59        Ok(writer.write_all(&bytes)?)
60    }
61}
62
63impl Links for DagPbCodec {
64    type LinksError = Error;
65
66    fn links(data: &[u8]) -> Result<impl Iterator<Item = Cid>, Self::LinksError> {
67        let mut links = Vec::new();
68        crate::links(data, &mut links)?;
69        Ok(links.into_iter())
70    }
71}
72
73impl Codec<PbNode> for DagPbCodec {
74    const CODE: u64 = 0x70;
75    type Error = Error;
76
77    fn decode<R: BufRead>(mut reader: R) -> Result<PbNode, Self::Error> {
78        let mut bytes = Vec::new();
79        reader.read_to_end(&mut bytes)?;
80        PbNode::from_bytes(Bytes::copy_from_slice(&bytes))
81    }
82
83    fn encode<W: Write>(mut writer: W, data: &PbNode) -> Result<(), Self::Error> {
84        let bytes = data.clone().into_bytes();
85        Ok(writer.write_all(&bytes)?)
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    use std::collections::BTreeMap;
94
95    #[test]
96    fn test_encode_decode() {
97        let cid =
98            Cid::try_from("bafkreie74tgmnxqwojhtumgh5dzfj46gi4mynlfr7dmm7duwzyvnpw7h7m").unwrap();
99        let mut pb_link = BTreeMap::<String, Ipld>::new();
100        pb_link.insert("Hash".to_string(), cid.into());
101        pb_link.insert("Name".to_string(), "block".to_string().into());
102        pb_link.insert("Tsize".to_string(), 13.into());
103
104        let links: Vec<Ipld> = vec![pb_link.into()];
105        let mut pb_node = BTreeMap::<String, Ipld>::new();
106        pb_node.insert("Data".to_string(), b"Here is some data\n".to_vec().into());
107        pb_node.insert("Links".to_string(), links.into());
108        let data: Ipld = pb_node.into();
109
110        let bytes = from_ipld(&data).unwrap();
111        let data2 = to_ipld(&bytes).unwrap();
112        assert_eq!(data, data2);
113    }
114}