Skip to content

Commit

Permalink
Change SDK::send's params to take new type IpldBlock.
Browse files Browse the repository at this point in the history
IpldBlock supports multiple codecs, including IPLD_RAW.

Serializing byte vectors will correctly serialize anything that serializes to "raw bytes".
However, it _won't_ serialize "byte lists", so `Vec<u8>` and `[u8]`
won't work. But that shouldn't be an issue in practice.

Users _should_ serialize to/from `ByteBuf`.
  • Loading branch information
arajasek committed Dec 5, 2022
1 parent 8b4f157 commit 8dcfe70
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 104 deletions.
65 changes: 35 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions ipld/encoding/src/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use serde::{Deserialize, Serialize};
use super::errors::Error;
use crate::{de, from_slice, ser, strict_bytes, to_vec};

pub const DAG_CBOR: u64 = 0x71;

/// Cbor utility functions for serializable objects
pub trait Cbor: ser::Serialize + de::DeserializeOwned {
/// Marshalls cbor encodable object into cbor bytes
Expand Down
5 changes: 5 additions & 0 deletions ipld/encoding/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,19 @@ impl From<Error> for io::Error {
/// This is used with the encoding errors, to detail the encoding protocol or any other
/// information about how the data was encoded or decoded
#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum CodecProtocol {
Unsupported,
Cbor,
Raw,
}

impl fmt::Display for CodecProtocol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
CodecProtocol::Unsupported => write!(f, "Unsupported"),
CodecProtocol::Cbor => write!(f, "Cbor"),
CodecProtocol::Raw => write!(f, "Raw"),
}
}
}
59 changes: 59 additions & 0 deletions ipld/encoding/src/ipld_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
use serde::de::value;
use {serde, serde_ipld_dagcbor};

use crate::{CodecProtocol, Error, RawBytes, DAG_CBOR, IPLD_RAW};

#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct IpldBlock {
pub codec: u64,
pub data: Vec<u8>,
}

impl IpldBlock {
pub fn deserialize<'de, T>(&'de self) -> Result<T, Error>
where
T: serde::Deserialize<'de>,
{
match self.codec {
IPLD_RAW => T::deserialize(value::BytesDeserializer::<value::Error>::new(
self.data.as_slice(),
))
.map_err(|e| Error {
description: e.to_string(),
protocol: CodecProtocol::Raw,
}),
DAG_CBOR => Ok(serde_ipld_dagcbor::from_slice(self.data.as_slice())?),
_ => Err(Error {
description: "unsupported protocol".to_string(),
protocol: CodecProtocol::Unsupported,
}),
}
}
pub fn serialize<T: serde::Serialize + ?Sized>(codec: u64, value: &T) -> Result<Self, Error> {
let data = match codec {
IPLD_RAW => crate::raw::to_vec(value)?,
DAG_CBOR => crate::to_vec(value)?,
_ => {
return Err(Error {
description: "unsupported protocol".to_string(),
protocol: CodecProtocol::Unsupported,
});
}
};
Ok(IpldBlock { codec, data })
}
pub fn serialize_cbor<T: serde::Serialize + ?Sized>(value: &T) -> Result<Self, Error> {
IpldBlock::serialize(DAG_CBOR, value)
}
}

impl From<RawBytes> for Option<IpldBlock> {
fn from(other: RawBytes) -> Self {
(!other.is_empty()).then(|| IpldBlock {
codec: DAG_CBOR,
data: other.into(),
})
}
}
5 changes: 5 additions & 0 deletions ipld/encoding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mod bytes;
mod cbor;
mod cbor_store;
mod errors;
pub mod ipld_block;
mod raw;
mod vec;
use std::io;

Expand All @@ -17,6 +19,9 @@ pub use self::cbor_store::CborStore;
pub use self::errors::*;
pub use self::vec::*;

pub const DAG_CBOR: u64 = 0x71;
pub const IPLD_RAW: u64 = 0x55;

// TODO: these really don't work all that well in a shared context like this as anyone importing
// them also need to _explicitly_ import the serde_tuple & serde_repr crates. These are _macros_,
// not normal items.
Expand Down
Loading

0 comments on commit 8dcfe70

Please sign in to comment.