-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: data format snapshot tests (#314)
* Add old snapshot impl * Update snapshots * Add support for snapshotting the entire fs public and private * Fix file extarnalcontent name issue * Fix non determinism in snapshot tests * Fix memoryblockstore serde issue * Fix doc * Fix wait-timeout dep issue * PrivateRef renames and serialization changes * Use serde_bytes and serde_byte_array for bytes and vecs * serde rename_all * Fixes from reviews * Fix Ipld decode issue * Fix snap files * Rebase fix --------- Signed-off-by: Stephen Akinyemi <[email protected]>
- Loading branch information
Showing
53 changed files
with
2,690 additions
and
232 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,37 +1,46 @@ | ||
/// Helper methods for decoding and encoding values into DagCbor. | ||
pub mod dagcbor { | ||
use crate::{AsyncSerialize, BlockStore}; | ||
use anyhow::Result; | ||
use libipld::{ | ||
cbor::DagCborCodec, | ||
codec::{Decode, Encode}, | ||
serde as ipld_serde, Ipld, | ||
}; | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
use std::io::Cursor; | ||
use crate::{AsyncSerialize, BlockStore}; | ||
use anyhow::Result; | ||
use libipld::{ | ||
codec::{Decode, Encode}, | ||
prelude::Codec, | ||
serde as ipld_serde, Ipld, | ||
}; | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
use std::io::Cursor; | ||
|
||
/// Encodes a serializable value into DagCbor bytes. | ||
pub fn encode<S: Serialize>(value: &S) -> Result<Vec<u8>> { | ||
let ipld = ipld_serde::to_ipld(value)?; | ||
let mut bytes = Vec::new(); | ||
ipld.encode(DagCborCodec, &mut bytes)?; | ||
Ok(bytes) | ||
} | ||
/// Encodes a serializable value into DagCbor bytes. | ||
pub fn encode<S, C>(value: &S, codec: C) -> Result<Vec<u8>> | ||
where | ||
S: Serialize, | ||
C: Codec, | ||
Ipld: Encode<C>, | ||
{ | ||
let ipld = ipld_serde::to_ipld(value)?; | ||
let mut bytes = Vec::new(); | ||
<Ipld as Encode<C>>::encode(&ipld, codec, &mut bytes)?; | ||
Ok(bytes) | ||
} | ||
|
||
/// Encodes an async serializable value into DagCbor bytes. | ||
pub async fn async_encode<V: AsyncSerialize>( | ||
value: &V, | ||
store: &impl BlockStore, | ||
) -> Result<Vec<u8>> { | ||
let ipld = value.async_serialize_ipld(store).await?; | ||
let mut bytes = Vec::new(); | ||
ipld.encode(DagCborCodec, &mut bytes)?; | ||
Ok(bytes) | ||
} | ||
/// Encodes an async serializable value into DagCbor bytes. | ||
pub async fn async_encode<V, C>(value: &V, store: &impl BlockStore, codec: C) -> Result<Vec<u8>> | ||
where | ||
V: AsyncSerialize, | ||
C: Codec, | ||
Ipld: Encode<C>, | ||
{ | ||
let ipld = value.async_serialize_ipld(store).await?; | ||
let mut bytes = Vec::new(); | ||
<Ipld as Encode<C>>::encode(&ipld, codec, &mut bytes)?; | ||
Ok(bytes) | ||
} | ||
|
||
/// Decodes recieved DagCbor bytes into a deserializable value. | ||
pub fn decode<D: DeserializeOwned>(bytes: &[u8]) -> Result<D> { | ||
let ipld = Ipld::decode(DagCborCodec, &mut Cursor::new(bytes))?; | ||
Ok(ipld_serde::from_ipld::<_>(ipld)?) | ||
} | ||
/// Decodes recieved DagCbor bytes into a deserializable value. | ||
pub fn decode<D, C>(bytes: &[u8], codec: C) -> Result<D> | ||
where | ||
D: DeserializeOwned, | ||
C: Codec, | ||
Ipld: Decode<C>, | ||
{ | ||
let ipld = <Ipld as Decode<C>>::decode(codec, &mut Cursor::new(bytes))?; | ||
Ok(ipld_serde::from_ipld::<_>(ipld)?) | ||
} |
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
Oops, something went wrong.