libipld_core/
raw.rs

1//! Implements the raw codec.
2use alloc::{boxed::Box, vec, vec::Vec};
3use core::{convert::TryFrom, iter::Extend};
4
5use crate::cid::Cid;
6use crate::codec::{Codec, Decode, Encode, References};
7use crate::error::{Result, UnsupportedCodec};
8use crate::io::{Read, Seek, Write};
9use crate::ipld::Ipld;
10
11/// Raw codec.
12#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
13pub struct RawCodec;
14
15impl Codec for RawCodec {}
16
17impl From<RawCodec> for u64 {
18    fn from(_: RawCodec) -> Self {
19        0x55
20    }
21}
22
23impl TryFrom<u64> for RawCodec {
24    type Error = UnsupportedCodec;
25
26    fn try_from(_: u64) -> core::result::Result<Self, Self::Error> {
27        Ok(Self)
28    }
29}
30
31impl Encode<RawCodec> for [u8] {
32    fn encode<W: Write>(&self, _: RawCodec, w: &mut W) -> Result<()> {
33        w.write_all(self).map_err(anyhow::Error::msg)
34    }
35}
36
37impl Encode<RawCodec> for Box<[u8]> {
38    fn encode<W: Write>(&self, _: RawCodec, w: &mut W) -> Result<()> {
39        w.write_all(&self[..]).map_err(anyhow::Error::msg)
40    }
41}
42
43impl Encode<RawCodec> for Vec<u8> {
44    fn encode<W: Write>(&self, _: RawCodec, w: &mut W) -> Result<()> {
45        w.write_all(&self[..]).map_err(anyhow::Error::msg)
46    }
47}
48
49impl Encode<RawCodec> for Ipld {
50    fn encode<W: Write>(&self, c: RawCodec, w: &mut W) -> Result<()> {
51        if let Ipld::Bytes(bytes) = self {
52            bytes.encode(c, w)
53        } else {
54            Err(anyhow::Error::msg(crate::error::TypeError::new(
55                crate::error::TypeErrorType::Bytes,
56                self,
57            )))
58        }
59    }
60}
61
62impl Decode<RawCodec> for Box<[u8]> {
63    fn decode<R: Read + Seek>(c: RawCodec, r: &mut R) -> Result<Self> {
64        let buf: Vec<u8> = Decode::decode(c, r)?;
65        Ok(buf.into_boxed_slice())
66    }
67}
68
69impl Decode<RawCodec> for Vec<u8> {
70    fn decode<R: Read + Seek>(_: RawCodec, r: &mut R) -> Result<Self> {
71        let mut buf = vec![];
72        r.read_to_end(&mut buf).map_err(anyhow::Error::msg)?;
73        Ok(buf)
74    }
75}
76
77impl Decode<RawCodec> for Ipld {
78    fn decode<R: Read + Seek>(c: RawCodec, r: &mut R) -> Result<Self> {
79        let bytes: Vec<u8> = Decode::decode(c, r)?;
80        Ok(Ipld::Bytes(bytes))
81    }
82}
83
84impl<T> References<RawCodec> for T {
85    fn references<R: Read, E: Extend<Cid>>(_c: RawCodec, _r: &mut R, _set: &mut E) -> Result<()> {
86        Ok(())
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    #[test]
95    fn test_raw_codec() {
96        let data: &[u8] = &[0, 1, 2, 3];
97        let bytes = RawCodec.encode(data).unwrap();
98        assert_eq!(data, &*bytes);
99        let data2: Vec<u8> = RawCodec.decode(&bytes).unwrap();
100        assert_eq!(data, &*data2);
101
102        let ipld = Ipld::Bytes(data2);
103        let bytes = RawCodec.encode(&ipld).unwrap();
104        assert_eq!(data, &*bytes);
105        let ipld2: Ipld = RawCodec.decode(&bytes).unwrap();
106        assert_eq!(ipld, ipld2);
107    }
108}