-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change SDK::send's params to take new type IpldBlock.
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
Showing
14 changed files
with
337 additions
and
104 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.