-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: to/from json, traits for cid
- Loading branch information
1 parent
a4246a8
commit b0ee54c
Showing
32 changed files
with
519 additions
and
703 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,45 @@ | ||
//! Traits related to [Ipld] and [DagCbor] encoding/decoding. | ||
//! | ||
//! [DagCbor]: DagCborCodec | ||
|
||
use crate::{consts::DAG_CBOR, workflow::Error, Unit}; | ||
use libipld::{ | ||
cbor::DagCborCodec, | ||
multihash::{Code, MultihashDigest}, | ||
prelude::Codec, | ||
Cid, Ipld, | ||
}; | ||
|
||
/// Trait for [DagCbor]-related encode/decode. | ||
/// | ||
/// [DagCbor]: DagCborCodec | ||
pub trait DagCbor | ||
where | ||
Self: Sized, | ||
Ipld: From<Self>, | ||
{ | ||
/// Performs the conversion from an owned `Self` to [Cid]. | ||
fn to_cid(self) -> Result<Cid, Error<Unit>> { | ||
let ipld: Ipld = self.into(); | ||
let bytes = DagCborCodec.encode(&ipld)?; | ||
let hash = Code::Sha3_256.digest(&bytes); | ||
Ok(Cid::new_v1(DAG_CBOR, hash)) | ||
} | ||
} | ||
|
||
/// Trait for [DagCbor]-related encode/decode for references. | ||
/// | ||
/// [DagCbor]: DagCborCodec | ||
pub trait DagCborRef | ||
where | ||
Self: Sized, | ||
for<'a> Ipld: From<&'a Self>, | ||
{ | ||
/// Performs the conversion from a referenced `Self` to [Cid]. | ||
fn to_cid(&self) -> Result<Cid, Error<Unit>> { | ||
let ipld: Ipld = self.into(); | ||
let bytes = DagCborCodec.encode(&ipld)?; | ||
let hash = Code::Sha3_256.digest(&bytes); | ||
Ok(Cid::new_v1(DAG_CBOR, hash)) | ||
} | ||
} |
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,47 @@ | ||
//! Traits related to [Ipld] and [DagJson] encoding/decoding. | ||
//! | ||
//! [DagJson]: DagJsonCodec | ||
|
||
use crate::{workflow::Error, Unit}; | ||
use libipld::{codec::Decode, json::DagJsonCodec, prelude::Codec, Ipld}; | ||
use std::io::Cursor; | ||
|
||
/// Trait for serializing and deserializing types to and from JSON. | ||
pub trait DagJson | ||
where | ||
Self: TryFrom<Ipld> + Clone, | ||
Ipld: From<Self>, | ||
{ | ||
/// Serialize `Self` type to JSON bytes. | ||
fn to_json(&self) -> Result<Vec<u8>, Error<Unit>> { | ||
let ipld: Ipld = self.to_owned().into(); | ||
Ok(DagJsonCodec.encode(&ipld)?) | ||
} | ||
|
||
/// Serialize `Self` type to JSON [String]. | ||
fn to_json_string(&self) -> Result<String, Error<Unit>> { | ||
let encoded = self.to_json()?; | ||
// JSON spec requires UTF-8 support | ||
let s = std::str::from_utf8(&encoded)?; | ||
Ok(s.to_string()) | ||
} | ||
|
||
/// Deserialize `Self` type from JSON bytes. | ||
fn from_json(data: &[u8]) -> Result<Self, Error<Unit>> { | ||
let ipld = Ipld::decode(DagJsonCodec, &mut Cursor::new(data))?; | ||
let from_ipld = Self::try_from(ipld).map_err(|_err| { | ||
// re-decode with an unwrap, without a clone, as we know the data is | ||
// valid JSON. | ||
Error::<Unit>::UnexpectedIpldTypeError( | ||
Ipld::decode(DagJsonCodec, &mut Cursor::new(data)).unwrap(), | ||
) | ||
})?; | ||
Ok(from_ipld) | ||
} | ||
|
||
/// Deserialize `Self` type from JSON [String]. | ||
fn from_json_string(json: String) -> Result<Self, Error<Unit>> { | ||
let data = json.as_bytes(); | ||
Self::from_json(data) | ||
} | ||
} |
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,122 @@ | ||
//! Typed cid for custom links. | ||
//! | ||
//! Extracted from [libipld::Link] to allow for custom de/serialization on | ||
//! custom types. | ||
|
||
use libipld::{ | ||
codec::{Codec, Decode, Encode}, | ||
error, Cid, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::{ | ||
cmp::Ordering, | ||
fmt, | ||
hash::{Hash, Hasher}, | ||
io::{Read, Seek, Write}, | ||
marker::PhantomData, | ||
ops::Deref, | ||
}; | ||
|
||
/// Typed cid. | ||
#[derive(Debug, Serialize, Deserialize)] | ||
#[repr(transparent)] | ||
pub struct Link<T> { | ||
cid: Cid, | ||
_marker: PhantomData<T>, | ||
} | ||
|
||
impl<T> Link<T> { | ||
/// Creates a new `Link`. | ||
pub fn new(cid: Cid) -> Self { | ||
Self { | ||
cid, | ||
_marker: PhantomData, | ||
} | ||
} | ||
|
||
/// Returns a reference to the cid. | ||
pub fn cid(&self) -> &Cid { | ||
&self.cid | ||
} | ||
} | ||
|
||
impl<T> fmt::Display for Link<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.cid.fmt(f) | ||
} | ||
} | ||
|
||
impl<T> Clone for Link<T> { | ||
fn clone(&self) -> Self { | ||
Self { | ||
cid: self.cid, | ||
_marker: self._marker, | ||
} | ||
} | ||
} | ||
|
||
impl<T> Copy for Link<T> {} | ||
|
||
impl<T> PartialEq for Link<T> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.cid.eq(other.cid()) | ||
} | ||
} | ||
|
||
impl<T> Eq for Link<T> {} | ||
|
||
impl<T> PartialOrd for Link<T> { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cid.cmp(other.cid())) | ||
} | ||
} | ||
|
||
impl<T> Ord for Link<T> { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
self.cid.cmp(other.cid()) | ||
} | ||
} | ||
|
||
impl<T> Hash for Link<T> { | ||
fn hash<H: Hasher>(&self, hasher: &mut H) { | ||
Hash::hash(self.cid(), hasher) | ||
} | ||
} | ||
|
||
impl<C: Codec, T> Encode<C> for Link<T> | ||
where | ||
Cid: Encode<C>, | ||
{ | ||
fn encode<W: Write>(&self, c: C, w: &mut W) -> error::Result<()> { | ||
self.cid().encode(c, w) | ||
} | ||
} | ||
|
||
impl<C: Codec, T> Decode<C> for Link<T> | ||
where | ||
Cid: Decode<C>, | ||
{ | ||
fn decode<R: Read + Seek>(c: C, r: &mut R) -> error::Result<Self> { | ||
Ok(Self::new(Cid::decode(c, r)?)) | ||
} | ||
} | ||
|
||
impl<T> Deref for Link<T> { | ||
type Target = Cid; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.cid() | ||
} | ||
} | ||
|
||
impl<T> AsRef<Cid> for Link<T> { | ||
fn as_ref(&self) -> &Cid { | ||
self.cid() | ||
} | ||
} | ||
|
||
impl<T> From<Cid> for Link<T> { | ||
fn from(cid: Cid) -> Self { | ||
Self::new(cid) | ||
} | ||
} |
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,9 @@ | ||
//! [libipld::Ipld] customization and extensions. | ||
|
||
mod dag_cbor; | ||
mod dag_json; | ||
mod link; | ||
|
||
pub use dag_cbor::*; | ||
pub use dag_json::*; | ||
pub use link::*; |
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
Oops, something went wrong.