Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
4.0 kB · 137 lines
Rust
at wip
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138//! A composed codec that covers the well-known codecs that cover the entire IPLD data model.
use ipld_core::{ cid::Cid, codec::{Codec, Links}, ipld::Ipld,};use std::io::{BufRead, Write};use thiserror::Error;
#[cfg(feature = "dag_cbor")]use serde_ipld_dagcbor::codec::DagCborCodec;
#[cfg(feature = "dag_json")]use serde_ipld_dagjson::codec::DagJsonCodec;
#[cfg(feature = "test_utils")]use proptest::prelude::*;
/// A composed codec that covers the well-known codecs that cover the entire IPLD data model.#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]pub enum TotalCodec { /// A tag representing [`DagCborCodec`]. #[default] DagCbor,
/// A tag representing [`DagJsonCodec`]. DagJson,}
impl Links for TotalCodec { type LinksError = LinksError;
fn links(&self, bytes: &[u8]) -> Result<impl Iterator<Item = Cid>, Self::LinksError> { match self { TotalCodec::DagCbor => Ok(DagCborCodec.links(bytes)?.collect::<Vec<_>>().into_iter()), // FIXME gross TotalCodec::DagJson => Ok(DagJsonCodec.links(bytes)?.collect::<Vec<_>>().into_iter()), // FIXME gross } }}
/// The union of errors for the [`TotalCodec`] variants.#[derive(Debug, Error)]pub enum LinksError { /// [`DagCborCodec`] errors. #[error("dag_cbor links error: {0}")] DagCborLinksError(#[from] serde_ipld_dagcbor::error::CodecError),
/// [`DagJsonCodec`] errors. #[error("dag_json links error: {0}")] DagJsonLinksError(#[from] serde_ipld_dagjson::error::CodecError),}
impl<T: serde::Serialize + for<'de> serde::Deserialize<'de>> Codec<T> for TotalCodec { type Error = (); // FIXME
fn to_code(&self) -> u64 { match self { TotalCodec::DagCbor => Codec::<Ipld>::to_code(&DagCborCodec), TotalCodec::DagJson => Codec::<Ipld>::to_code(&DagJsonCodec), } }
fn try_from_code(code: u64) -> Option<Self> { if code == Codec::<Ipld>::to_code(&DagCborCodec) { return Some(TotalCodec::DagCbor); }
if code == Codec::<Ipld>::to_code(&DagJsonCodec) { return Some(TotalCodec::DagJson); }
None }
fn encode<W: Write>(&self, writer: W, data: &T) -> Result<(), Self::Error> { match self { TotalCodec::DagCbor => DagCborCodec.encode(writer, data).map_err(|_| ()), TotalCodec::DagJson => DagJsonCodec.encode(writer, data).map_err(|_| ()), } }
fn decode<R: BufRead>(&self, reader: R) -> Result<T, Self::Error> { match self { TotalCodec::DagCbor => DagCborCodec.decode(reader).map_err(|_| ()), TotalCodec::DagJson => DagJsonCodec.decode(reader).map_err(|_| ()), } }}
#[cfg(feature = "dag_cbor")]impl From<DagCborCodec> for TotalCodec { fn from(_: DagCborCodec) -> Self { TotalCodec::DagCbor }}
#[cfg(feature = "dag_json")]impl From<DagJsonCodec> for TotalCodec { fn from(_: DagJsonCodec) -> Self { TotalCodec::DagJson }}
#[cfg(feature = "dag_cbor")]impl TryFrom<TotalCodec> for DagCborCodec { type Error = &'static str;
fn try_from(value: TotalCodec) -> Result<Self, Self::Error> { match value { TotalCodec::DagCbor => Ok(DagCborCodec), _ => Err("not a DagCborCodec"), } }}
#[cfg(feature = "dag_json")]impl TryFrom<TotalCodec> for DagJsonCodec { type Error = &'static str;
fn try_from(value: TotalCodec) -> Result<Self, Self::Error> { match value { TotalCodec::DagJson => Ok(DagJsonCodec), _ => Err("not a DagJsonCodec"), } }}
#[cfg(feature = "test_utils")]impl Arbitrary for TotalCodec { type Parameters = (); type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy { prop_oneof![Just(TotalCodec::DagCbor), Just(TotalCodec::DagJson)].boxed() }}