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