1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
use crate::codec::{Code, Codec, Decode, Encode}; use crate::error::{TypeError, TypeErrorType}; use crate::ipld::Ipld; use std::io::{Read, Write}; use thiserror::Error; pub struct Raw; impl Codec for Raw { const CODE: Code = Code::Raw; type Error = RawError; } #[derive(Debug, Error)] pub enum RawError { #[error("{0}")] Io(#[from] std::io::Error), #[error("{0}")] Type(#[from] TypeError), } impl Encode<Raw> for [u8] { fn encode<W: Write>(&self, w: &mut W) -> Result<(), RawError> { Ok(w.write_all(self)?) } } impl Encode<Raw> for Box<[u8]> { fn encode<W: Write>(&self, w: &mut W) -> Result<(), RawError> { Ok(w.write_all(&self[..])?) } } impl Encode<Raw> for Vec<u8> { fn encode<W: Write>(&self, w: &mut W) -> Result<(), RawError> { Ok(w.write_all(&self[..])?) } } impl Encode<Raw> for Ipld { fn encode<W: Write>(&self, w: &mut W) -> Result<(), RawError> { if let Ipld::Bytes(bytes) = self { bytes.encode(w) } else { Err(TypeError::new(TypeErrorType::Bytes, self).into()) } } } impl Decode<Raw> for Box<[u8]> { fn decode<R: Read>(r: &mut R) -> Result<Self, RawError> { let buf: Vec<u8> = Decode::<Raw>::decode(r)?; Ok(buf.into_boxed_slice()) } } impl Decode<Raw> for Vec<u8> { fn decode<R: Read>(r: &mut R) -> Result<Self, RawError> { let mut buf = vec![]; r.read_to_end(&mut buf)?; Ok(buf) } } impl Decode<Raw> for Ipld { fn decode<R: Read>(r: &mut R) -> Result<Self, RawError> { let bytes: Vec<u8> = Decode::decode(r)?; Ok(Ipld::Bytes(bytes)) } } #[cfg(test)] mod tests { use super::*; #[test] fn test_raw_codec() { let data: &[u8] = &[0, 1, 2, 3]; let bytes = Raw::encode(data).unwrap(); assert_eq!(data, &*bytes); let data2: Vec<u8> = Raw::decode(&bytes).unwrap(); assert_eq!(data, &*data2); let ipld = Ipld::Bytes(data2); let bytes = Raw::encode(&ipld).unwrap(); assert_eq!(data, &*bytes); let ipld2: Ipld = Raw::decode(&bytes).unwrap(); assert_eq!(ipld, ipld2); } }